Generate a 3D Mesh from a Point Cloud with Python

The fastest way to generate a 3D mesh from a point cloud

Mattia Gatti
3 min readAug 20, 2022
Photo by engin akyurt on Unsplash

Several implementations have been written in Python to obtain a mesh from a point cloud. The problem with most of them is that they imply setting many parameters which are hard to tune, especially without being a 3D data processing expert. In this short guide, I want to show the fastest and easiest process to generate a mesh from a point cloud.

Introduction

A point cloud is a collection of points with 3-axis coordinates (x, y, z). A collection of this type can come from different sources and be saved in different formats. Point clouds can be converted into 3D meshes by using different algorithms called surface reconstruction algorithms. To perform surface reconstruction this guide uses PyVista which is an easy-to-use library to process 3D data.

To install the latest version of PyVista from PyPI use:

pip install pyvista

Procedure

The code to generate the mesh is very short. You just need to provide a NumPy array with a shape of N × 3 where N is the number of points and the three columns are the x position, y position, and z position of each point. The most challenging part of the process is obtaining a point cloud of the object of interest, because once you have it, the full code to generate the mesh is very short:

In this example, the point cloud is extracted from a CSV file in the following format:

A point cloud csv. Image by the author.

You can download the CSV I used for this example here. The point cloud is part of the PyVista resources.

Regardless of the source of your points, what is important is to pass the method pv.PolyData(points) a NumPy array in the format mentioned above.

If you want to visualize the point cloud use:

point_cloud.plot(eye_dome_lighting=True)

Eye dome lighting is a shading technique to improve depth perception when visualizing point clouds.

An example of a point cloud visualization. Source file from PyVista examples.

If you want to visualize the resulting mesh use:

mesh.plot(color='orange')
An example of a mesh visualization. Source file from PyVista examples.

Conclusion

The code has to be slightly modified depending on the source of your point cloud, but otherwise, it only takes a few lines to generate a mesh. I haven’t mentioned various theoretical things, but they are not essential to get the job done. If you want to generate a 3D mesh from an image, you can also check out my other guide:

Thanks for reading, I hope you have found this useful.

If you enjoyed reading my story and want to support me as a writer consider signing up to become a Medium member. It’s 5$ a month, giving you unlimited access to all the stories. If you sign up using my link, I’ll earn a small commission and it costs you the same. https://mattiagatti.medium.com/membership

--

--

Mattia Gatti
Mattia Gatti

Written by Mattia Gatti

Currently doing research into AI Remote Sensing. Writing about Deep Learning and Geospatial Data Analysis.

Responses (3)