Mastering 3D Computer Vision & Point Cloud Processing-Mod 11 — Voxel Grid to Point Cloud Conversion with Code

Rajavel PM (RPM)
6 min readMay 9, 2024

--

Introduction:

Welcome to the ✍️“3D Computer Vision & Point Cloud Processing Blog Series”. This series of blogs is your “Hands-on guide to mastering 3D point cloud processing with Python.”

In this post, we’ll delve into how the voxel grid can be converted to Point Cloud data.

Topics to Be Discussed in this Blog

  1. Intro to 3D Data Types
  2. Voxel Grid to Point Cloud Conversion
  3. PCD vs PCD from voxel
  4. Summary

Intro to 3D Data Types

  • There are several common 3D data types, including point clouds, meshes, and voxel grids.
  • Point clouds consist of points in 3D space representing object surfaces or features.
  • Meshes are collections of vertices, edges, and faces that define 3D object shapes.
  • Voxel grids are three-dimensional grids where each cell represents a value or attribute, often used for volumetric data.

For more details refer my previous blog.

Why Convert Voxel Grid to Point Cloud

1. Converting Voxel Grid to Point cloud is essential for several reasons,

  1. Data Reduction: Converting voxel grids to point clouds reduces the data size by representing surfaces or key features with fewer points. This helps save memory, especially for high-resolution grids.
  2. Simplification: Point clouds offer a simpler representation of an object’s surface compared to voxel grids. This simplicity is beneficial for visualization, rendering, and processing tasks.
  3. Point-Based Algorithms: Point clouds are compatible with various algorithms designed for tasks like registration, feature extraction, and classification. Converting data to a point cloud format enables the use of these algorithms.

2. Applications

  1. 3D Scanning and Reconstruction: Point clouds are commonly used to represent surfaces in 3D scanning and reconstruction, where they are generated from depth data or laser scans.
  2. LiDAR Data Processing: LiDAR sensors generate point clouds, and processing this data involves manipulating, classifying, and extracting features from point clouds.
  3. Robotics: Point clouds play a vital role in robotics for tasks like recognizing objects, navigating environments, and avoiding obstacles.
  4. Computer Vision: Point clouds are used in computer vision applications such as structure from motion (SfM) and simultaneous localization and mapping (SLAM).

3. Pros of Converting to Point Cloud

  1. Data Reduction: Point clouds are generally smaller than voxel grids, saving memory and storage space.
  2. Compatibility: Point clouds are a standard format for many 3D processing algorithms and libraries, making them compatible with various software tools.
  3. Simplified Representation: Point clouds offer a simplified surface representation that is suitable for visualization and many processing tasks.

4. Cons of Converting to Point Cloud

  1. Loss of Volumetric Information: Converting to a point cloud may lead to a loss of volumetric information, as point clouds primarily capture surface geometry.
  2. Sparsity: The point cloud may be sparser than the original voxel grid, potentially reducing the quality of certain operations. It depends on the conversion parameters as well.

5. Let’s code

