@[TOC]亲测可用
用c搞了两天,我放弃了,python不香吗?有手就能学会
直接上源码:
import pyrealsense2 as rs
import numpy as np
import cv2
# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
config.enable_stream(rs.stream.infrared, 1, 640, 480, rs.format.y8, 30)
config.enable_stream(rs.stream.infrared, 2, 640, 480, rs.format.y8, 30)
# Start streaming
pipeline.start(config)
try:
while True:
# Wait for a coherent pair of frames: depth and color
frames = pipeline.wait_for_frames()
depth_frame = frames.get_depth_frame()
color_frame = frames.get_color_frame()
ir_frame_left = frames.get_infrared_frame(1)
ir_frame_right = frames.get_infrared_frame(2)
if not depth_frame or not color_frame:
continue
# Convert images to numpy arrays
depth_image = np.asanyarray(depth_frame.get_data())
color_image = np.asanyarray(color_frame.get_data())
ir_left_image = np.asanyarray(ir_frame_left.get_data())
ir_right_image = np.asanyarray(ir_frame_right.get_data())
# Apply colormap on depth image (image must be converted to 8-bit per pixel first)
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
# Stack both images horizontally
images1 = np.hstack((color_image, depth_colormap))
images2 = np.hstack((ir_left_image, ir_right_image))
# Show images
cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
cv2.imshow('RealSense', images1)
cv2.imshow("Display pic_irt", images2)
key = cv2.waitKey(1)
# Press esc or 'q' to close the image window
if key & 0xFF == ord('q') or key == 27:
cv2.destroyAllWindows()
break
finally:
# Stop streaming
pipeline.stop()
记录下自己的过程,有问题请交流:
import pyrealsense2 as rs
import numpy as np
import cv2
# import scipy.misc
import time
def get_image():
# Configure depth and color streams
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 640, 480, rs.format.bgr8, 30)
config.enable_stream(rs.stream.infrared, 1, 640, 480, rs.format.y8, 30)
config.enable_stream(rs.stream.infrared, 2, 640, 480, rs.format.y8, 30)
# Start streaming
pipeline.start(config)
#获取图像,realsense刚启动的时候图像会有一些失真,我们保存第100帧图片。
for i in range(100):
data = pipeline.wait_for_frames()
depth = data.get_depth_frame()
color = data.get_color_frame()
ir_left = data.get_infrared_frame(1)
ir_right = data.get_infrared_frame(2)
#获取内参
dprofile = depth.get_profile()
cprofile = color.get_profile()
lprofile = ir_left.get_profile()
rprofile = ir_right.get_profile()
cvsprofile = rs.video_stream_profile(cprofile)
dvsprofile = rs.video_stream_profile(dprofile)
lvsprofile = rs.video_stream_profile(lprofile)
rvsprofile = rs.video_stream_profile(rprofile)
color_intrin=cvsprofile.get_intrinsics()
print(color_intrin)
depth_intrin=dvsprofile.get_intrinsics()
print(depth_intrin)
ir_left_intrin = lvsprofile.get_intrinsics()
print(ir_left_intrin)
ir_right_intrin = rvsprofile.get_intrinsics()
print(ir_right_intrin)
#外参
# extrin = dprofile.get_extrinsics_to(cprofile)
# print(extrin)
extrin = lprofile.get_extrinsics_to(rprofile)
print(extrin)
depth_image = np.asanyarray(depth.get_data())
color_image = np.asanyarray(color.get_data())
ir_left_image = np.asanyarray(ir_left.get_data())
ir_right_image = np.asanyarray(ir_right.get_data())
# Apply colormap on depth image (image must be converted to 8-bit per pixel first)
depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
# Show images
images0 = np.hstack((ir_left_image, ir_right_image))
cv2.imshow('images0', images0)
# cv2.imshow('ir_left_image', ir_left_image)
# cv2.imshow('ir_right_image', ir_right_image)
cv2.waitKey(100)
t = time.time()
tname = str(t)[5:10]
cv2.imwrite('ir_left_image' + str(tname) + '.png', ir_left_image)
cv2.imwrite('ir_right_image' + str(tname) + '.png', ir_right_image)
cv2.imwrite('color' + str(tname) + '.png', color_image)
cv2.imwrite('depth' + str(tname) + '.png', depth_image)
cv2.imwrite('depth_colorMAP' + str(tname) + '.png', depth_colormap)
# scipy.misc.imsave('outfile1.png', depth_image)
# scipy.misc.imsave('outfile2.png', color_image)
if __name__ == "__main__":
get_image()
后面写入函数我是用时间戳做名字的,各位可以用自己喜欢的方式
结果实例
前五张分别是彩色深度、黑白深度、RGB图、左右红外。
第六张懒得裁了,见谅哈啊哈。