7 ways to implement lidar LAS data filtering using python, using laspy to read and write

LiDAR data can be affected by noise and imperfect measurements in practical applications, so data denoising and filtering methods become critical to improve data quality and accuracy. The following are some commonly used lidar data denoising and filtering methods.
The original data is as follows:
Insert image description here

1. Moving Average Filter:

Moving average filtering is a simple filtering method that smoothes data by calculating the average of the data within a certain range around the data point. This method is suitable for removing high-frequency noise, but may result in blurred edge information.

Code:

import laspy
import numpy as np
from scipy.signal import medfilt
from skimage.metrics import structural_similarity as ssim

# 读取LAS文件
in_file_path = 'F:/激光雷达/武汉地调中心/1500N点云.las'
out_file_path_ma = 'F:/激光雷达/武汉地调中心/output_moving_average.las'

in_las = laspy.file.File(in_file_path, mode='r')

# 提取点云数据
x = in_las.x
y = in_las.y
z = in_las.z

# 应用移动平均滤波
window_size = 5
filtered_z_ma = np.convolve(z, np.ones(window_size)/window_size, mode='same')

# 创建新的LAS文件并保存滤波后数据
out_las_ma = laspy.file.File(out_file_path_ma, mode='w', header=in_las.header)
out_las_ma.x = x
out_las_ma.y = y
out_las_ma.z = filtered_z_ma
out_las_ma.close()

2. Median Filter:

Median filtering is a nonlinear filtering method that sorts the values ​​around the data point by size, and then takes the middle value as the filtering result. Median filtering can effectively remove impulse noise and outliers, but may reduce data detail.

Code:

import laspy
import numpy as np
from scipy.signal import medfilt
from skimage.metrics import structural_similarity as ssim

# 读取LAS文件
in_file_path = 'F:/激光雷达/武汉地调中心/1500N点云.las'
out_file_path_med = 'F:/激光雷达/武汉地调中心/output_median.las'

in_las = laspy.file.File(in_file_path, mode='r')

# 提取点云数据
x = in_las.x
y = in_las.y
z = in_las.z

# 应用中值滤波
window_size_med = 3
filtered_z_med = medfilt(z, kernel_size=window_size_med)

# 创建新的LAS文件并保存滤波后数据
out_las_med = laspy.file.File(out_file_path_med, mode='w', header=in_las.header)
out_las_med.x = x
out_las_med.y = y
out_las_med.z = filtered_z_med
out_las_med.close()

3. Weighted Moving Average Filter:

Weighted moving average filtering assigns different weights to data points at different locations and calculates a weighted average based on the weights. This method can better balance smoothing and preserving details according to the characteristics of the data distribution.

Code:

import laspy
import numpy as np
from scipy.signal import medfilt
from skimage.metrics import structural_similarity as ssim

# 读取LAS文件
in_file_path = 'F:/激光雷达/武汉地调中心/1500N点云.las'
out_file_path_weighted_ma = 'F:/激光雷达/武汉地调中心/output_weighted_moving_average.las'

in_las = laspy.file.File(in_file_path, mode='r')

# 提取点云数据
x = in_las.x
y = in_las.y
z = in_las.z

# 应用加权移动平均滤波
window_size = 5
weights = np.arange(1, window_size + 1).astype(float)  # 转换为浮点数类型
weights /= np.sum(weights)
filtered_z_weighted_ma = np.convolve(z, weights, mode='same')

# 创建新的LAS文件并保存滤波后数据
out_las_weighted_ma = laspy.file.File(out_file_path_weighted_ma, mode='w', header=in_las.header)
out_las_weighted_ma.x = x
out_las_weighted_ma.y = y
out_las_weighted_ma.z = filtered_z_weighted_ma
out_las_weighted_ma.close()

4. Gaussian Filter:

Gaussian filtering smoothes data based on a Gaussian function. It can effectively remove noise while retaining details, and is suitable for smooth signals.

Code:

import laspy
import numpy as np
from scipy.ndimage import gaussian_filter1d
from skimage.metrics import structural_similarity as ssim

# 读取LAS文件
in_file_path = 'F:/激光雷达/武汉地调中心/1500N点云.las'
out_file_path_gaussian = 'F:/激光雷达/武汉地调中心/output_gaussian.las'

in_las = laspy.file.File(in_file_path, mode='r')

# 提取点云数据
x = in_las.x
y = in_las.y
z = in_las.z

