OpenCV 图形API(65)图像结构分析和形状描述符------拟合二维点集的直线函数 fitLine2D()
- 操作系统:ubuntu22.04
- OpenCV版本:OpenCV4.9
- IDE:Visual Studio Code
- 编程语言:C++11
算法描述
拟合一条直线到2D点集。
该函数通过最小化 ∑ i ρ ( r i ) \sum_i \rho(r_i) ∑iρ(ri)来将一条直线拟合到2D点集,其中 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⋅(Cr−log(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⋅(1−exp(−(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⋅(r−C/2)if r<Cotherwise
该算法基于M估计器(http://en.wikipedia.org/wiki/M-estimator)技术,使用加权最小二乘法迭代地拟合直线。每次迭代后,权重 wi 被调整为与 ρ(ri) 成反比。
注意:
函数文本ID为 “org.opencv.imgproc.shape.fitLine2DMat”
在给定N维点集的情况下,Mat应该是二维的,如果有N个通道,则应有单行或单列;如果只有单个通道,则应有N列。
函数原型
GOpaque<Vec4f> cv::gapi::fitLine2D
(const GMat & src,const DistanceTypes distType,const double param = 0.,const double reps = 0.,const double aeps = 0.
)
参数
- 参数 src 输入2D点集存储在下列容器之一:Mat, std::vectorcv::Point2i, std::vectorcv::Point2f, std::vectorcv::Point2d。
- 参数 distType M估计器使用的距离,参见DistanceTypes。DIST_USER和DIST_C不被支持。
- 参数 param 某些类型距离的数值参数©。如果是0,则选择最优值。
- 参数 reps 对于半径(坐标原点与直线之间的距离)的足够精度。1.0是一个好的默认值用于reps。如果是0,则选择默认值。
- 参数 aeps 对于角度的足够精度。0.01是一个好的默认值用于aeps。如果是0,则选择默认值。
返回值
输出直线参数:一个包含4个元素的向量(如Vec4f)- (vx, vy, x0, y0),其中(vx, vy)是平行于直线的归一化向量,(x0, y0)是直线上的一个点。
代码示例
#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()
{// 声明并初始化随机数生成器RNG rng( 0xFFFFFFFF );// 读取输入图像并转换为灰度图Mat src = imread( "/media/dingxin/data/study/OpenCV/sources/images/Lenna.png", IMREAD_GRAYSCALE );if ( src.empty() ){std::cerr << "无法读取图像" << std::endl;return -1;}// 对图像进行阈值处理以生成二值图像Mat binary;threshold( src, binary, 128, 255, THRESH_BINARY );// 查找轮廓std::vector< std::vector< Point > > contours;std::vector< Vec4i > hierarchy;findContours( binary, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE );if ( !contours.empty() ){// 使用第一个轮廓进行直线拟合auto contour = contours[ 0 ];// 创建G-API网络cv::GMat in;auto lineParams = gapi::fitLine2D( in, DIST_L2, 0, 0.01, 0.01 );// 将轮廓转换为GMat格式(这里需要将点列表转换为适合G-API的格式)Mat contourMat( contour ); // 注意:这可能需要额外处理以确保数据正确传递// 定义输出变量Vec4f fittedLine;// 运行G-API计算图cv::GComputation cc( GIn( in ), GOut( lineParams ) );cc.apply( cv::gin( contourMat ), cv::gout( fittedLine ), compile_args( gapi::kernels<>() ) );// 打印拟合的直线参数std::cout << "Fitted Line Parameters: " << fittedLine << std::endl;// 绘制原始轮廓和拟合的直线Mat dst;cvtColor( binary, dst, COLOR_GRAY2BGR ); // 转换为彩色图像以便于绘制颜色轮廓drawContours( dst, contours, 0, Scalar( 255, 0, 0 ), 2 ); // 绘制轮廓// 根据拟合直线参数绘制直线Point point1 = Point( 0, static_cast< int >( fittedLine[ 1 ] * 0 + fittedLine[ 3 ] ) );Point point2 = Point( static_cast< int >( binary.cols ), static_cast< int >( fittedLine[ 1 ] * binary.cols + fittedLine[ 3 ] ) );line( dst, point1, point2, Scalar( 0, 255, 0 ), 2 );// 显示结果imshow( "Contours and Fitted Line", dst );waitKey();}else{std::cerr << "未找到任何轮廓" << std::endl;}return 0;
}
运行结果
Fitted Line Parameters: [0.321031, 0.947069, 138.5, 508.667]