Follow the below steps to run the script.

  1. Create a file named “voxel-to-pcd.py” and paste the following code into it.
  2. Download “bunny-pcd-to-voxel-001.ply” ply data from https://github.com/MatPixel/dataset-for-3d-pointcloud-processing-3d-deep-learning/blob/main/bunny-pcd-to-voxel-001.ply and place it in the same directory as the Python script.
  3. Open the file in viewer such as MeshLab (https://www.meshlab.net/) for understanding the data.
  4. Run the script using a Python interpreter such as visual studio code https://code.visualstudio.com/.
  5. The script will load the point cloud, estimate normals if needed, convert it to a mesh, and save the resulting mesh as “bunny-voxel-001-to-pcd.ply” in the same directory.

CODE:

# Import libraries
import numpy as np
import open3d as o3d

# Load a point cloud
ply_file_path_read = "bunny-pcd-to-voxel-001.ply"
voxel_grid = o3d.io.read_voxel_grid(ply_file_path_read)
voxelsize = voxel_grid.voxel_size
print(f"Voxel size of the Voxel Grid = {voxelsize}")
voxelorigin = voxel_grid.origin
print(f"Voxel Grid Origin = {voxelorigin}")

# Computer voxel centers from Voxel grid as points in the PCD
voxels = voxel_grid.get_voxels()

"""
# Approach1: get points using 'get_voxel_center_coordinate'
points_from_voxel = []
for voxel in voxels:
voxel_center = voxel_grid.get_voxel_center_coordinate(voxel.grid_index)
points_from_voxel.append(voxel_center)
"""
# Approach2: get points manually
#voxelindex = 0; voxel_center_as_point_in_pcd = voxelorigin + voxels[voxelindex].grid_index*voxelsize
#print(voxel_center_as_point_in_pcd)
points_from_voxel = np.asarray([(voxelorigin + voxel.grid_index*voxelsize) for voxel in voxels])
print(f"Number of points computed from Voxel Grid = {len(points_from_voxel)}")

# Visualize and save the PCD generated from Voxel Grid
point_cloud = o3d.geometry.PointCloud()
point_cloud.points = o3d.utility.Vector3dVector(points_from_voxel)
o3d.visualization.draw_geometries([point_cloud], width=1200, height=800)
o3d.io.write_point_cloud("bunny-voxel-001-to-pcd.ply", point_cloud, write_ascii=True)

OUTPUT:

Voxel size of the Voxel Grid = 0.001
Voxel Grid Origin = [-0.0951899 0.0324874 -0.0623736]
Number of points computed from Voxel Grid = 34583

The results indicate that a voxel grid with a voxel size of 0.001 was used, and its origin is located at coordinates [-0.0951899, 0.0324874, -0.0623736]. The voxel grid contains a total of 34583 points.

  1. Voxel Size: The voxel size of the voxel grid used for conversion is 0.001 units. A voxel size of 0.001 indicates that each voxel (3D pixel) in the grid represents a cubic volume with sides measuring 0.001 units. Smaller voxel sizes result in higher resolution but may require more memory and processing power.
  2. Voxel Grid Origin: The origin of the voxel grid, from which the conversion was performed, is located at coordinates (-0.0951899, 0.0324874, -0.0623736). These coordinates specify the position of the voxel grid’s origin in 3D space.
  3. Number of Points: The resulting point cloud contains a total of 34583 points.
Plots from left right: Original PCD, PCD from Voxel Grid and combined. You can notice the difference in the boundaries due to voxelization.

PCD vs PCD from voxel

The table below shows the difference between the Original PCD and the PCD generated from the voxel grid.

  1. Number of Points: The original PCD has 35,947 points, while the PCD computed from the voxel grid has 34,583 points. This difference in point count indicates that some information may have been lost or simplified during the conversion process from the voxel grid to the point cloud. The impact is more visible nearby object’s boundary (as you can see in the combine plot above).
  2. Coordinates: The first point’s coordinates in the original PCD are (-0.0378297, 0.12794, 0.00447467), while the computed PCD from the voxel grid has different coordinates (-0.0331899, 0.173487, -0.0033736). These differences can arise from the way voxels are sampled or interpolated to generate the point cloud from the voxel grid. While some coordinates may be similar between the two datasets, there are likely differences in the specific positions of the points due to the voxelization process.

Note that the point cloud X, Y, Z values are not exactly matching with original point cloud because the point cloud is converted to Voxel center and then converted back to point cloud. So, we can expect maximum error of +/- voxel_size/sqrt(2). [Question: Why sqrt? Answer: a point at the corner of the voxel can have a max of voxel_size/sqrt(2)]

Summary

In this blog, we discussed converting voxel grids to point clouds in 3D computer vision. Voxel grids are useful for representing 3D data but can be large. Converting them to point clouds reduces their size while keeping important surface details. We discussed why this conversion is important and its applications in various fields. The code provided shows how to convert a voxel grid to a point cloud using Python and Open3D library. The results highlighted the voxel size, origin, and the number of points in the resulting point cloud.

✨Happy exploring! Happy learning!✨

📝Next Blog Preview:

In the upcoming post, 🚀“Mastering 3D Computer Vision & Point Cloud Processing- Mod 12— Voxel Grid to Mesh Conversion with Code”.

Topics to Be Discussed in the Next Blog

  1. Voxel Grid to Mesh Conversion
  2. Intro to Commonly used File formats

Topics to Be Discussed in the Upcoming Blogs — Immediate Focus

  • Discussion on Commonly used File formats
  • Most used Dataset
  • Most Point Cloud Preprocessing Techniques

--

--

Rajavel PM (RPM)

A Computer Vision Engineer, working on 2D/3D Computer vision, 2D/3D Machine & Deep Learning, AI, Generative AI. Let's learn together. Join me!