PageRenderTime 371ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/opencv/src/fitellipse.cpp

https://gitlab.com/juan-cardelino/misc_projects
C++ | 134 lines | 68 code | 30 blank | 36 comment | 5 complexity | f55dbc9e630863026a947d2567a75d19 MD5 | raw file
  1. /********************************************************************************
  2. *
  3. *
  4. * This program is demonstration for ellipse fitting. Program finds
  5. * contours and approximate it by ellipses.
  6. *
  7. * Trackbar specify threshold parametr.
  8. *
  9. * White lines is contours. Red lines is fitting ellipses.
  10. *
  11. *
  12. * Autor: Denis Burenkov.
  13. *
  14. *
  15. *
  16. ********************************************************************************/
  17. #ifdef _CH_
  18. #pragma package <opencv>
  19. #endif
  20. #define CV_NO_BACKWARD_COMPATIBILITY
  21. #ifndef _EiC
  22. #include "cv.h"
  23. #include "highgui.h"
  24. #endif
  25. int slider_pos = 70;
  26. // Load the source image. HighGUI use.
  27. IplImage *image02 = 0, *image03 = 0, *image04 = 0;
  28. void process_image(int h);
  29. int main( int argc, char** argv )
  30. {
  31. const char* filename = argc == 2 ? argv[1] : (char*)"stuff.jpg";
  32. // load image and force it to be grayscale
  33. if( (image03 = cvLoadImage(filename, 0)) == 0 )
  34. return -1;
  35. // Create the destination images
  36. image02 = cvCloneImage( image03 );
  37. image04 = cvCloneImage( image03 );
  38. // Create windows.
  39. cvNamedWindow("Source", 1);
  40. cvNamedWindow("Result", 1);
  41. // Show the image.
  42. cvShowImage("Source", image03);
  43. // Create toolbars. HighGUI use.
  44. cvCreateTrackbar( "Threshold", "Result", &slider_pos, 255, process_image );
  45. process_image(0);
  46. // Wait for a key stroke; the same function arranges events processing
  47. cvWaitKey(0);
  48. cvReleaseImage(&image02);
  49. cvReleaseImage(&image03);
  50. cvDestroyWindow("Source");
  51. cvDestroyWindow("Result");
  52. return 0;
  53. }
  54. // Define trackbar callback functon. This function find contours,
  55. // draw it and approximate it by ellipses.
  56. void process_image(int h)
  57. {
  58. CvMemStorage* storage;
  59. CvSeq* contour;
  60. // Create dynamic structure and sequence.
  61. storage = cvCreateMemStorage(0);
  62. contour = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , storage);
  63. // Threshold the source image. This needful for cvFindContours().
  64. cvThreshold( image03, image02, slider_pos, 255, CV_THRESH_BINARY );
  65. // Find all contours.
  66. cvFindContours( image02, storage, &contour, sizeof(CvContour),
  67. CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
  68. // Clear images. IPL use.
  69. cvZero(image02);
  70. cvZero(image04);
  71. // This cycle draw all contours and approximate it by ellipses.
  72. for(;contour;contour = contour->h_next)
  73. {
  74. int count = contour->total; // This is number point in contour
  75. CvPoint center;
  76. CvSize size;
  77. CvBox2D box;
  78. // Number point must be more than or equal to 6 (for cvFitEllipse_32f).
  79. if( count < 6 )
  80. continue;
  81. CvMat* points_f = cvCreateMat( 1, count, CV_32FC2 );
  82. CvMat points_i = cvMat( 1, count, CV_32SC2, points_f->data.ptr );
  83. cvCvtSeqToArray( contour, points_f->data.ptr, CV_WHOLE_SEQ );
  84. cvConvert( &points_i, points_f );
  85. // Fits ellipse to current contour.
  86. box = cvFitEllipse2( points_f );
  87. // Draw current contour.
  88. cvDrawContours(image04,contour,CV_RGB(255,255,255),CV_RGB(255,255,255),0,1,8,cvPoint(0,0));
  89. // Convert ellipse data from float to integer representation.
  90. center = cvPointFrom32f(box.center);
  91. size.width = cvRound(box.size.width*0.5);
  92. size.height = cvRound(box.size.height*0.5);
  93. // Draw ellipse.
  94. cvEllipse(image04, center, size,
  95. -box.angle, 0, 360,
  96. CV_RGB(0,0,255), 1, CV_AA, 0);
  97. cvReleaseMat(&points_f);
  98. }
  99. // Show image. HighGUI use.
  100. cvShowImage( "Result", image04 );
  101. }
  102. #ifdef _EiC
  103. main(1,"fitellipse.c");
  104. #endif