avatarMattia Gatti

Summary

This article provides a guide on generating a 3D mesh from a point cloud using Python and the PyVista library.

Abstract

The article discusses the process of converting a point cloud, a collection of points with 3-axis coordinates, into a 3D mesh using surface reconstruction algorithms. It highlights the use of the PyVista library in Python for this process, which simplifies 3D data processing. The article provides a short code snippet for generating the mesh and mentions the importance of obtaining a point cloud of the object of interest. It also includes examples of visualizing the point cloud and resulting mesh.

Opinions

  • The author suggests that PyVista is an easy-to-use library for processing 3D data.
  • The author believes that the most challenging part of the process is obtaining a point cloud of the object of interest.
  • The author does not mention various theoretical things, implying that they are not essential to get the job done.
  • The author provides a link to their other guide on generating a 3D mesh from an image with Python.
  • The author encourages readers to support them as a writer by signing up to become a Medium member.
  • The author recommends using eye dome lighting as a shading technique to improve depth perception when visualizing point clouds.
  • The author suggests that the code has to be slightly modified depending on the source of the point cloud.

Generate a 3D Mesh from a Point Cloud with Python

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

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

3d
Deep Learning
Computer Vision
AI
Machine Learning
Recommended from ReadMedium