python 直接读取深度图像、rgb图像、点云并可视化

article/2025/8/4 9:50:14

以Intel的D435I相机为例,我们可以使用python代码,进行深度图像、RGB图像的读取和点云的可视化:

import pyrealsense2 as rs
import numpy as np
import cv2import pcl
from pcl import pcl_visualizationcloud = pcl.PointCloud_PointXYZRGB()
def visual(visual_viewer, pt,color):length = len(pt)points = np.zeros((length, 4),dtype=np.float32)for i in range(length):points[i][0] = pt[i][0]points[i][1] = pt[i][1]points[i][2] = pt[i][2]points[i][3] = color[i][1]# pt = [list(i) for i in pt]# pt = np.array([*pt])# pt = np.hstack((pt, np.uint8(color)))cloud.from_array(points) #从array构建点云的方式visual_viewer.ShowColorCloud(cloud)v = True# while v:#     v = not (visual_viewer.WasStopped())if __name__ == "__main__":# Configure depth and color streamspipeline = 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)# Start streamingpipeline.start(config)#深度图像向彩色对齐align_to_color=rs.align(rs.stream.color)pc = rs.pointcloud()points = rs.points()visual_viewer = pcl_visualization.CloudViewing()try:while True:# Wait for a coherent pair of frames: depth and colorframes = pipeline.wait_for_frames()frames = align_to_color.process(frames)depth_frame = frames.get_depth_frame()color_frame = frames.get_color_frame()if not depth_frame or not color_frame:continue# Convert images to numpy arraysdepth_image = np.asanyarray(depth_frame.get_data())color_image = np.asanyarray(color_frame.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 horizontallyimages = np.hstack((color_image, depth_colormap))# Get point datacolorful=color_image.reshape(-1,3)pc.map_to(color_frame)points = pc.calculate(depth_frame)#获取顶点坐标vtx = np.asanyarray(points.get_vertices())visual(visual_viewer, vtx, colorful)# Show imagescv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)cv2.imshow('RealSense', images)key = cv2.waitKey(1)# Press esc or 'q' to close the image windowif key & 0xFF == ord('q') or key == 27:cv2.destroyAllWindows()breakfinally:# Stop streamingpipeline.stop()

若以racord的*.bag文件进行读取和可视化,有两种方案:

方案一:采用intel提供的库 pyrealsense2,从文件读取数据即可:

pipeline = rs.pipeline()# Create a config object
config = rs.config()# Tell config that we will use a recorded device from file to be used by the pipeline through playback.
rs.config.enable_device_from_file(config, file_path, repeat_playback=False)
pipeline.start(config)

其他就和上面用相机读取的时候一致了。

方案二:采用rosbag读取*.bag数据,并用cv_bridge转换数据:

import os
import cv2
import numpy as np
import rosbag
from cv_bridge import CvBridge
import sensor_msgs.point_cloud2 as pc2
from sensor_msgs.msg import PointCloud2import pcl
from pcl import pcl_visualization
import pyrealsense2 as rsimport numpy as np# 相机参数
depth_camera_metrix = np.array([[920.523,0.0,641.936],   [0.0, 919.243,351.036],   [0,0,1]])def depth2xyz(depth_map,depth_cam_matrix,flatten=False,depth_scale=1000):fx,fy = depth_cam_matrix[0,0],depth_cam_matrix[1,1]cx,cy = depth_cam_matrix[0,2],depth_cam_matrix[1,2]h,w=np.mgrid[0:depth_map.shape[0],0:depth_map.shape[1]]z=depth_map/depth_scalex=(w-cx)*z/fxy=(h-cy)*z/fyxyz=np.dstack((x,y,z)) if flatten==False else np.dstack((x,y,z)).reshape(-1,3)#xyz=cv2.rgbd.depthTo3d(depth_map,depth_cam_matrix)return xyzdef visual_pcl(visual, depth, color):xyz = depth2xyz(depth, depth_camera_metrix)# colorful = np.asanyarray(color.get_data())xyz = xyz.reshape(-1, 3)color=color.reshape(-1,3)# pc.map_to(color)# points = pc.calculate(depth)# vtx = np.asanyarray(points.get_vertices())cloud = pcl.PointCloud_PointXYZRGB()temp = np.zeros((len(xyz), 4), np.float32)length = len(xyz)for i in range(length):temp[i][0] = xyz[i][0] * 1000.0temp[i][1] = xyz[i][1] * 1000.0temp[i][2] = xyz[i][2] * 1000.0temp[i][3] = color[i][0]cloud.from_array(temp)visual.ShowColorCloud(cloud)if __name__ == "__main__":file_path = "./stairs.bag"visual = pcl_visualization.CloudViewing()pc = rs.pointcloud()points = rs.points()bag = rosbag.Bag(file_path, "r")bag_data = bag.read_messages()info = bag.get_type_and_topic_info()print(info)bridge = CvBridge()depth = None; points_tp = Nonefor topic, msg, t in bag_data:print(topic)if topic == "/cam_1/aligned_depth_to_color/image_raw/compressed":# # cv_img = bridge.imgmsg_to_cv2(msg, "16UC1")# bridge.encoding_to_dtype_with_channels("16UC1")depth = bridge.compressed_imgmsg_to_cv2(msg)print(depth.shape)# depth = pc2.read_points(msg)# points = np.asanyarray(depth)  # 如果bag中保存了该类格式,可用此种方法读取点cv2.imshow("Image window", depth)cv2.waitKey(3)print("here")elif topic == "cam_1/color/image_raw/compressed":cv_img = bridge.compressed_imgmsg_to_cv2(msg)colorful=cv_img.reshape(-1,3)# pc.map_to(cv_img)# points = pc.calculate(depth)# vtx = np.asanyarray(points.get_vertices())visual_pcl(visual, depth, colorful)cv2.imshow("Cam1", cv_img)cv2.waitKey(3)print("end")

