Mastering 3D Computer Vision & Point Cloud Processing- Mod 13— Commonly used 3D File formats PLY, PCD, OBJ with Code
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 “Different 3D File formats for Point Cloud, Mesh and voxel Grid Data”.
Topics to Be Discussed in this Blog
- Intro to 3D Data File Formats
- File Formats and Their Applications
For more details about 3D Data types refer my previous blog.
In our previous blog, we discussed different 3D data types like point clouds and meshes. In this blog, we’ll explore 3D data file formats like XYZ, PCD, and OBJ.
Intro to 3D Data File Formats
Below table explores the commonly used 3D file formats.
ASCII
- ASCII: This format stores data in a human-readable text form, making it easy to read and edit but usually larger in size.
Binary (BIN)
- Binary (BIN): Binary files store data in a non-human-readable format, which is more compact and faster to read and write compared to ASCII.
XYZ
- XYZ: This is a simple text format for storing point cloud data, where each line represents a point in 3D space with its X, Y, and Z coordinates. It’s widely used for its simplicity but can become large and unwieldy with extensive datasets.
PCD (Point Cloud Data)
- PCD: A file format used by the Point Cloud Library (PCL). It supports both ASCII and binary formats and is optimized for storing and processing point cloud data. PCD files are commonly used in robotics and computer vision applications.
PLY (Polygon File Format)
- PLY: This format can store both point cloud and mesh data. It supports both ASCII and binary formats. PLY files are versatile and widely used in 3D scanning, modeling, and visualization.
OBJ
- OBJ: This is a standard file format for representing 3D mesh data. It supports both ASCII and binary formats and is widely used in 3D modeling and computer graphics. OBJ files can store vertices, edges, faces, and texture information.
File Formats and Their Applications
- Point Cloud Data: Point clouds are typically stored in XYZ, PCD, or PLY formats. These formats are ideal for representing 3D surfaces or features and are used in applications like 3D scanning, LiDAR data processing, and robotics.
- Mesh Data: Meshes are commonly stored in PLY and OBJ formats. These formats are suitable for detailed representations of 3D objects and are used in computer graphics, CAD, and 3D printing.
- Voxel Grid Data: Voxel grids can be stored in PLY format. This format is useful for representing volumetric data and is used in applications such as medical imaging and 3D reconstruction.
Follow the below steps to run the script.
- Create a file named “file-format-conversion.py” and paste the following code into it.
- Download
* bunny-pcd.ply from https://github.com/MatPixel/dataset-for-3d-pointcloud-processing-3d-deep-learning/blob/main/bunny-mesh.ply
* bunny-mesh.ply from https://github.com/MatPixel/dataset-for-3d-pointcloud-processing-3d-deep-learning/blob/main/bunny-mesh.ply
* bunny-pcd-to-voxel-001.ply 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. - Open the file in viewer such as MeshLab (https://www.meshlab.net/) for understanding the data.
- Run the script using a Python interpreter such as visual studio code https://code.visualstudio.com/.
- The script will load point cloud, Mesh and voxel Grid and save it in different file formats. It generates different 3D data formats in the same directory.
CODE:
# Import libraries
import numpy as np
import open3d as o3d
# for Point Cloud
ply_file_path_read = "bunny-pcd.ply"
point_cloud = o3d.io.read_point_cloud(ply_file_path_read)
o3d.io.write_point_cloud("bunny-point-cloud-format-ascii-ply.ply", point_cloud, write_ascii = True, print_progress = True)
o3d.io.write_point_cloud("bunny-point-cloud-format-ascii-pcd.pcd", point_cloud, write_ascii = True, print_progress = True)
o3d.io.write_point_cloud("bunny-point-cloud-format-ascii-xyz.xyz", point_cloud, write_ascii = True, print_progress = True)
o3d.io.write_point_cloud("bunny-point-cloud-format-bin-ply.ply", point_cloud, write_ascii = False, print_progress = True)
o3d.io.write_point_cloud("bunny-point-cloud-format-bin-pcd.pcd", point_cloud, write_ascii = False, print_progress = True)
o3d.io.write_point_cloud("bunny-point-cloud-format-bin-xyz.xyz", point_cloud, write_ascii = False, print_progress = True)
# For Mesh
ply_file_path_read = "bunny-mesh.ply"
mesh = o3d.io.read_triangle_mesh(ply_file_path_read)
o3d.io.write_triangle_mesh("bunny-mesh-format-ascii-ply.ply", mesh, write_ascii = True, print_progress = True)
o3d.io.write_triangle_mesh("bunny-mesh-format-ascii-obj.obj", mesh, write_ascii = True, print_progress = True)
o3d.io.write_triangle_mesh("bunny-mesh-format-bin-ply.ply", mesh, write_ascii = False, print_progress = True)
o3d.io.write_triangle_mesh("bunny-mesh-format-bin-obj.obj", mesh, write_ascii = False, print_progress = True)
# For Voxel Grid
ply_file_path_read = "bunny-pcd-to-voxel-001.ply"
voxel_grid = o3d.io.read_voxel_grid(ply_file_path_read)
o3d.io.write_voxel_grid("bunny-voxel-format-ascii-ply.ply", voxel_grid, write_ascii = True, print_progress = True)
o3d.io.write_voxel_grid("bunny-voxel-format-bin-ply.ply", voxel_grid, write_ascii = False, print_progress = True)
OUTPUT:
bunny-point-cloud-format-ascii-ply.ply
bunny-point-cloud-format-ascii-pcd.pcd
bunny-point-cloud-format-ascii-xyz.xyz
bunny-point-cloud-format-bin-ply.ply
bunny-point-cloud-format-bin-pcd.pcd
bunny-point-cloud-format-bin-xyz.xyz
bunny-mesh-format-ascii-ply.ply
bunny-mesh-format-ascii-obj.obj
bunny-mesh-format-bin-ply.ply
bunny-mesh-format-bin-obj.obj
bunny-voxel-format-ascii-ply.ply
bunny-voxel-format-bin-ply.ply
Sample Output
1. bunny-mesh-format-ascii-ply.ply
For your reference, the content from the file “bunny-mesh-format-ascii-ply.ply” is provided below (content viewed in notepad++).
ply
format ascii 1.0
comment Created by Open3D
element vertex 35947
property double x
property double y
property double z
property double nx
property double ny
property double nz
element face 71600
property list uchar uint vertex_indices
end_header
-0.0378297 0.12794 0.00447467 0.223443 0.969626 -0.0994941
-0.0447794 0.128887 0.00190497 0.302825 0.918277 -0.255076
-0.0680095 0.151244 0.0371953 0.0930339 0.776336 0.623416
-0.00228741 0.13015 0.0232201 -0.0645244 0.968816 0.239232
The PLY file shown above contains information about a 3D Mesh data. Here’s is the details.
General:
- Format: ASCII (text-based)
- Version: 1.0
- Comment: Created by Open3D (software used to generate the file)
Elements:
- Vertices: 35947 (number of points/vertices in 3D space)
- Faces: 71600 (number of polygonal faces that connect the vertices to form the surface)
Vertex Properties:
- x, y, z (coordinates for each vertex in double precision)
- nx, ny, nz (normal vectors for each vertex, likely representing the surface orientation)
The corresponding Mesh data is shown below.
2. bunny-mesh-format-ascii-obj.obj
For your reference, the content from the file “bunny-mesh-format-ascii-obj.obj” is provided below (content viewed in notepad++).
# Created by Open3D
# object name: bunny-mesh-format-ascii-obj
# number of vertices: 35947
# number of triangles: 71600
v -0.0378297 0.12794 0.00447467
vn 0.223443 0.969626 -0.0994941
v -0.0447794 0.128887 0.00190497
vn 0.302825 0.918277 -0.255076
v -0.0680095 0.151244 0.0371953
vn 0.0930339 0.776336 0.623416
v -0.00228741 0.13015 0.0232201
vn -0.0645244 0.968816 0.239232
v -0.0226054 0.126675 0.00715587
vn -0.24771 0.963495 -0.101574
v -0.0251078 0.125921 0.00624226
vn -0.189054 0.975418 -0.113221
v -0.0371209 0.127449 0.0017956
...
...
...
f 1//1 470//470 14339//14339
f 1//1 1641//1641 470//470
f 1641//1641 586//586 470//470
f 586//586 14340//14340 470//470
f 14340//14340 14339//14339 470//470
f 14340//14340 15393//15393 14339//14339
f 15393//15393 15391//15391 14339//14339
f 15391//15391 14331//14331 14339//14339
f 14331//14331 1//1 14339//14339
f 14331//14331 2131//2131 1//1
f 2131//2131 1620//1620 1//1
f 1620//1620 1641//1641 1//1
f 1620//1620 7//7 1641//1641
f 7//7 2397//2397 1641//1641
f 2397//2397 3064//3064 1641//1641
The file data shown above contains information about a 3D Mesh data in OBJ file format. Here’s is the details.
General:
- Format: ASCII
- Comment: Created by Open3D (software used to generate the file)
- Number of vertices: 35,947
- Number of triangles: 71,600
Vertex Data (.obj v
lines):
- Each line defines a vertex with its x, y, and z coordinates.
Vertex Normal Data (.obj vn
lines):
- Some lines define vertex normals with their x, y, and z components.
Face Data (.obj f
lines):
- Each line defines a triangle (face) by referencing vertex and, normal vector indices.
You can notice the change in data structure between the PLY and OBJ file formats. Each format is suited for different applications based on their specific demands.
✨Happy exploring! Happy learning!✨
📝Next Blog Preview:
In the upcoming post, 🚀“Mastering 3D Computer Vision & Point Cloud Processing- Mod 14— ASCII vs Bin on XYZ, PCD, PLY File Formats with Code”.
Topics to Be Discussed in the Next Blog
- ASCII vs Bin on XYZ Point Cloud Data
- ASCII vs Bin on PCD Point Cloud Data
- ASCII vs Bin on PLY Point Cloud Data
Topics to Be Discussed in the Upcoming Blogs — Immediate Focus
- Discussion on Commonly used File formats Differences
- Most used Dataset
- Most Point Cloud Preprocessing Techniques