1 #include "opencv2/imgcodecs.hpp" 2 #include "opencv2/highgui/highgui.hpp" 3 #include "opencv2/imgproc/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 const char* filename = argc >= 2 ? argv[1] : "../data/pic1.png"; 20 21 Mat src = imread(filename, 0); 22 if(src.empty()) 23 { 24 help(); 25 cout << "can not open " << filename << endl; 26 return -1; 27 } 28 29 Mat dst, cdst; 30 Canny(src, dst, 50, 200, 3); 31 cvtColor(dst, cdst, COLOR_GRAY2BGR); 32 33 #if 0 34 vector<Vec2f> lines; 35 HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 ); 36 37 for( size_t i = 0; i < lines.size(); i++ ) 38 { 39 float rho = lines[i][0], theta = lines[i][1]; 40 Point pt1, pt2; 41 double a = cos(theta), b = sin(theta); 42 double x0 = a*rho, y0 = b*rho; 43 pt1.x = cvRound(x0 + 1000*(-b)); 44 pt1.y = cvRound(y0 + 1000*(a)); 45 pt2.x = cvRound(x0 - 1000*(-b)); 46 pt2.y = cvRound(y0 - 1000*(a)); 47 line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA); 48 } 49 #else 50 vector<Vec4i> lines; 51 HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 ); 52 for( size_t i = 0; i < lines.size(); i++ ) 53 { 54 Vec4i l = lines[i]; 55 line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, LINE_AA); 56 } 57 #endif 58 imshow("source", src); 59 imshow("detected lines", cdst); 60 61 waitKey(); 62 63 return 0; 64 } 65