示例图片:

 

 

 


http://chatgpt.dhexx.cn/article/Rvv9gCKW.shtml

相关文章

彩色图像和深度图像合成点云

彩色图像 深度图像 点云图 从2D到3D(数学部分) 上面两个图像给出了机器人外部世界的一个局部的信息.假设这个世界由一个点云来描述:X{x1,…,xn}.其中每一个点,有r,g,b,x,y,z一共6个分量,表示它们颜色与空间位置。 颜色方面&#…

matlab灰度图孔洞填充,一种深度图像空洞的自动填充方法与流程

本发明涉及深度图像空洞填充技术,从彩色图像的结构相似性出发,求解空洞像素点的多尺度结构相似性,将空洞像素分为平滑区域空洞像素和非平滑区域空洞像素,并针对不同的空洞像素,采用不同的填充算法。在平滑区域空洞的填充过程中,利用最短距离聚类算法对深度图像的像素深度…

动态捕捉(四)深度图像基础知识

第一部分: 深度图像(depth image)也被称为距离影像(range image),是指将从图像采集器到场景中各点的距离(深度)作为像素值的图像,它直接反映了景物可见表面的几何形状。深度图像经过…

【论文精读】Unsupervised Deep Image Stitching: Reconstructing Stitched Features to Images(无监督的深度图像拼接)

论文下载链接 文章目录 前言摘要一、介绍二、相关工作2.1 基于特征的图像拼接2.2 基于学习的图像拼接2.3深度单应方法 》研究动机三、无监督图像拼接Ⅰ、无监督图像对齐Ⅱ、无监督图像重建 四、实验Ⅰ、数据集Ⅱ、实验细节 五、总结和未来工作Ⅰ、总结Ⅱ、局限和未来工作 前言 …

如何从点云创建深度图像,看这篇你就懂了(附详细代码)

作者I Roar冷颜CSDN 编辑I 3D视觉开发者社区 前言 目前,深度图像的获取方法有:激光雷达深度成像法、计算机立体视觉成像、坐标测量机法、莫尔条纹法、结构光法等。针对深度图像的研究重点主要集中在以下几个方面:深度图像的分割技术&#xff…

深度图像的获取原理

点击上方“小白学视觉”,选择加"星标"或“置顶” 重磅干货,第一时间送达 转载| 机器人配视觉 今天介绍一下深度图像的获取方法主要有哪些,以及这些方法会导致深度图像中存在什么样的问题。 在计算机视觉系统中,三维场景…

二维ICP配准

二维ICP配准 1. 理论1.1 ICP算法1.2 目标函数1.3 简化目标函数1.4 变换矩阵求解 2. Python实现2.1 导入模块2.2. 定义相关函数2.2.1 求解二维点间距函数2.2.2 求解二维点距离点集最近点函数2.2.3 求解二维点集间平均距离函数 2.3. 定义ICP_2D2.4. 定义点云2.5. ICP 配准 1. 理论…

