Python converts txt files into pcd files and visualizes them with open3D

Python converts txt files into pcd files and visualizes them with open3D

I have been working on some 3D point cloud stuff recently and encountered a problem: the point cloud data provided by many data sets are in txt format, but the data sets used in the model are basically in pcd format! I tried many methods but couldn't find a solution. Then I checked the format description of the pcd file (Click to view the pcd file format) and then thought about the c. Programming language files such as java, py, description files such as json, etc., and even a picture can be regarded as text files, but with different suffixes and the encoding method set inside, the compiler can parse different files, because each point of the pcd file The data is in ASCII encoding and is the same as the txt file, (I said a lot of nonsense before!!!)

Therefore, just manually change the txt suffix to pcd! ! Simply speechless!

Visualization

Then in order to verify my idea, I want to try point cloud visualization of my pcd file. Many people on the Internet use pcl for visualization. Here is a piece of code:

import numpy as np
import pcl.pcl_visualization
pt = pcl.load("D://code-python//Data//lidar//2094.799809520.pcd")

#转为数组 形如 24000 x 3 
points = pt.to_array()

#但是pcl显示是要 N*4 所以要扩展一列 取一列插入数组使 N*3 变为 N*4
x = points[:,0]
points = np.insert(points,3,x,axis=1)

# 这里对第四列进行赋值,它代表颜色值,根据你自己的需要赋值即可;
points[:, 3] = 255

# PointCloud_PointXYZRGB 需要点云数据是N*4,分别表示x,y,z,RGB ,其中RGB 用一个整数表示颜色;
color_cloud = pcl.PointCloud_PointXYZRGB(points)
visual = pcl.pcl_visualization.CloudViewing()

#窗口名 
visual.ShowColorCloud(color_cloud, b'sta')

flag = True
while flag:
    flag != visual.WasStopped()

But! After I pip install pcl, I got an error saying that there is no load in pcl! Then I tried various reinstallations but nothing worked! I wonder if anyone knows how to solve this problem!
Then try to use open3D for visualization. Also install the package with pip install open3d first, and then Kaka, two sentences The code displays a point cloud image! ٩(๑>◡<๑)۶

import open3d as o3d

point = o3d.io.read_point_cloud("pcd0100.pcd")

o3d.visualization.draw_geometries([point])

Insert image description here
RGB original image:
Insert image description here

Guess you like

Origin blog.csdn.net/weixin_46088099/article/details/125574775