(原)opencv直线拟合fitLine

转载请注明出处

http://www.cnblogs.com/darkknightzh/p/5486234.html

参考网址:

http://blog.csdn.net/thefutureisour/article/details/7599537

 1 Mat img(60, 60, CV_8UC1, Scalar(0));
 2     std::vector<Point2f> points;
 3     points.push_back(Point2f(10.5, 10.2));
 4     points.push_back(Point2f(20.6, 20.7));
 5     points.push_back(Point2f(33.5, 30.2));
 6     points.push_back(Point2f(40.7, 50));
 7 
 8     //储存拟合直线的容器  
 9     Vec4f line;
10     //直线拟合函数  
11     fitLine(Mat(points), line, CV_DIST_L1, 0, 0.01, 0.01);
12     std::cout << "line: (" << line[0] << "," << line[1] << ")(" << line[2] << "," << line[3] << ")\n";
13 
14     for (auto i = 0; i < points.size(); i++)
15         cv::circle(img, cvPoint(points.at(i).x, points.at(i).y), 2, Scalar(255));
16 
17     double k = line[1] / line[0];
18     double step = 40;
19     cv::line(img, cvPoint(line[2] - step, line[3] - k*step), cvPoint(line[2] + step, k*step + line[3]), Scalar(255));
20 
21     imshow("img", img);
22     waitKey(0);

拟合的结果Vec4f类型的line中的前两个值 给出的是直线的方向的单位向量,后两个值给出的是该直线通过的一个点(转自参考网址)。

posted on 2016-05-12 16:36  darkknightzh  阅读(11684)  评论(0编辑  收藏  举报

导航