代碼解釋
import pyrealsense2 as rs
import numpy as np
import cv2
import json
import png
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)
profile = pipeline.start(config)
align_to = rs.stream.color
align = rs.align(align_to)
depth_intrin = None
def get_aligned_images():
frames = pipeline.wait_for_frames()
aligned_frames = align.process(frames)
aligned_depth_frame = aligned_frames.get_depth_frame()
color_frame = aligned_frames.get_color_frame()
intr = color_frame.profile.as_video_stream_profile().intrinsics
camera_parameters = {'fx': intr.fx, 'fy': intr.fy,
'ppx': intr.ppx, 'ppy': intr.ppy,
'height': intr.height, 'width': intr.width,
'depth_scale': profile.get_device().first_depth_sensor().get_depth_scale()
}
with open('./intrinsics.json', 'w') as fp:
json.dump(camera_parameters, fp)
depth_image = np.asanyarray(aligned_depth_frame.get_data())
depth_image_8bit = cv2.convertScaleAbs(depth_image, alpha=0.03)
pos=np.where(depth_image_8bit==0)
depth_image_8bit[pos]=255
color_image = np.asanyarray(color_frame.get_data())
d = aligned_depth_frame.get_distance(320, 240)
return color_image, depth_image,
if __name__ == "__main__":
n=0
while 1:
rgb, depth = get_aligned_images()
cv2.imshow('RGB image',rgb)
key = cv2.waitKey(1)
if key & 0xFF == ord('q') or key == 27:
pipeline.stop()
break
elif key==ord('s'):
n=n+1
cv2.imwrite('./bd/rgb' + str(n)+'.jpg',rgb)
with open('./bd/rgb' + str(n) + "_d.jpg", 'wb') as f:
writer = png.Writer(width=depth.shape[1], height=depth.shape[0],
bitdepth=16, greyscale=True)
zgray2list = depth.tolist()
writer.write(f, zgray2list)
cv2.destroyAllWindows()