2 * @file houghclines.cpp
3 * @brief This program demonstrates line finding with the Hough transform
6 #include "opencv2/imgcodecs.hpp"
7 #include "opencv2/highgui.hpp"
8 #include "opencv2/imgproc.hpp"
13 int main(int argc, char** argv)
15 // Declare the output variables
19 const char* default_file = "../../../data/sudoku.png";
20 const char* filename = argc >=2 ? argv[1] : default_file;
23 Mat src = imread( filename, IMREAD_GRAYSCALE );
25 // Check if image is loaded fine
27 printf(" Error opening image\n");
28 printf(" Program Arguments: [image_name -- default %s] \n", default_file);
35 Canny(src, dst, 50, 200, 3);
38 // Copy edges to the images that will display the results in BGR
39 cvtColor(dst, cdst, COLOR_GRAY2BGR);
43 // Standard Hough Line Transform
44 vector<Vec2f> lines; // will hold the results of the detection
45 HoughLines(dst, lines, 1, CV_PI/180, 150, 0, 0 ); // runs the actual detection
49 for( size_t i = 0; i < lines.size(); i++ )
51 float rho = lines[i][0], theta = lines[i][1];
53 double a = cos(theta), b = sin(theta);
54 double x0 = a*rho, y0 = b*rho;
55 pt1.x = cvRound(x0 + 1000*(-b));
56 pt1.y = cvRound(y0 + 1000*(a));
57 pt2.x = cvRound(x0 - 1000*(-b));
58 pt2.y = cvRound(y0 - 1000*(a));
59 line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA);
64 // Probabilistic Line Transform
65 vector<Vec4i> linesP; // will hold the results of the detection
66 HoughLinesP(dst, linesP, 1, CV_PI/180, 50, 50, 10 ); // runs the actual detection
70 for( size_t i = 0; i < linesP.size(); i++ )
73 line( cdstP, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, LINE_AA);
79 imshow("Source", src);
80 imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst);
81 imshow("Detected Lines (in red) - Probabilistic Line Transform", cdstP);