1 #include "opencv2/imgcodecs.hpp"
2 #include "opencv2/highgui.hpp"
3 #include "opencv2/imgproc.hpp"
12 cout << "\nThis program demonstrates line finding with the Hough transform.\n"
14 "./houghlines <image_name>, Default is ../data/pic1.png\n" << endl;
17 int main(int argc, char** argv)
19 cv::CommandLineParser parser(argc, argv,
20 "{help h||}{@image|../data/pic1.png|}"
22 if (parser.has("help"))
27 string filename = parser.get<string>("@image");
31 cout << "no image_name provided" << endl;
34 Mat src = imread(filename, 0);
38 cout << "can not open " << filename << endl;
43 Canny(src, dst, 50, 200, 3);
44 cvtColor(dst, cdst, COLOR_GRAY2BGR);
48 HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 );
50 for( size_t i = 0; i < lines.size(); i++ )
52 float rho = lines[i][0], theta = lines[i][1];
54 double a = cos(theta), b = sin(theta);
55 double x0 = a*rho, y0 = b*rho;
56 pt1.x = cvRound(x0 + 1000*(-b));
57 pt1.y = cvRound(y0 + 1000*(a));
58 pt2.x = cvRound(x0 - 1000*(-b));
59 pt2.y = cvRound(y0 - 1000*(a));
60 line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
64 HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 );
65 for( size_t i = 0; i < lines.size(); i++ )
68 line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, LINE_AA);
71 imshow("source", src);
72 imshow("detected lines", cdst);