Open3D 非线性最小二乘法拟合二维直线

目录

一、概述

1.1原理

1.2实现步骤

二、代码实现

2.1关键函数

2.1.1残差函数

2.1.2拟合直线

2.2完整代码

三、实现效果

3.1拟合后点云

3.2结果数据


前期试读,后续会将博客加入下列链接的专栏,欢迎订阅

Open3D点云算法与点云深度学习案例汇总(长期更新)-CSDN博客

一、概述

        使用非线性最小二乘法拟合二维直线,可以通过优化算法来最小化点云数据与直线模型之间的误差。

1.1原理

1.2实现步骤

1.生成或读取点云数据:
        使用 Open3D 生成或读取二维平面上的点云数据。
2.定义目标函数:
        定义用于非线性最小二乘法优化的目标函数。
3.初始参数估计:
        选择初始的斜率和截距估计值。
4.优化求解:
        使用 scipy.optimize.least_squares 或其他优化算法进行非线性最小二乘法优化,拟合直线。
5.可视化结果:
        使用 Open3D 可视化原始点云数据和拟合的直线。

二、代码实现

2.1关键函数

2.1.1残差函数

使用 residuals 函数计算直线拟合的残差。残差是每个点的 y 坐标与直线方程的差值。

def residuals(params, points):
    """
    计算残差,用于非线性最小二乘法优化。

    参数:
    params (numpy.ndarray): 直线的参数 (m, b)。
    points (numpy.ndarray): 点云数据,形状为 (N, 2)。

    返回:
    numpy.ndarray: 残差,形状为 (N,)。
    """
    m, b = params
    residuals = points[:, 1] - (m * points[:, 0] + b)
    return residuals

2.1.2拟合直线

        使用 fit_line_least_squares 函数进行非线性最小二乘法优化,拟合直线。初始参数估计使用点云数据的斜率和截距。

def fit_line_least_squares(points):
    """
    使用非线性最小二乘法拟合直线。

    参数:
    points (numpy.ndarray): 点云数据,形状为 (N, 2)。

    返回:
    tuple: 拟合的直线的参数 (m, b)。
    """
    # 初始参数估计
    m_estimate = (points[-1, 1] - points[0, 1]) / (points[-1, 0] - points[0, 0])
    b_estimate = points[0, 1] - m_estimate * points[0, 0]
    initial_params = [m_estimate, b_estimate]

    # 非线性最小二乘法优化
    result = least_squares(residuals, initial_params, args=(points,))
    return result.x

2.2完整代码


import open3d as o3d
import numpy as np
from scipy.optimize import least_squares

def generate_noisy_line(slope, intercept, num_points=1000, noise_level=0.05):
    """
    生成带有噪声的二维直线点云数据。

    参数:
    slope (float): 直线的斜率。
    intercept (float): 直线的截距。
    num_points (int): 点的数量。
    noise_level (float): 噪声水平。

    返回:
    numpy.ndarray: 生成的点云数据。
    """
    x = np.linspace(-10, 10, num_points)
    y = slope * x + intercept
    points = np.vstack((x, y)).T

    noise = np.random.normal(0, noise_level, points.shape)
    noisy_points = points + noise

    return noisy_points

def residuals(params, points):
    """
    计算残差,用于非线性最小二乘法优化。

    参数:
    params (numpy.ndarray): 直线的参数 (m, b)。
    points (numpy.ndarray): 点云数据,形状为 (N, 2)。

    返回:
    numpy.ndarray: 残差,形状为 (N,)。
    """
    m, b = params
    residuals = points[:, 1] - (m * points[:, 0] + b)
    return residuals

def fit_line_least_squares(points):
    """
    使用非线性最小二乘法拟合直线。

    参数:
    points (numpy.ndarray): 点云数据,形状为 (N, 2)。

    返回:
    tuple: 拟合的直线的参数 (m, b)。
    """
    # 初始参数估计
    m_estimate = (points[-1, 1] - points[0, 1]) / (points[-1, 0] - points[0, 0])
    b_estimate = points[0, 1] - m_estimate * points[0, 0]
    initial_params = [m_estimate, b_estimate]

    # 非线性最小二乘法优化
    result = least_squares(residuals, initial_params, args=(points,))
    return result.x

def create_line_mesh(m, b, x_range, resolution=100):
    """
    创建一个直线的 Mesh,用于可视化。

    参数:
    m (float): 直线的斜率。
    b (float): 直线的截距。
    x_range (tuple): x 的范围。
    resolution (int): 直线的分辨率。

    返回:
    open3d.geometry.LineSet: 直线的 Mesh 对象。
    """
    x = np.linspace(x_range[0], x_range[1], resolution)
    y = m * x + b
    line_points = np.vstack((x, y)).T

    # 创建线框用于可视化直线
    lines = [[i, i + 1] for i in range(resolution - 1)]
    line_set = o3d.geometry.LineSet()
    line_set.points = o3d.utility.Vector3dVector(np.c_[line_points, np.zeros(resolution)])
    line_set.lines = o3d.utility.Vector2iVector(lines)
    line_set.colors = o3d.utility.Vector3dVector([[1, 0, 0]] * len(lines))  # 红色

    return line_set

# 生成带有噪声的二维直线点云数据
slope = 2.0
intercept = 1.0
num_points = 100
noise_level = 0.2
points = generate_noisy_line(slope, intercept, num_points, noise_level)

# 使用非线性最小二乘法拟合直线
fitted_params = fit_line_least_squares(points)
fitted_slope = fitted_params[0]
fitted_intercept = fitted_params[1]
print(f"拟合的斜率: {fitted_slope},拟合的截距: {fitted_intercept}")

# 创建点云对象
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(np.c_[points, np.zeros(points.shape[0])])

# 创建拟合的直线的 Mesh 对象
x_range = (-10, 10)
line_mesh = create_line_mesh(fitted_slope, fitted_intercept, x_range)

# 可视化点云和拟合的直线
o3d.visualization.draw_geometries([pcd, line_mesh], window_name="Nonlinear Least Squares Line Fitting",
                                  width=800, height=600, left=50, top=50)

三、实现效果

3.1拟合后点云

3.2结果数据

拟合的斜率: 1.9940143480810066,拟合的截距: 1.0658299517098777

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值