CloudCompare——实现点云由粗到精的配准

目录 1.粗配准2.精配准3.合并点云4.去除重叠点5.附:手算配准精度5.1 精确选取同名点5.2 计算配准误差 6.相关链接 1.粗配准 使用Tools-> Registration -> Align (point pairs picking)工具手动选取匹配点对。此工具允许用户通过在两个实体中拾取至少三个等效点…

图像配准综述

点击上方“小白学视觉”,选择加"星标"或“置顶” 重磅干货,第一时间送达本文转自 | 新机器视觉 Image registration 图像配准 图像配准与相关 图像处理研究领域中的一个典型问题和技术难点,其目的在于比较或融合针对同一对象在不同…

PCL教程-点云配准之成对逐步配准(两两配准)

原文链接:How to incrementally register pairs of cloudshttps://pcl.readthedocs.io/projects/tutorials/en/latest/pairwise_incremental_registration.html#pairwise-incremental-registrationhttps://pcl.readthedocs.io/projects/tutorials/en/latest/pairwis…

图像配准学习小结 (一)

图像配准学习小结 (一) 一、含义和意义 图像配准是找到一组变换参数,使得变换后的待配准图像与原始图像之间的相似度达到最高,目的在于将同一场景的不同图像转换到同样的坐标系中。待配准图像与原图像可能存在时间、捕获方式、角…

图像配准概述

图像配准在医学图像领域是一项重要的技术,在许多的临床诊断中,为了分析患者的状况,常常需要采集患者的扫描影像,例如,X线、MRI、CT和超声,这些扫描影像可以对患者的诊断提供依据,然而&#xff0…

基于MATLAB的医学图像配准算法仿真

目录 一、理论基础 二、案例背景 1.问题描述 2.思路流程 三、部分MATLAB程序 四、仿真结论分析 五、算法相关应用 六、参考文献 一、理论基础 其中h表示二维空间坐标变换,g表示灰度或辐射变换,描述因传感器类型的不同或辐射变形所引入的图像变换…

图像配准简介

图像配准在目标检测、模型重建、运动估计、特征匹配,肿瘤检测、病变定位、血管造影、地质勘探、航空侦察等领域都有广泛的应用。 每一种配准方法通常都针对某个具体问题而设计的,众多方法中,唯一的共性就是每个配准问题最终都要在变换空间中寻找一种最有的变换,这种变换能…

基于深度学习的单模医学图像配准综述(附VoxelMorph配准实例)

本文是基于深度学习的单模态医学图像配准的综述,除了介绍配准任务、配准过程之外,还会从实际操作出发,以经典的VoxelMorph为例做详细介绍。如果有什么讲的不清楚的地方欢迎大家留言讨论,如果有什么错误的地方,也恳请大…

图像配准

1、定义 维基百科上的定义:图像配准与相关是图像处理研究领域中的一个典型问题和技术难点,其目的在于比较或融合针对同一对象在不同条件下获取的图像,例如图像会来自不同的采集设备,取自不同的时间,不同的拍摄视角等等…

【Image Registration】图像配准综述

文章目录 一、图像配准定义二、图像配准应用场景2.1 医学图像领域2.2 其他领域 三、图像配准分类四、图像配准过程4.1 特征检测(Feature detection)4.2 特征匹配(Feature matching)4.2.1 基于区域的方法(Area-based me…

计算机视觉 什么是图像配准?

一、图像配准概述 图像配准是叠加两个或多个来自不同来源、在不同时间和角度拍摄的图像的过程。图像配准过程是一种自动或手动操作,它试图发现两张照片之间的匹配点并在空间上对齐它们以最小化所需的误差,即两幅图像之间的统一邻近度测量。医学、遥感和计算机视觉都使用图像配…

1.图像配准简述

1.图像配准:作为图像融合的一个预处理步骤,图像配准是对两幅图像,通过寻找一种空间变换把一幅图像映射到另一幅图像,使得两图中对应于空间同一位置的点一一对应起来,从而达到信息融合的目的。 2.原理: 3.分…

机器视觉(九):图像配准

目录: 机器视觉(一):概述 机器视觉(二):机器视觉硬件技术 机器视觉(三):摄像机标定技术 机器视觉(四):空域图像增强 …