search
HomeBackend DevelopmentPython TutorialUsing Python to implement point cloud ground detection

Using Python to implement point cloud ground detection

May 09, 2023 pm 05:28 PM
pythonpoint cloudGround inspection

1. Computer Vision Coordinate System

Before starting, it is important to understand the traditional coordinate system in computer vision. This is followed by Open3D and the Microsoft Kinect sensor. In computer vision, images are represented by a separate 2D coordinate system, where the x-axis points from left to right and the y-axis points up and down. For a camera, the origin of the 3D coordinate system is at the focus of the camera, with the x-axis pointing to the right, the y-axis pointing down, and the z-axis pointing forward.

了解点云:使用 Python 实现地面检测

Computer Vision Coordinate System

We first import the required Python library:

import numpy as np
import open3d as o3d

For better understanding, let’s start with PLY file, use Open3D to create the default 3D coordinate system and display them:

# Read point cloud:
pcd = o3d.io.read_point_cloud("data/depth_2_pcd.ply")
# Create a 3D coordinate system:
origin = o3d.geometry.TriangleMesh.create_coordinate_frame(size=0.5)
# geometries to draw:
geometries = [pcd, origin]
# Visualize:
o3d.visualization.draw_geometries(geometries)

了解点云:使用 Python 实现地面检测

With coordinate system The point cloud displayed at the origin

The blue arrow is the Z-axis, the red arrow is the X-axis, and the green arrow is the Y-axis. You can see that the point cloud is represented in the same coordinate system as the Open3D coordinate system. Now, let's get the points with the minimum and maximum values ​​for each axis:

# Get max and min points of each axis x, y and z:
x_max = max(pcd.points, key=lambda x: x[0])
y_max = max(pcd.points, key=lambda x: x[1])
z_max = max(pcd.points, key=lambda x: x[2])
x_min = min(pcd.points, key=lambda x: x[0])
y_min = min(pcd.points, key=lambda x: x[1])
z_min = min(pcd.points, key=lambda x: x[2])

We can print them, but for better visualization, we create a sphere at each point location. By default, Open3D creates 3D geometries at the origin position:

了解点云:使用 Python 实现地面检测

To move the sphere to a given position, a translation transformation is required. In the example below, the spheres are translated by the vector [1,1,1]:

了解点云:使用 Python 实现地面检测

Let's go back to our example and assign each sphere a color. For each position, we create a sphere and translate it to that position. Then we assign the correct color and finally we add it to the display.

# Colors:
RED = [1., 0., 0.]
GREEN = [0., 1., 0.]
BLUE = [0., 0., 1.]
YELLOW = [1., 1., 0.]
MAGENTA = [1., 0., 1.]
CYAN = [0., 1., 1.]


positions = [x_max, y_max, z_max, x_min, y_min, z_min]
colors = [RED, GREEN, BLUE, MAGENTA, YELLOW, CYAN]
for i in range(len(positions)):
 # Create a sphere mesh:
 sphere = o3d.geometry.TriangleMesh.create_sphere(radius=0.05)
 # move to the point position:
 sphere.translate(np.asarray(positions[i]))
 # add color:
 sphere.paint_uniform_color(np.asarray(colors[i]))
 # compute normals for vertices or faces:
 sphere.compute_vertex_normals()
 # add to geometry list to display later:
 geometries.append(sphere)


# Display:
o3d.visualization.draw_geometries(geometries)

了解点云:使用 Python 实现地面检测

In fact, the y-axis represents the height of the point: in the real world, the highest ball is the yellow ball, and the lowest ball is the green ball. However, since the y-axis is downward, the yellow sphere has the smallest value and the green sphere has the largest value.

Another interesting sphere is the cyan sphere at the origin. As we mentioned in the previous tutorial, pixels with a depth value of 0 are noise points, so the point located at the origin is the point calculated from these noise pixels (when z=0, then x=0 and y= 0).

2. Ground Detection

Now that we have shown some important points, how to perform ground detection? In the previous example, the green sphere is located on the ground. To be precise, its center corresponds to the highest point along the y-axis which is a ground point. Suppose for ground detection we change the color of all points with y_max to green

If you display the point cloud, you will notice that not all ground points are green. In fact, only one point, corresponding to the center of the previous green sphere, is green. This is due to depth camera accuracy and noise.

