opencv-fitLine

opencv-fitLine§fitLine()voidcv::fitLine ( InputArray points, OutputArray line, int distType, double param, double reps, doub…

§ fitLine()

void cv::fitLine ( InputArray  points,
    OutputArray  line,
    int  distType,
    double  param,
    double  reps,
    double  aeps 
  )    
Python:
  line = cv.fitLine( points, distType, param, reps, aeps[, line] )

#include <opencv2/imgproc.hpp>

Fits a line to a 2D or 3D point set.

The function fitLine fits a line to a 2D or 3D point set by minimizing ∑iρ(ri) where ri is a distance between the ith point, the line and ρ(r) is a distance function, one of the following:

  • DIST_L2

    ρ(r)=r2/2(the simplest and the fastest least-squares method)

  • DIST_L1

    ρ(r)=r

  • DIST_L12

    ρ(r)=2⋅(1+r22−−−−−−√−1)

  • DIST_FAIR

    ρ(r)=C2⋅(rC−log(1+rC))whereC=1.3998

  • DIST_WELSCH

    ρ(r)=C22⋅(1−exp(−(rC)2))whereC=2.9846

  • DIST_HUBER

    ρ(r)={r2/2C⋅(r−C/2)if r<CotherwisewhereC=1.345

The algorithm is based on the M-estimator ( http://en.wikipedia.org/wiki/M-estimator ) technique that iteratively fits the line using the weighted least-squares algorithm. After each iteration the weights wi are adjusted to be inversely proportional to ρ(ri) .

Parameters

points Input vector of 2D or 3D points, stored in std::vector<> or Mat.
line Output line parameters. In case of 2D fitting, it should be a vector of 4 elements (like Vec4f) – (vx, vy, x0, y0), where (vx, vy) is a normalized vector collinear to the line and (x0, y0) is a point on the line. In case of 3D fitting, it should be a vector of 6 elements (like Vec6f) – (vx, vy, vz, x0, y0, z0), where (vx, vy, vz) is a normalized vector collinear to the line and (x0, y0, z0) is a point on the line.
distType Distance used by the M-estimator, see DistanceTypes
param Numerical parameter ( C ) for some types of distances. If it is 0, an optimal value is chosen.
reps Sufficient accuracy for the radius (distance between the coordinate origin and the line).
aeps

Sufficient accuracy for the angle. 0.01 would be a good default value for reps and aeps.

The example below:  


	Mat img = Mat::zeros(1000,1000,CV_8UC3);
	vector<Point> point_set;
	Point P1(100, 100), P2(100, 400), P3(150, 300), P4(200, 200), \
		P5(300, 300), P6(300, 800), P7(700, 100),P8(750,750);
	point_set.push_back(P1);
	point_set.push_back(P2);
	point_set.push_back(P3);
	point_set.push_back(P4);
	point_set.push_back(P5);
	point_set.push_back(P6);
	point_set.push_back(P7);
	point_set.push_back(P8);



	//将拟合点绘制到空白图上
	for (int i = 0; i < point_set.size(); i++)
	{
		cv::circle(img, point_set[i], 5, cv::Scalar(0, 255, 0), 2, 8, 0);
	}

	Vec4f fitline;
	fitLine(point_set, fitline, DIST_L2, 0, 0.01, 0.01);

	// 求出直线上的两个点
	double k_line = fitline[1] / fitline[0];

	//计算直线的端点: y = k(x - x0) + y0 
	//其中 fitline[2],fitline[3]为点斜式的点
	Point p1(0, k_line*(0 - fitline[2]) + fitline[3]);
	Point p2(img.rows - 1, k_line*(img.rows - 1 - fitline[2]) + fitline[3]);

	//显示拟合出的直线方程
	char text_equation[1024];
	sprintf(text_equation, "y-%.2f=%.2f(x-%.2f)", fitline[3], k_line, fitline[2]);

	putText(img, text_equation, Point(img.rows/2, img.cols/2),\
		FONT_HERSHEY_COMPLEX, 1, Scalar(0, 0, 255), 2, 8);
	//显示拟合出的直线
	line(img, p1, p2, Scalar(0, 0, 255), 2);
	imshow("原图+拟合结果", img);

	waitKey();

 

今天的文章opencv-fitLine分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/4719.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注