# 应用高斯滤波
sigma = 1.0  # 高斯核标准差
filtered_z_gaussian = gaussian_filter1d(z, sigma=sigma)

# 创建新的LAS文件并保存滤波后数据
out_las_gaussian = laspy.file.File(out_file_path_gaussian, mode='w', header=in_las.header)
out_las_gaussian.x = x
out_las_gaussian.y = y
out_las_gaussian.z = filtered_z_gaussian
out_las_gaussian.close()

5. Waveform Removal Filter:

This filtering method is mainly used to remove ground signals from lidar echoes to better detect obstacles. This method requires modeling the ground and then subtracting the ground signal from the data.

Code:

import laspy
import numpy as np
from scipy.signal import detrend
from skimage.metrics import structural_similarity as ssim

# 读取LAS文件
in_file_path = 'F:/激光雷达/武汉地调中心/1500N点云.las'
out_file_path_waveform_removal = 'F:/激光雷达/武汉地调中心/output_waveform_removal.las'

in_las = laspy.file.File(in_file_path, mode='r')

# 提取点云数据
x = in_las.x
y = in_las.y
z = in_las.z

# 应用波形去除滤波
filtered_z_waveform_removal = detrend(z)

# 创建新的LAS文件并保存滤波后数据
out_las_waveform_removal = laspy.file.File(out_file_path_waveform_removal, mode='w', header=in_las.header)
out_las_waveform_removal.x = x
out_las_waveform_removal.y = y
out_las_waveform_removal.z = filtered_z_waveform_removal
out_las_waveform_removal.close()

6. Adaptive Filtering:

Adaptive filtering methods dynamically adjust filtering parameters based on local characteristics of data points. For example, adaptive median filtering adjusts the size of the filter window according to changes in the data distribution to balance noise removal and detail preservation.

Code:

import laspy
import numpy as np
from scipy.signal import wiener
from skimage.metrics import structural_similarity as ssim

# 读取LAS文件
in_file_path = 'F:/激光雷达/武汉地调中心/1500N点云.las'
out_file_path_adaptive = 'F:/激光雷达/武汉地调中心/output_adaptive.las'

in_las = laspy.file.File(in_file_path, mode='r')

# 提取点云数据
x = in_las.x
y = in_las.y
z = in_las.z

# 应用自适应滤波
try:
    filtered_z_adaptive = wiener(z)
except (ValueError, ZeroDivisionError):
    # 处理除以零或无效值的情况
    filtered_z_adaptive = z  # 可以选择保持原始数据,或者使用其他滤波方法来代替

# 创建新的LAS文件并保存滤波后数据
out_las_adaptive = laspy.file.File(out_file_path_adaptive, mode='w', header=in_las.header)
out_las_adaptive.x = x
out_las_adaptive.y = y
out_las_adaptive.z = filtered_z_adaptive
out_las_adaptive.close()

7. Wavelet Transform Filter:

Wavelet transform filtering can decompose the signal into sub-signals of different frequencies, then remove high-frequency noise as needed, and then reconstruct the signal back. This method is very useful when dealing with data containing multi-scale information.

Code:

import laspy
import numpy as np
import pywt
from skimage.metrics import structural_similarity as ssim

# 读取LAS文件
in_file_path = 'F:/激光雷达/武汉地调中心/1500N点云.las'
out_file_path_wavelet = 'F:/激光雷达/武汉地调中心/output_wavelet.las'

in_las = laspy.file.File(in_file_path, mode='r')

# 提取点云数据
x = in_las.x
y = in_las.y
z = in_las.z

# 应用小波变换滤波
wavelet_name = 'db4'  # 小波基函数的名称
level = 2  # 分解的级别
coeffs = pywt.wavedec(z, wavelet_name, level=level)
coeffs[1:] = [pywt.threshold(coeff, value=0.5, mode='soft') for coeff in coeffs[1:]]  # 对细节系数进行软阈值处理
filtered_z_wavelet = pywt.waverec(coeffs, wavelet_name)

# 创建新的LAS文件并保存滤波后数据
out_las_wavelet = laspy.file.File(out_file_path_wavelet, mode='w', header=in_las.header)
out_las_wavelet.x = x
out_las_wavelet.y = y
out_las_wavelet.z = filtered_z_wavelet
out_las_wavelet.close()

The comparison results show that the error indicators show that adaptive filtering (Adaptive Filtering) and wavelet transform filter (Wavelet Transform Filter) have better processing effects, with the latter being the best.

Guess you like

Origin blog.csdn.net/weixin_42464154/article/details/132287267