To overcome this limitation, we need to add a threshold so that points with y coordinates of [y_max-threshold, y_max] are considered ground points. To do this, after getting y_max, we check if the y coordinate of each point is within the interval and then set its color to green. Finally, the color attributes of the point cloud are updated and the results are displayed.

# Define a threshold:
THRESHOLD = 0.075


# Get the max value along the y-axis:
y_max = max(pcd.points, key=lambda x: x[1])[1]


# Get the original points color to be updated:
pcd_colors = np.asarray(pcd.colors)


# Number of points:
n_points = pcd_colors.shape[0]


# update color:
for i in range(n_points):
# if the current point is aground point:
if pcd.points[i][1] >= y_max - THRESHOLD:
pcd_colors[i] = GREEN# color it green


pcd.colors = o3d.utility.Vector3dVector(pcd_colors)


# Display:
o3d.visualization.draw_geometries([pcd, origin])

了解点云:使用 Python 实现地面检测

In this example, we will only color the points representing the ground green. In real-world applications, the ground is extracted to define walkable areas, such as robots or visually impaired systems, or to place objects on them, such as interior design systems. It can also be removed, so the remaining points can be segmented or classified, as in scene understanding and object detection systems.

3. Organized point cloud

We know that a point cloud is defined as a set of 3D points. A set is an unordered structure, so the point cloud represented by the set is called an unorganized point cloud. Similar to the RGB matrix, the organized point cloud is a 2D matrix with 3 channels representing the x, y and z coordinates of the points. The matrix structure provides the relationship between adjacent points, thereby reducing the time complexity of some algorithms, such as the nearest neighbor algorithm.

For example, we are writing a research paper and we want to show the results of our detection algorithm in the form of graphs. We can either take a screenshot of the point cloud or display the results on a depth image, as shown below. In my opinion, the second option is the best. In this case, an organized point cloud is needed to save the positions of depth pixels.

了解点云:使用 Python 实现地面检测

左:3D 可视化的屏幕截图 右:深度图像的结果

让我们从之前的深度图像创建一个有组织的点云。我们首先导入相机参数。我们还导入深度图像并将其转换为3通道灰度图像,以便我们可以将地面像素设置为绿色:

import imageio.v3 as iio
import numpy as np
import matplotlib.pyplot as plt


# Camera parameters:
FX_DEPTH = 5.8262448167737955e+02
FY_DEPTH = 5.8269103270988637e+02
CX_DEPTH = 3.1304475870804731e+02
CY_DEPTH = 2.3844389626620386e+02


# Read depth image:
depth_image = iio.imread('../data/depth_2.png')
# Compute the grayscale image:
depth_grayscale = np.array(256 * depth_image / 0x0fff, dtype=np.uint8)
# Convert a grayscale image to a 3-channel image:
depth_grayscale = np.stack((depth_grayscale,) * 3, axis=-1)

要计算一个有组织的点云,我们使用与上一篇教程相同的方法(Python:基于 RGB-D 图像的点云计算)。我们没有将深度图像扁平化,而是将jj和ii重塑为与深度图像相同的形状,如下所示:

# get depth image resolution:
height, width = depth_image.shape
# compute indices and reshape it to have the same shape as the depth image:
jj = np.tile(range(width), height).reshape((height, width))
ii = np.repeat(range(height), width).reshape((height, width))
# Compute constants:
xx = (jj - CX_DEPTH) / FX_DEPTH
yy = (ii - CY_DEPTH) / FY_DEPTH
# compute organised point cloud:
organized_pcd = np.dstack((xx * depth_image, yy * depth_image, depth_image))

如果你打印出创建的点云的形状,你可以看到它是一个有3个通道的矩阵(480,640,3)。如果你觉得这个代码很难理解,请回到之前的教程(Python:基于 RGB-D 图像的点云计算)。

类似地,我们像上面那样检测地面,但不是更新点的颜色并显示点云,而是更新灰度图像的像素并显示它:

# Ground_detection:
THRESHOLD = 0.075 * 1000# Define a threshold
y_max = max(organized_pcd.reshape((height * width, 3)), key=lambda x: x[1])[
1]# Get the max value along the y-axis


