Common Vision Blox 15.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Friends Modules Pages
Access to Simple Point Clouds in Python

This section explains how to access data from the CVB point cloud classes in Python cvb.PointCloud, cvb.DensePointCloud, cvb.SparsePointCloud.

For more details on the concept behind the CVB point cloud classes, please refer to the section CVB Point Cloud.

Mapping a Point Cloud to NumPy Array Without Copying

with cloud = ...: # (1)
# Map the point cloud to a NumPy array without copying (copy=False by default).
np_array = cvb.as_array(cloud, copy=False) # (2)
# Verify if the mapping was successful.
if np_array.flags["OWNDATA"]: # (3)
raise RuntimeError("cannot map to numpy array")
numpy.array as_array(Any buffer, bool copy=False)

(1) A point cloud is loaded, acquired or created. The with statement should be used to manage the point cloud's lifetime automatically.
(2) The point cloud is mapped to a NumPy array without copying, allowing direct access to pixel data.
(3) Check whether the mapping was successful; if OWNDATA is True, the image could not be mapped. In this case, the image has to be copied by setting copy=True in previous call.

Note that the shape of the mapped NumPy array depends on the type of point cloud. For an organized cvb.DensePointCloud, the shape[0] represents the height, shape[1] represents the width, and shape[2] denotes the number of planes, as the following code example illustrates:

if type(cloud) == cvb.DensePointCloud:
print(f"Width x Height: {np_array.shape[1]} x {np_array.shape[0]}") # (1)
print(f"Number of planes: {np_array.shape[2]}")
# Modify point cloud values (Set the first two lines of points to the
# background value 0).
np_array[:2, :, -2:] = 0 # (2)
# Iterate over all grid points and print their coordinates.
for row in range(0, np_array.shape[0]): # Loop over grid in y direction (rows).
for col in range(0, np_array.shape[1]): # Loop over grid in x direction (columns).
X = np_array[row,col,0] # X-coordinate
Y = np_array[row,col,1] # Y-coordinate
Z = np_array[row,col,2] # Z-coordinate
C = np_array[row,col,3] # optional confidence
print(f"({X},{Y},{Z})")
# Save modified point cloud.
cloud.save("cloud_with_background.xyz") # (3)

(1) Print dimensions of the (dense) point cloud array.
(2) Modify point cloud values in the NumPy array. If the array has been previously mapped with option copy=False, this operation directly updates the underlying image data. In this case the modified point cloud can be saved to a file as done in step (3).

The following code example demonstrates the dimensions and how to access the array for an un-organized cvb.SparsePointCloud:

if type(cloud) == cvb.SparsePointCloud:
print(f"Number of points: {np_array.shape[0]}") # (1)
print(f"Number of planes: {np_array.shape[1]}")
# Modify point cloud values (Set the first two points to the
# background value 0).
np_array[:2, -1:] = 0 # (2)
# Iterate over all points and print their X,Y,Z coordinates.
for i in range(0, np_array.shape[0]): # Loop over points.
print(f"#{i}: ({np_array[i,0]},{np_array[i,1]},{np_array[i,2]})")
# Save modified point cloud.
cloud.save("cloud_with_background.xyz") # (3)

(1) Print dimensions of the (sparse) point cloud array.
(2) Modify point cloud values in the NumPy array. If the array has been previously mapped with option copy=False, this operation directly updates the underlying image data. In this case the modified point cloud can be saved to a file as done in step (3).

Examples and Further Reading

Point Cloud Concept
Point Cloud Stream