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 << " This program demonstrates line finding with the Hough transform. " 12 "Usage: " 13 "./houghlines <image_name>, Default is pic1.jpg " << 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 }