日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Intel Realsense D435 python 从深度相机realsense生成pcl点云

發布時間:2025/3/19 python 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Intel Realsense D435 python 从深度相机realsense生成pcl点云 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

引用文章:python 從深度相機realsense生成pcl點云

從深度相機realsense生成pcl點云

    • 一、通過realsense取得深度信息和彩色信息
    • 二、獲取坐標和色彩信息
    • 三、通過pcl可視化點云

一、通過realsense取得深度信息和彩色信息

ubuntu下intel realsense的軟件可以打開realsen的界面,里面可以得到彩色圖像和深度圖像,我們通過realsense的python接口獲取彩色信息和深度信息。

  • 基礎的獲取彩色和深度信息,realsense中的視頻流轉換為python的numpy格式,通過opencv輸出
  • import pyrealsense2 as rs import numpy as np import cv2 import pclif __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)try:while True:# Wait for a coherent pair of frames: depth and colorframes = pipeline.wait_for_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))# 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()
  • 獲取內參和保存圖片
    分別用opencv和scipy.misc保存圖片,略微會有一些差異,同時我們也獲取了相機參數。
  • import pyrealsense2 as rs import numpy as np import cv2 import scipy.misc import pcldef get_image():# 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)#獲取圖像,realsense剛啟動的時候圖像會有一些失真,我們保存第100幀圖片。for i in range(100):data = pipeline.wait_for_frames()depth = data.get_depth_frame()color = data.get_color_frame()#獲取內參dprofile = depth.get_profile()cprofile = color.get_profile()cvsprofile = rs.video_stream_profile(cprofile)dvsprofile = rs.video_stream_profile(dprofile)color_intrin=cvsprofile.get_intrinsics()print(color_intrin)depth_intrin=dvsprofile.get_intrinsics()print(color_intrin)extrin = dprofile.get_extrinsics_to(cprofile)print(extrin)depth_image = np.asanyarray(depth.get_data())color_image = np.asanyarray(color.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)cv2.imwrite('color.png', color_image)cv2.imwrite('depth.png', depth_image)cv2.imwrite('depth_colorMAP.png', depth_colormap)scipy.misc.imsave('outfile1.png', depth_image)scipy.misc.imsave('outfile2.png', color_image)

    二、獲取坐標和色彩信息

  • 通過realsense攝像頭,獲取了頂點坐標和色彩信息。具體并不是很了解,pc.mac_to() 和 points=pc.calculate()是把色彩和深度結合了? 再通過points獲取頂點坐標。我們將頂點坐標和彩色相機得到的像素存入txt文件。
  • def my_depth_to_cloud():pc = rs.pointcloud()points = rs.points()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)pipe_profile = pipeline.start(config)for i in range(100):data = pipeline.wait_for_frames()depth = data.get_depth_frame()color = data.get_color_frame()frames = pipeline.wait_for_frames()depth = frames.get_depth_frame()color = frames.get_color_frame()colorful = np.asanyarray(color.get_data())colorful=colorful.reshape(-1,3)pc.map_to(color)points = pc.calculate(depth)#獲取頂點坐標vtx = np.asanyarray(points.get_vertices())#獲取紋理坐標#tex = np.asanyarray(points.get_texture_coordinates())with open('could.txt','w') as f:for i in range(len(vtx)):f.write(str(np.float(vtx[i][0])*1000)+' '+str(np.float(vtx[i][1])*1000)+' '+str(np.float(vtx[i][2])*1000)+' '+str(np.float(colorful[i][0]))+' '+str(np.float(colorful[i][1]))+' '+str(np.float(colorful[i][2]))+'\n')

    三、通過pcl可視化點云

    https://github.com/strawlab/python-pcl/blob/master/examples/example.py

  • 在pcl中,要顯示三維加色彩的點云坐標,每個點云包含了 x,y,z,rgb四個參數,特別的,rbg這個參數是由三維彩色坐標轉換過來的。剛才得到的could.txt中,x,y,z,r,g,b 轉換為x,y,z,rgb,只改顏色數據np.int(data[i][3])<<16|np.int(data[i][4])<<8|np.int(data[i][5])。保存為cloud_rbg.txt
  • with open('could.txt','r') as f:lines = f.readlines()num=0for line in lines:l=line.strip().split()data.append([np.float(l[0]),np.float(l[1]),np.float(l[2]),np.float(l[3]),np.float(l[4]),np.float(l[5])])#data.append([np.float(l[0]), np.float(l[1]), np.float(l[2])])num = num+1with open('cloud_rgb.txt', 'w') as f:for i in range(len(data)):f.write(str(np.float(data[i][0])) + ' ' + str(np.float(data[i][1])) + ' ' + str(np.float(data[i][2]))+ ' ' + str(np.int(data[i][3])<<16|np.int(data[i][4])<<8|np.int(data[i][5]))+'\n')
  • 顯示
  • def visual():cloud = pcl.PointCloud_PointXYZRGB()points = np.zeros((307200,4),dtype=np.float32)n=0with open('cloud_rgb.txt','r') as f:lines = f.readlines()for line in lines:l=line.strip().split()#保存xyz時候擴大了1000倍,發現并沒有用points[n][0] = np.float(l[0])/1000points[n][1] = np.float(l[1])/1000points[n][2] = np.float(l[2])/1000points[n][3] = np.int(l[3])n=n+1print(points[0:100]) #看看數據是怎么樣的cloud.from_array(points) #從array構建點云的方式visual = pcl.pcl_visualization.CloudViewing()visual.ShowColorCloud(cloud)v = Truewhile v:v = not (visual.WasStopped())
  • 想生成pcd,再讀取pcd,但是下面的程序在可視化的時候報錯
  • def txt2pcd():import timefilename='cloud_rgb.txt'print("the input file name is:%r." % filename)start = time.time()print("open the file...")file = open(filename, "r+")count = 0# 統計源文件的點數for line in file:count = count + 1print("size is %d" % count)file.close()# output = open("out.pcd","w+")f_prefix = filename.split('.')[0]output_filename = '{prefix}.pcd'.format(prefix=f_prefix)output = open(output_filename, "w+")list = ['# .PCD v0.7 - Point Cloud Data file format\n', 'VERSION 0.7\n', 'FIELDS x y z rgb\n', 'SIZE 4 4 4 4\n','TYPE F F F U\n', 'COUNT 1 1 1 1\n']output.writelines(list)output.write('WIDTH ') # 注意后邊有空格output.write(str(count))output.write('\nHEIGHT ')output.write(str(1)) # 強制類型轉換,文件的輸入只能是str格式output.write('\nPOINTS ')output.write(str(count))output.write('\nDATA ascii\n')file1 = open(filename, "r")all = file1.read()output.write(all)output.close()file1.close()end = time.time()print("run time is: ", end - start)import pcl.pcl_visualizationcloud = pcl.load_XYZRGB('cloud_rgb.pcd')visual = pcl.pcl_visualization.CloudViewing()visual.ShowColorCloud(cloud, 'cloud')flag = Truewhile flag:flag != visual.WasStopped() # TypeError: expected bytes, str found

    原圖,深度圖,云圖(云圖一片黑,鼠標滾輪翻一下) 如下:
    opencv保存的顏色圖:

    scipy保存的顏色圖:

    深度圖:

    深度圖可視化:

    云圖(似乎深度和色彩沒有對齊):

    總結

    以上是生活随笔為你收集整理的Intel Realsense D435 python 从深度相机realsense生成pcl点云的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。