intel realsense保存16位深度图与rgb图程序

IntelRealsense库安装

pip install pyrealsense2 

代码解释

import pyrealsense2 as rs
import numpy as np
import cv2
import json
import png
# 开启摄像头通信管道
pipeline = rs.pipeline()
# 获取配置
config = rs.config()
# 设置深度图为16位,fps为30,尺寸640x480
config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
# 设置RGB图为bgr格式(每个通道8位共24位),fps为30,尺寸640x480
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

# 该函数作用是获取对齐后的图像(rgb和深度图对齐)
def get_aligned_images():
	# 获取一帧图像
    frames = pipeline.wait_for_frames()
    # 将图像对齐
    aligned_frames = align.process(frames)
    # 获取对齐后的深度图
    aligned_depth_frame = aligned_frames.get_depth_frame()
    # 深度相关参数
    # depth_intrin = aligned_depth_frame.profile.as_video_stream_profile().intrinsics
    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)
    # 获取16位深度图
    depth_image = np.asanyarray(aligned_depth_frame.get_data())
    # 转为8位深度图
    # 2^8为256,2^16为65536, 2^8/2^16约等于0.004,alpha=0.004,cv2.convertScaleAbs作用是所有像素点的值乘以alpha
    depth_image_8bit = cv2.convertScaleAbs(depth_image, alpha=0.004)
    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和深度图
        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
            # 保存rgb图
            cv2.imwrite('./bd/rgb' + str(n)+'.jpg',rgb)
            # 保存16位深度图
            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()


  • 7
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alex-Leung

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值