哪个网站可以做店招,wordpress建表,凡网站创建,网站地图怎么做操作系统#xff1a;ubuntu22.04 OpenCV版本#xff1a;OpenCV4.9 IDE:Visual Studio Code 编程语言#xff1a;C11
算法描述
拟合一条直线到2D或3D点集。
fitLine 函数通过最小化 ∑ i ρ ( r i ) \sum_i \rho(r_i) ∑iρ(ri)来拟合一条直线到2D或3D点集#xff0c… 操作系统ubuntu22.04 OpenCV版本OpenCV4.9 IDE:Visual Studio Code 编程语言C11
算法描述
拟合一条直线到2D或3D点集。
fitLine 函数通过最小化 ∑ i ρ ( r i ) \sum_i \rho(r_i) ∑iρ(ri)来拟合一条直线到2D或3D点集其中 r i r_i ri 是第i 个点到直线的距离而 ρ ( r ) \rho(r) ρ(r)是一个距离函数可以是以下之一 DIST_L2 ρ ( r ) r 2 / 2 (the simplest and the fastest least-squares method) \rho (r) r^2/2 \quad \text{(the simplest and the fastest least-squares method)} ρ(r)r2/2(the simplest and the fastest least-squares method) 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⋅(12r2 −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 C1.3998 ρ(r)C2⋅(Cr−log(1Cr))whereC1.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 C2.9846 ρ(r)2C2⋅(1−exp(−(Cr)2))whereC2.9846 DIST_HUBER ρ ( r ) { r 2 2 if r C C ⋅ ( r − C 2 ) otherwise 此处 C 1.345 \rho(r) \begin{cases} \frac{r^2}{2} \text{if } r C \\ C \cdot (r - \frac{C}{2}) \text{otherwise} \end{cases} 此处C1.345 ρ(r){2r2C⋅(r−2C)if rCotherwise此处C1.345
该算法基于 M-估计器技术http://en.wikipedia.org/wiki/M-estimator该技术迭代地使用加权最小二乘法来拟合直线。在每次迭代之后权重 w i w_i wi被调整为与 ρ ( r i ) \rho(r_i) ρ(ri)成反比。
函数原型 void cv::fitLine
(InputArray points,OutputArray line,int distType,double param,double reps,double aeps
)
参数
参数points 输入的2D或3D点集存储在 std::vector 或 Mat 中。参数输出的直线参数。在2D拟合的情况下它应该是一个包含4个元素的向量如 Vec4f—— (vx, vy, x0, y0)其中 (vx, vy) 是与直线共线的归一化向量(x0, y0) 是直线上的一点。在3D拟合的情况下它应该是一个包含6个元素的向量如 Vec6f—— (vx, vy, vz, x0, y0, z0)其中 (vx, vy, vz) 是与直线共线的归一化向量(x0, y0, z0) 是直线上的一点。参数distType 由 M-估计器使用的距离类型参见 DistanceTypes。参数param 对某些类型的距离来说的数值参数C。如果它是0则会选择一个最优值。参数reps 对半径坐标原点与直线之间的距离的充分精度。参数aeps 对角度的充分精度。对于 reps 和 aeps0.01 是一个好的默认值。
代码示例
#include iostream
#include opencv2/opencv.hppusing namespace std;
using namespace cv;int main()
{// 创建一个空白图像Mat img( 400, 400, CV_8UC3, Scalar( 255, 255, 255 ) );// 创建一组2D点vector Point2f points;points.push_back( Point2f( 100, 100 ) );points.push_back( Point2f( 200, 100 ) );points.push_back( Point2f( 200, 200 ) );points.push_back( Point2f( 100, 200 ) );points.push_back( Point2f( 150, 150 ) );points.push_back( Point2f( 150, 250 ) );points.push_back( Point2f( 250, 150 ) );points.push_back( Point2f( 250, 250 ) );// 定义输出直线Vec4f line;// 拟合直线fitLine( points, line, DIST_L2, 0, 0.01, 0.01 );// 获取直线参数float vx line[ 0 ];float vy line[ 1 ];float x0 line[ 2 ];float y0 line[ 3 ];// 计算两点来绘制直线Point p1( x0 - 100 * vx, y0 - 100 * vy );Point p2( x0 100 * vx, y0 100 * vy );// 在原图上绘制直线cv::line( img, p1, p2, Scalar( 0, 0, 255 ), 2, LINE_8 );// 绘制点集for ( const auto pt : points ){circle( img, pt, 5, Scalar( 0, 255, 0 ), -1 );}// 显示结果imshow( Line Fitting, img );waitKey( 0 );return 0;
}运行结果