OpenCV 图形API(66)图像结构分析和形状描述符------将一条直线拟合到三维点集上函数fitLine3D()

  • 操作系统:ubuntu22.04
  • OpenCV版本:OpenCV4.9
  • IDE:Visual Studio Code
  • 编程语言:C++11

算法描述

拟合一条直线到3D点集。

该函数通过最小化 ∑iρ(ri) 来将一条直线拟合到3D点集,其中 ri 是第 i 个点与直线之间的距离,ρ® 是距离函数,可以是以下之一:

  • DIST_L2

    ρ ( r ) = r 2 / 2 (最简单且最快的最小二乘法) \rho (r) = r^2/2 \quad \text{(最简单且最快的最小二乘法)} ρ(r)=r2/2(最简单且最快的最小二乘法)

  • DIST_L1

    ρ ( r ) = r \rho (r) = r ρ(r)=r

  • DIST_L12

ρ ( r ) = 2 ⋅ ( 1 + r 2 2 − 1 ) \rho (r) = 2 \cdot ( \sqrt{1 + \frac{r^2}{2}} - 1) ρ(r)=2(1+2r2 1)

  • DIST_FAIR

    ρ ( r ) = C 2 ⋅ ( r C − log ⁡ ( 1 + r C ) ) where C = 1.3998 \rho \left (r \right ) = C^2 \cdot \left ( \frac{r}{C} - \log{\left(1 + \frac{r}{C}\right)} \right ) \quad \text{where} \quad C=1.3998 ρ(r)=C2(Crlog(1+Cr))whereC=1.3998

  • DIST_WELSCH

    ρ ( r ) = C 2 2 ⋅ ( 1 − exp ⁡ ( − ( r C ) 2 ) ) where C = 2.9846 \rho \left (r \right ) = \frac{C^2}{2} \cdot \left ( 1 - \exp{\left(-\left(\frac{r}{C}\right)^2\right)} \right ) \quad \text{where} \quad C=2.9846 ρ(r)=2C2(1exp((Cr)2))whereC=2.9846

  • DIST_HUBER

ρ ( r ) = { r 2 / 2 if  r < C C ⋅ ( r − C / 2 ) otherwise \rho(r) = \begin{cases} r^2/2 & \text{if } r < C \\ C \cdot (r - C/2) & \text{otherwise} \end{cases} ρ(r)={r2/2C(rC/2)if r<Cotherwise

该算法基于M估计器(http://en.wikipedia.org/wiki/M-estimator)技术,使用加权最小二乘法迭代地拟合直线。每次迭代后,权重 wi 被调整为与 ρ(ri) 成反比。

注意:

函数文本ID为 “org.opencv.imgproc.shape.fitLine3DMat”
在给定N维点集的情况下,Mat应该是二维的,如果有N个通道,则应有单行或单列;如果只有单个通道,则应有N列。

函数原型

GOpaque<Vec6f> cv::gapi::fitLine3D 	
(
 	const GMat &  	src,
	const DistanceTypes  	distType,
	const double  	param = 0.,
	const double  	reps = 0.,
	const double  	aeps = 0. 
) 		

参数

  • 参数 src 输入3D点集存储在下列容器之一:Mat, std::vectorcv::Point3i, std::vectorcv::Point3f, std::vectorcv::Point3d。
  • 参数 distType M估计器使用的距离,参见DistanceTypes。DIST_USER和DIST_C不被支持。
  • 参数 param 某些类型距离的数值参数©。如果是0,则选择最优值。
  • 参数 reps 对于半径(坐标原点与直线之间的距离)的足够精度。1.0是一个好的默认值用于reps。如果是0,则选择默认值。
  • 参数 aeps 对于角度的足够精度。0.01是一个好的默认值用于aeps。如果是0,则选择默认值。

返回值:

输出直线参数:一个包含6个元素的向量(如Vec6f)- (vx, vy, vz, x0, y0, z0),其中(vx, vy, vz)是平行于直线的归一化向量,(x0, y0, z0)是直线上的一个点。

代码示例

#include <iostream>
#include <opencv2/gapi.hpp>
#include <opencv2/gapi/core.hpp>
#include <opencv2/gapi/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>  // 确保包含这个头文件以获取RNG定义

using namespace cv;
using namespace cv::gapi;

int main()
{
    // 假设我们有一个包含三维点的容器,这里用vector<Point3f>表示
    std::vector< Point3f > points3D = {
        { 1.f, 2.f, 3.f }, { 4.f, 5.f, 6.f }, { 7.f, 8.f, 9.f },
        // 添加更多点...
    };

    // 将三维点转换为适合G-API的格式
    Mat pointsMat( points3D );

    // 创建G-API网络
    cv::GMat in;
    auto lineParams = gapi::fitLine3D( in, DIST_L2, 0., 0.01, 0.01 );

    // 定义输出变量
    Vec6f fittedLine;

    // 运行G-API计算图
    cv::GComputation cc( GIn( in ), GOut( lineParams ) );
    cc.apply( cv::gin( pointsMat ), cv::gout( fittedLine ), compile_args( gapi::kernels<>() ) );

    // 打印拟合的直线参数
    std::cout << "Fitted Line Parameters: " << fittedLine << std::endl;

    // 输出结果解释:
    // fittedLine[0], fittedLine[1], fittedLine[2] 表示直线的方向向量 (vx, vy, vz)
    // fittedLine[3], fittedLine[4], fittedLine[5] 表示直线上的一点 (x0, y0, z0)

    return 0;
}

运行结果

Fitted Line Parameters: [0.57735, 0.57735, 0.57735, 4, 5, 6]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

村北头的码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值