PageRenderTime 63ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 1ms

/examples/opencv/src/houghlines.c

https://gitlab.com/juan-cardelino/misc_projects
C | 62 lines | 50 code | 10 blank | 2 comment | 3 complexity | 24ece75e874bcdb063bfba22858b6b17 MD5 | raw file
  1. #define CV_NO_BACKWARD_COMPATIBILITY
  2. /* This is a standalone program. Pass an image name as a first parameter of the program.
  3. Switch between standard and probabilistic Hough transform by changing "#if 1" to "#if 0" and back */
  4. #include <cv.h>
  5. #include <highgui.h>
  6. #include <math.h>
  7. int main(int argc, char** argv)
  8. {
  9. const char* filename = argc >= 2 ? argv[1] : "pic1.png";
  10. IplImage* src = cvLoadImage( filename, 0 );
  11. IplImage* dst;
  12. IplImage* color_dst;
  13. CvMemStorage* storage = cvCreateMemStorage(0);
  14. CvSeq* lines = 0;
  15. int i;
  16. if( !src )
  17. return -1;
  18. dst = cvCreateImage( cvGetSize(src), 8, 1 );
  19. color_dst = cvCreateImage( cvGetSize(src), 8, 3 );
  20. cvCanny( src, dst, 50, 200, 3 );
  21. cvCvtColor( dst, color_dst, CV_GRAY2BGR );
  22. #if 0
  23. lines = cvHoughLines2( dst, storage, CV_HOUGH_STANDARD, 1, CV_PI/180, 100, 0, 0 );
  24. for( i = 0; i < MIN(lines->total,100); i++ )
  25. {
  26. float* line = (float*)cvGetSeqElem(lines,i);
  27. float rho = line[0];
  28. float theta = line[1];
  29. CvPoint pt1, pt2;
  30. double a = cos(theta), b = sin(theta);
  31. double x0 = a*rho, y0 = b*rho;
  32. pt1.x = cvRound(x0 + 1000*(-b));
  33. pt1.y = cvRound(y0 + 1000*(a));
  34. pt2.x = cvRound(x0 - 1000*(-b));
  35. pt2.y = cvRound(y0 - 1000*(a));
  36. cvLine( color_dst, pt1, pt2, CV_RGB(255,0,0), 3, CV_AA, 0 );
  37. }
  38. #else
  39. lines = cvHoughLines2( dst, storage, CV_HOUGH_PROBABILISTIC, 1, CV_PI/180, 50, 50, 10 );
  40. for( i = 0; i < lines->total; i++ )
  41. {
  42. CvPoint* line = (CvPoint*)cvGetSeqElem(lines,i);
  43. cvLine( color_dst, line[0], line[1], CV_RGB(255,0,0), 3, CV_AA, 0 );
  44. }
  45. #endif
  46. cvNamedWindow( "Source", 1 );
  47. cvShowImage( "Source", src );
  48. cvNamedWindow( "Hough", 1 );
  49. cvShowImage( "Hough", color_dst );
  50. cvWaitKey(0);
  51. return 0;
  52. }