Mastering 3D Computer Vision & Point Cloud Processing-Mod5-Understanding 3D Point Clouds: View, Visualize, and Store
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 explore how to view point clouds, visualize them using software, and store them in different file formats.
Topics to Be Discussed in this Blog
- 3D Point Cloud Data Structure
- Sample 3D Point Cloud Object
- How to view the metadata and data of a Point Cloud PLY file?
- How to visualize a Point Cloud?
- Different File Formats Used for 3D Data (How to Store a Point Cloud Data?)
📚3D Point Cloud Data Structure
In files, point clouds are represented as arrays or data structures where each element holds the coordinates of a point. More details you can find in my previous blog.
In addition to coordinates, points in a point cloud can also have optional attributes. These attributes can include color information, which specifies the color of the point, intensity information, which represents the brightness or reflectivity of the point, and normals, which indicate the direction that the point is facing.
Sample 3D Point Cloud Object
A sample PLY file is provided below for reference. In this file, points (referred to as vertices) are listed row by row, with each row containing the details of a single 3D point.
ply
format ascii 1.0
element vertex 25
property float x
property float y
property float z
end_header
1 1 10 // Vertex 1, coordinates (1, 1, 10)
2 1 20 // Vertex 2, coordinates (2, 1, 20)
3 1 30 // Vertex 3, coordinates (3, 1, 30)
4 1 40 // Vertex 4, coordinates (4, 1, 40)
5 1 50 // Vertex 5, coordinates (5, 1, 50)
1 2 26 // Vertex 6, coordinates (1, 2, 26)
2 2 27 // Vertex 7, coordinates (2, 2, 27)
3 2 28 // Vertex 8, coordinates (3, 2, 28)
4 2 29 // Vertex 9, coordinates (4, 2, 29)
5 2 30 // Vertex 10, coordinates (5, 2, 30)
...
The files below show the ‘PLY file of a cube object’ and its corresponding ‘3D object’ (viewed from a single viewpoint).
1. Cube Object (cube.ply)
ply
format ascii 1.0
element vertex 8
property float32 x
property float32 y
property float32 z
property list uint8 int32 vertex_indices
end_header
-0.5 -0.5 -0.5
0.5 -0.5 -0.5
0.5 0.5 -0.5
-0.5 0.5 -0.5
-0.5 -0.5 0.5
0.5 -0.5 0.5
0.5 0.5 0.5
-0.5 0.5 0.5
2. Cube Object
Pink-colored points represent 3D points, such as (-0.5, -0.5, -0.5), which is one of the eight points in the cube object. The white colored lines are purely for illustration purposes.
How to view the Metadata and Data of a Point Cloud PLY File?
Every point cloud data contains two types of information: Metadata (header) and Data.
- Metadata and Header: Metadata can typically be accessed by reading the header of the PLY file. The header contains information about the data structure, such as the number of points, the data format (e.g., ASCII or binary), and any other relevant details about the point cloud.
- Below is the header info from the file. The provided header information describes a PLY file containing a point cloud with 8 vertices and 6 faces. Each vertex is represented by its x, y, and z coordinates, while each face is defined by a list of vertex indices. The format of the file is ASCII 1.0
ply
format ascii 1.0
element vertex 8
property float32 x
property float32 y
property float32 z
property list uint8 int32 vertex_indices
end_header
- Data: The actual point data can be accessed by reading the body of the PLY file. This part of the file contains the point coordinates and any additional attributes (e.g., intensity, color, normals) as defined in the PLY file format specification. The data section follows the header and contains the actual point data in either ASCII or binary format, depending on how the PLY file was saved.
- Below is the data information from the file (viewed in Notepad++).
-0.5 -0.5 -0.5
0.5 -0.5 -0.5
0.5 0.5 -0.5
-0.5 0.5 -0.5
-0.5 -0.5 0.5
0.5 -0.5 0.5
0.5 0.5 0.5
-0.5 0.5 0.5
You can view metadata and data using different methods. Below are some of the most commonly used ones.
Option1: Using Text viewer/Code Editor Tools:
- You can view the data of a point cloud in Notepad++ by opening the PLY file.
- Similar software for viewing and editing text files include Sublime Text, Visual Studio Code, Atom, and TextMate.
- Header info viewed in Notepad++ is given below.
ply
format ascii 1.0
element vertex 8
property float32 x
property float32 y
property float32 z
property list uint8 int32 vertex_indices
end_header
- Data viewed in Notepad++ is given below.
-0.5 -0.5 -0.5
0.5 -0.5 -0.5
0.5 0.5 -0.5
-0.5 0.5 -0.5
-0.5 -0.5 0.5
0.5 -0.5 0.5
0.5 0.5 0.5
-0.5 0.5 0.5
Option2: Using Programming Libraries:
- Use programming libraries like Python’s plyfile library for reading PLY files.
- Write a python script (pcd-read-header-inf.py) to read the PLY file and extract header info.
# -*- coding: utf-8 -*-
"""
@author: Rajavel
"""
# Read a point cloud header info:
# Import libraries
#pip install plyfile
import plyfile
from plyfile import PlyData
print("plyfile version:", plyfile.__version__)
# Read a point cloud
ply_file_path_read = "data/cube.ply"
plydata = PlyData.read(ply_file_path_read)
# Print the header data
print("Point cloud file header details:\n", plydata)
It outputs,
Point clod file header data:
ply
format ascii 1.0
element vertex 8
property double x
property double y
property double z
end_header
Option3: Using Command Line Tools:
- Some command line tools like plyinfo (
pip install plytools)
part of the plytools package) can display metadata information directly from the command line. - Use the command plyinfo <filename>.ply to see the metadata details of the PLY file.
plyinfo cube.ply
How to visualize a Point Cloud?
Visualizing point clouds is essential for understanding and analyzing 3D data. Various software tools and libraries are available for this purpose, each offering different features and capabilities. Some of the most popular ones are listed below.
- MeshLab : MeshLab is an open-source tool used for processing and editing 3D triangular meshes and point clouds. It provides a wide range of tools for visualization, editing, cleaning, and rendering of 3D data. (https://www.meshlab.net/)
- Open3D: Open3D is a powerful open-source library for 3D data processing and point cloud visualization. It offers a wide range of functions for point cloud registration, filtering, and reconstruction. (http://www.open3d.org/)
- Unity 3D: Unity 3D is a popular game development platform that can also be used for interactive point cloud visualization. It provides tools for creating immersive 3D experiences and simulations. (https://unity.com/)
- CloudCompare: CloudCompare is a standalone software designed for visualizing, editing, processing, and analyzing point clouds. It offers various features such as registration, comparison, and segmentation of point clouds. (https://www.cloudcompare.org/)
- MATLAB: MATLAB is a high-level programming language and interactive environment used for data analysis, visualization, and numerical computation. It includes tools for processing and visualizing 3D data, including point clouds. (https://www.mathworks.com/products/matlab.html)
- Matplotlib: Matplotlib is a widely used open-source library for creating static, animated, and interactive visualizations in Python. While it’s not specifically designed for point cloud visualization, it can be used for basic visualizations. (https://matplotlib.org/)
Different File Formats Used for 3D Data (How to Store a Point Cloud Data?)
Storing point cloud data efficiently is crucial, and the choice of file format plays a significant role in how effectively and flexibly the data can be stored and accessed.
- Efficiency: File formats should store point cloud data efficiently, meaning they occupy less file size and speed up data read and write operations. This efficiency is crucial when dealing with large datasets, common in LiDAR scans or 3D reconstructions.
- Flexibility: Different file formats offer varying levels of flexibility in storing additional attributes such as color, intensity, and normals.
- Compatibility: Choosing a widely used file format ensures compatibility with various software tools and libraries, making it easier to share and process the data across different platforms and applications. LAS, for instance, is a standard format in the geospatial industry, ensuring interoperability with GIS software.
- Ease of Use: Some file formats, like XYZ, offer simplicity in data representation, making them easy to create and manipulate. This simplicity can be advantageous for quick prototyping or when detailed attributes are not required.
Below are the most commonly used file formats for 3D point cloud data. The colored cube object below is used for illustration. It has eight colored points.
Format 1: XYZ
The XYZ format is a simple, text-based file format used for storing point cloud data. It is named after the three coordinates it stores for each point: X, Y, and Z. Each line in an XYZ file represents a single point, with the X, Y, and Z coordinates separated by spaces or tabs.
The cube object in XYZ file format (cube.xyz) viewed in Notepad++ is shown below.
1 0 0
1 1 0
0 1 0
0 0 1
1 0 1
1 1 1
0 1 1
The “cube.xyz” file represents the vertices of a cube in 3D space. Each line in the file corresponds to a vertex of the cube, with the format (X, Y, Z) indicating the coordinates of the vertex.
For example:
. The first line “1 0 0” represents a vertex at coordinates (1, 0, 0).
. The second line “1 1 0” represents a vertex at coordinates (1, 1, 0).
. And so on, for the remaining vertices of the cube.
The XYZ format is simple and easy to read, making it a common choice for storing basic point cloud data. However, it lacks support for additional attributes such as color or intensity, which may be important for some applications.
Format 2: PLY (Polygon File Format)
The PLY is a versatile and flexible format commonly used for storing 3D data, including point clouds, textures, and mesh information. One of its key strengths is its ability to support various data attributes for points, such as color and normals. This means that in addition to the geometric coordinates (X, Y, Z) of each point, PLY files can also include information about the color of the point and its surface normal, which is the direction perpendicular to the surface at that point.
The inclusion of color and normals makes PLY files suitable for storing detailed geometry and color information, which is useful for applications that require high-fidelity 3D models. Another advantage of the PLY format is its flexibility in storage options.
PLY format’s versatility and flexibility make it a popular choice for storing detailed 3D data.
The cube object in PLY file format (cube.ply) viewed in Notepad++ is shown below.
ply
format ascii 1.0
element vertex 8
property double x
property double y
property double z
property double nx
property double ny
property double nz
property uchar red
property uchar green
property uchar blue
end_header
0 0 0 0 0 -1 255 0 0
1 0 0 0 0 -1 0 255 0
1 1 0 0 0 -1 0 0 255
0 1 0 0 0 -1 255 255 0
0 0 1 0 0 1 255 0 255
1 0 1 0 0 1 0 255 255
1 1 1 0 0 1 255 255 255
0 1 1 0 0 1 255 255 255
The details are,
- ply: This line indicates the beginning of the PLY file.
- format ascii 1.0: Specifies that the file format is ASCII with version 1.0.
- element vertex 8: Indicates that there are 8 vertices (points) in the file.
- property double x, property double y, property double z: Define properties for each vertex, specifying their X, Y, and Z coordinates.
- property double nx, property double ny, property double nz: Define properties for the vertex normals, specifying their X, Y, and Z components.
- property uchar red, property uchar green, property uchar blue: Define properties for the vertex colors, specifying their red, green, and blue components as unsigned characters (8-bit integers).
- end_header: Marks the end of the header section and the beginning of the data section.
- Following the header, there are 8 lines of data, each representing a vertex with its coordinates (x, y, z), normal vector (nx, ny, nz), and color (red, green, blue). For example, the first line “0 0 0 0 0 -1 255 0 0” represents a vertex at coordinates (0, 0, 0), with a normal vector of (0, 0, -1), and a color of (255, 0, 0) in RGB format.
- Similarly, the other lines represent the remaining vertices of the cube, each with its coordinates, normal vector, and RGB color.
Format 3: PCD (Point Cloud Data)
PCD (Point Cloud Data) is a file format specifically designed for storing point cloud data, offering a comprehensive solution for rich data representation. Unlike simpler formats like XYZ, PCD can store additional information beyond just point coordinates. This includes data such as surface normals, colors, and other attributes associated with each point.
PCD files provide a structured and flexible way to store point cloud data and the format allows for efficient storage and retrieval of complex point cloud data.
The cube object in PCD file format (cube.pcd) viewed in Notepad++ is shown below.
# .PCD v0.7 - Point Cloud Data file format
VERSION 0.7
FIELDS x y z normal_x normal_y normal_z rgb
SIZE 4 4 4 4 4 4 4
TYPE F F F F F F F
COUNT 1 1 1 1 1 1 1
WIDTH 8
HEIGHT 1
VIEWPOINT 0 0 0 1 0 0 0
POINTS 8
DATA ascii
0 0 0 0 0 -1 2.341805152e-38
1 0 0 0 0 -1 9.147676375e-41
1 1 0 0 0 -1 3.573311084e-43
0 1 0 0 0 -1 2.350952828e-38
0 0 1 0 0 1 2.341840885e-38
1 0 1 0 0 1 9.183409486e-41
1 1 1 0 0 1 2.350988562e-38
0 1 1 0 0 1 2.350988562e-38
The details are,
1. VERSION 0.7: Indicates the version of the .PCD file format.
2. FIELDS x y z normal_x normal_y normal_z rgb: Specifies the fields present in each point. In this case, the fields are x, y, z (coordinates), normal_x, normal_y, normal_z (normal vectors), and rgb (color).
3. SIZE 4 4 4 4 4 4 4: Specifies the size (in bytes) of each field. For example, x, y, z, and rgb are 4 bytes each.
4. TYPE F F F F F F F: Specifies the data type of each field. ‘F’ indicates floating point.
5. COUNT 1 1 1 1 1 1 1: Specifies the number of elements for each field. In this case, each field has only one element per point.
6. WIDTH 8: Specifies the number of points in the point cloud.
7. HEIGHT 1: Specifies the height of the point cloud. For unorganized point clouds, this value is usually 1.
8. VIEWPOINT 0 0 0 1 0 0 0: Specifies the viewpoint of the sensor used to capture the point cloud. This line is optional and can be used to describe the sensor’s position and orientation.
9. POINTS 8: Indicates the total number of points in the point cloud.
10. DATA ascii: Specifies the data format. ‘ascii’ indicates that the data is stored in ASCII format.
11. The DATA section contains the actual point cloud data. Each line represents a single point, with the fields separated by spaces. For example, the first point has coordinates (0, 0, 0), normal vector (0, 0, -1), and RGB color value 2.341805152e-38. The value 2.341805152e-38 likely represents the RGB color information of the point in a compressed form. The specific encoding depends on the data and the format used. More details about PCD can be found here https://pointclouds.org/documentation/tutorials/pcd_file_format.html
Format 4: OBJ (Object)
OBJ is a file format commonly used for representing 3D models in computer graphics. While its primary purpose is for 3D models, OBJ files can also be used to store point cloud data, albeit with some limitations compared to specialized formats like PCD. OBJ files can store point cloud data along with associated attributes such as normals, colors, and textures. Colors and textures can be used to add visual information to the points.
However, OBJ is not as specialized for point cloud data as formats like PCD. It lacks some of the advanced features and optimizations specifically designed for point clouds, such as efficient storage of large datasets or support for additional attributes beyond normals, colors, and textures.
The cube object in OBJ file format (cube.obj) viewed in Notepad++ is shown below.
mtllib cube-pcd.mtl
o obj1
v 1 0 0
v 1 1 0
v 0 1 0
v 0 0 1
v 1 0 1
v 1 1 1
v 0 1 1
# Vertices: 7, normals: 7, texture coordinates: 0, faces: 0
The details are,
- mtllib cube-pcd.mtl: This line indicates that the OBJ file references a material library file named “cube-pcd.mtl.” Material library files (.mtl) contain information about materials and textures used in the 3D model. We will discuss the “mtllib cube-pcd.mtl” file in detail in upcoming blogs.
- o obj1: This line defines an object named “obj1” in the OBJ file. Objects in OBJ files are collections of vertices, faces, and other geometric data.
- v 1 0 0: This line represents a vertex with coordinates (1, 0, 0) in 3D space. The “v” stands for vertex.
- v 1 1 0, v 0 1 0, …: Similarly, these lines define additional vertices of the object, each with its own set of XYZ coordinates.
- # Vertices: 7, normals: 7, texture coordinates: 0, faces: 0: This comment line provides information about the geometry in the OBJ file. It states that there are 7 vertices and 7 normals defined, with no texture coordinates or faces (triangles or polygons) specified.
Format 5: LAZ (LASzip-compressed)
LAS stands for “LiDAR (Light Detection and Ranging) Data Exchange Standard, is a standard format for LiDAR point cloud data, commonly used in the geospatial industry. LAZ is the compressed version of LAS data. It supports various data attributes and can handle large datasets, including information like classification and intensity, making it suitable for advanced point cloud applications.
✨Happy exploring! Happy learning!✨
📝Next Blog Preview:
In the upcoming post, 🚀“Mastering 3D Computer Vision & Point Cloud Processing- Mod 6–Different File Formats for Reading and Writing Point Cloud Data”.
Topics to Be Discussed in the Next Blog
- Different Formats for Reading and writing the Point Cloud Data
- ASCII Format
- Binary Format
- Difference between ASCII and Binary 3D Point Cloud Data
Topics to Be Discussed in the Upcoming Blogs — Immediate Focus
- 3D Python Libraries Installation for Hands-on Practice
- Know your 3D Python Libraries
- Libraries for Visualizing Point Clouds
- Mostly used Tools and libraries for Point Cloud Visualization by developers
- Sample Point cloud visualization using Matplotlib and Open3D
- Explore 3D Metadata