PCL-Python (70%)

Jupyter ๋ฒ„์ ผ์€ [์ด๊ณณ]์—์„œ ํ™•์ธ ๊ฐ€๋Šฅ ํ•ฉ๋‹ˆ๋‹ค.

1. ์ฝ๊ธฐ

import pcl

pc = pcl.load("./sample/lobby.pcd") # "pc.from_file" Deprecated
#cloud = pcl.load_XYZRGBA("tabletop.pcd")
print(pc)

2. ์ƒ์„ฑ

import pcl
import numpy as np


pc_array = np.array([[1, 2, 3], [3, 4, 5]], dtype=np.float32)
print(pc_array)

#๋ฐฉ๋ฒ• 1
pc = pcl.PointCloud(pc_array)
print(pc)

#๋ฐฉ๋ฒ• 2
pc = pcl.PointCloud()
pc.from_array(pc_array)
print(pc)


#๋ฐฉ๋ฒ• 3 

searchPoint = pcl.PointCloud()
searchPoints = np.zeros((1, 3), dtype=np.float32) #np.zeros((1, 4) for RGBD
searchPoints[0][0] = 1024 * random.random() / (RAND_MAX + 1.0)
searchPoints[0][1] = 1024 * random.random() / (RAND_MAX + 1.0)
searchPoints[0][2] = 1024 * random.random() / (RAND_MAX + 1.0)


#๋ฐฉ๋ฒ• 4 
p = pcl.PointCloud(10)  # "empty" point cloud
a = np.asarray(p)       # NumPy view on the cloud
a[:] = 0                # fill with zeros
print(p[3])             # prints (0.0, 0.0, 0.0)
a[:, 0] = 1             # set x coordinates to 1
print(p[3])             # prints (1.0, 0.0, 0.0)


#๋ฐฉ๋ฒ• 5 for ROS
new_cloud = pcl.PointCloud()
new_cloud.from_array(new_data)
new_cloud = pcl_helper.XYZ_to_XYZRGB(new_cloud,[255,255,255])

3. ์“ฐ๊ธฐ

import pcl

# ๋ฐฉ๋ฒ• 1
pcl.save(pc, 'pc2pcd.pcd') 
#pcl.save_XYZRGBA(pc, 'pc2pcd.pcd') #RGB-D์„ผ์„œ์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉ, x,y,z์ขŒํ‘œ ์ด์™ธ ์ƒ‰์ƒ ์ •๋ณด ํฌํ•จ


# ๋ฐฉ๋ฒ• 2
pc.to_file('pc2pcd.pcd')

4. ๋ณ€ํ™˜

์ถ”ํ›„ ๊ตฐ์ง‘ํ™”, ๋ถ„๋ฅ˜, ์ „์ฒ˜๋ฆฌ๋ฅผ ์œ„ํ•ด์„œ ์ผ๋ฐ˜์ ์œผ๋กœ Numpy๋กœ ๋ณ€ํ™˜ ํ•˜์—ฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ฏ€๋กœ ๋ณ€ํ™˜ ๊ณผ์ •์— ๋Œ€ํ•˜์—ฌ ์‚ดํŽด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

import pcl
import numpy as np

# PC to Numpy
pc_array = pc.to_array()

print("pc_array size : {}".format(pc_array.size))
print("pc Type : {}".format(type(pc)))
print("pc_array Type : {}".format(type(pc_array)))

# Numpy to PC 
pc_new = pcl.PointCloud()
pc_new.from_array(pc_array) # 2.์ƒ์„ฑ ๋ฐฉ๋ฒ•๊ณผ ๋™์ผ #dtype=np.float32


# Indices to PC
cloud = pcl.load("tabletop_passthrough.pcd")

inliers_cloud = pcl.PointCloud()
inliers = np.zeros((len(indices), 3), dtype=np.float32)

for i in range(len(indices)):
    inliers[i] = cloud[indices[i]]
inliers_cloud.from_array(inliers)

5. ์ •๋ณด ์ถœ๋ ฅ

import pcl
pc = pcl.load("./sample/lobby.pcd") 


print("ํฌ์ธํŠธ ์ˆ˜ : {}".format(pc)) 
print("ํฌ์ธํŠธ ์ˆ˜ : {}".format(pc.size)) 



# ํฌ์ธํŠธ ๊ฐ’ 
print ('Loaded ' + str(pc.width * pc.height) + ' data points from test_pcd.pcd with the following fields: ')

for i in range(0, 10):#pc.size):
    print ('x: ' + str(pc[i][0]) + ', y : ' + str(pc[i][1]) + ', z : ' + str(pc[i][2]))

Last updated

Was this helpful?