首页 > 其他 > 详细

OpenCV 霍夫线变换

时间:2020-01-09 13:28:25      阅读:72      评论:0      收藏:0      [点我收藏+]

 

 1 #include "opencv2/highgui/highgui.hpp"
 2 #include "opencv2/imgproc/imgproc.hpp"
 3 
 4 #include <iostream>
 5 
 6 using namespace cv;
 7 using namespace std;
 8 
 9 void help()
10 {
11  cout << "\nThis program demonstrates line finding with the Hough transform.\n"
12          "Usage:\n"
13          "./houghlines <image_name>, Default is pic1.jpg\n" << endl;
14 }
15 
16 int main(int argc, char** argv)
17 {
18  const char* filename = argc >= 2 ? argv[1] : "pic1.jpg";
19 
20  Mat src = imread(filename, 0);
21  if(src.empty())
22  {
23      help();
24      cout << "can not open " << filename << endl;
25      return -1;
26  }
27 
28  Mat dst, cdst;
29  Canny(src, dst, 50, 200, 3);
30  cvtColor(dst, cdst, CV_GRAY2BGR);
31 
32  #if 0
33   vector<Vec2f> lines;
34   HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 );
35 
36   for( size_t i = 0; i < lines.size(); i++ )
37   {
38      float rho = lines[i][0], theta = lines[i][1];
39      Point pt1, pt2;
40      double a = cos(theta), b = sin(theta);
41      double x0 = a*rho, y0 = b*rho;
42      pt1.x = cvRound(x0 + 1000*(-b));
43      pt1.y = cvRound(y0 + 1000*(a));
44      pt2.x = cvRound(x0 - 1000*(-b));
45      pt2.y = cvRound(y0 - 1000*(a));
46      line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
47   }
48  #else
49   vector<Vec4i> lines;
50   HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 );
51   for( size_t i = 0; i < lines.size(); i++ )
52   {
53     Vec4i l = lines[i];
54     line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, CV_AA);
55   }
56  #endif
57  imshow("source", src);
58  imshow("detected lines", cdst);
59 
60  waitKey();
61 
62  return 0;
63 }

OpenCV 霍夫线变换

原文:https://www.cnblogs.com/ybqjymy/p/12170933.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!