# Set the ground pixels to green:
for i in range(height):
for j in range(width):
if organized_pcd[i][j][1] >= y_max - THRESHOLD:
depth_grayscale[i][j] = [0, 255, 0]# Update the depth image


# Display depth_grayscale:
plt.imshow(depth_grayscale)
plt.show()

4.结论

在本教程中,为了熟悉点云,我们引入了默认坐标系统,并实现了一个简单的地面检测算法。事实上,地面检测在某些应用(如导航)中是一项重要的任务,文献中已经提出了几种算法。实现算法简单;它认为最低点是地面。然而,它的限制是,深度相机必须与地面平行,这是大多数现实应用的情况不是这样的。


The above is the detailed content of Using Python to implement point cloud ground detection. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:51CTO.COM. If there is any infringement, please contact admin@php.cn delete
Python vs. C  : Learning Curves and Ease of UsePython vs. C : Learning Curves and Ease of UseApr 19, 2025 am 12:20 AM

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Python vs. C  : Memory Management and ControlPython vs. C : Memory Management and ControlApr 19, 2025 am 12:17 AM

Python and C have significant differences in memory management and control. 1. Python uses automatic memory management, based on reference counting and garbage collection, simplifying the work of programmers. 2.C requires manual management of memory, providing more control but increasing complexity and error risk. Which language to choose should be based on project requirements and team technology stack.

Python for Scientific Computing: A Detailed LookPython for Scientific Computing: A Detailed LookApr 19, 2025 am 12:15 AM

Python's applications in scientific computing include data analysis, machine learning, numerical simulation and visualization. 1.Numpy provides efficient multi-dimensional arrays and mathematical functions. 2. SciPy extends Numpy functionality and provides optimization and linear algebra tools. 3. Pandas is used for data processing and analysis. 4.Matplotlib is used to generate various graphs and visual results.

Python and C  : Finding the Right ToolPython and C : Finding the Right ToolApr 19, 2025 am 12:04 AM

Whether to choose Python or C depends on project requirements: 1) Python is suitable for rapid development, data science, and scripting because of its concise syntax and rich libraries; 2) C is suitable for scenarios that require high performance and underlying control, such as system programming and game development, because of its compilation and manual memory management.

Python for Data Science and Machine LearningPython for Data Science and Machine LearningApr 19, 2025 am 12:02 AM

Python is widely used in data science and machine learning, mainly relying on its simplicity and a powerful library ecosystem. 1) Pandas is used for data processing and analysis, 2) Numpy provides efficient numerical calculations, and 3) Scikit-learn is used for machine learning model construction and optimization, these libraries make Python an ideal tool for data science and machine learning.

Learning Python: Is 2 Hours of Daily Study Sufficient?Learning Python: Is 2 Hours of Daily Study Sufficient?Apr 18, 2025 am 12:22 AM

Is it enough to learn Python for two hours a day? It depends on your goals and learning methods. 1) Develop a clear learning plan, 2) Select appropriate learning resources and methods, 3) Practice and review and consolidate hands-on practice and review and consolidate, and you can gradually master the basic knowledge and advanced functions of Python during this period.

Python for Web Development: Key ApplicationsPython for Web Development: Key ApplicationsApr 18, 2025 am 12:20 AM

Key applications of Python in web development include the use of Django and Flask frameworks, API development, data analysis and visualization, machine learning and AI, and performance optimization. 1. Django and Flask framework: Django is suitable for rapid development of complex applications, and Flask is suitable for small or highly customized projects. 2. API development: Use Flask or DjangoRESTFramework to build RESTfulAPI. 3. Data analysis and visualization: Use Python to process data and display it through the web interface. 4. Machine Learning and AI: Python is used to build intelligent web applications. 5. Performance optimization: optimized through asynchronous programming, caching and code

Python vs. C  : Exploring Performance and EfficiencyPython vs. C : Exploring Performance and EfficiencyApr 18, 2025 am 12:20 AM

Python is better than C in development efficiency, but C is higher in execution performance. 1. Python's concise syntax and rich libraries improve development efficiency. 2.C's compilation-type characteristics and hardware control improve execution performance. When making a choice, you need to weigh the development speed and execution efficiency based on project needs.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),