Tutorial Hough Circles
[platform/upstream/opencv.git] / samples / cpp / houghlines.cpp
1 #include "opencv2/imgcodecs.hpp"
2 #include "opencv2/highgui.hpp"
3 #include "opencv2/imgproc.hpp"
4
5 #include <iostream>
6
7 using namespace cv;
8 using namespace std;
9
10 static void help()
11 {
12     cout << "\nThis program demonstrates line finding with the Hough transform.\n"
13             "Usage:\n"
14             "./houghlines <image_name>, Default is ../data/pic1.png\n" << endl;
15 }
16
17 int main(int argc, char** argv)
18 {
19     cv::CommandLineParser parser(argc, argv,
20         "{help h||}{@image|../data/pic1.png|}"
21     );
22     if (parser.has("help"))
23     {
24         help();
25         return 0;
26     }
27     string filename = parser.get<string>("@image");
28     if (filename.empty())
29     {
30         help();
31         cout << "no image_name provided" << endl;
32         return -1;
33     }
34     Mat src = imread(filename, 0);
35     if(src.empty())
36     {
37         help();
38         cout << "can not open " << filename << endl;
39         return -1;
40     }
41
42     Mat dst, cdst;
43     Canny(src, dst, 50, 200, 3);
44     cvtColor(dst, cdst, COLOR_GRAY2BGR);
45
46 #if 0
47     vector<Vec2f> lines;
48     HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 );
49
50     for( size_t i = 0; i < lines.size(); i++ )
51     {
52         float rho = lines[i][0], theta = lines[i][1];
53         Point pt1, pt2;
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);
61     }
62 #else
63     vector<Vec4i> lines;
64     HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 );
65     for( size_t i = 0; i < lines.size(); i++ )
66     {
67         Vec4i l = lines[i];
68         line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, LINE_AA);
69     }
70 #endif
71     imshow("source", src);
72     imshow("detected lines", cdst);
73
74     waitKey();
75
76     return 0;
77 }