/santa/OpenCV/samples/c/fitellipse.c

https://github.com/siegleal/iSanta · C · 152 lines · 79 code · 32 blank · 41 comment · 6 complexity · 4f45436884e7cde8c40eee7351c1b153 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. #ifndef _EiC
  21. #include "cv.h"
  22. #include "highgui.h"
  23. #endif
  24. int slider_pos = 70;
  25. // Load the source image. HighGUI use.
  26. IplImage *image02 = 0, *image03 = 0, *image04 = 0;
  27. void process_image(int h);
  28. int main( int argc, char** argv )
  29. {
  30. const char* filename = argc == 2 ? argv[1] : (char*)"stuff.jpg";
  31. // load image and force it to be grayscale
  32. if( (image03 = cvLoadImage(filename, 0)) == 0 )
  33. return -1;
  34. // Create the destination images
  35. image02 = cvCloneImage( image03 );
  36. image04 = cvCloneImage( image03 );
  37. // Create windows.
  38. cvNamedWindow("Source", 1);
  39. cvNamedWindow("Result", 1);
  40. // Show the image.
  41. cvShowImage("Source", image03);
  42. // Create toolbars. HighGUI use.
  43. cvCreateTrackbar( "Threshold", "Result", &slider_pos, 255, process_image );
  44. process_image(0);
  45. // Wait for a key stroke; the same function arranges events processing
  46. cvWaitKey(0);
  47. cvReleaseImage(&image02);
  48. cvReleaseImage(&image03);
  49. cvDestroyWindow("Source");
  50. cvDestroyWindow("Result");
  51. return 0;
  52. }
  53. // Define trackbar callback functon. This function find contours,
  54. // draw it and approximate it by ellipses.
  55. void process_image(int h)
  56. {
  57. CvMemStorage* stor;
  58. CvSeq* cont;
  59. CvBox2D32f* box;
  60. CvPoint* PointArray;
  61. CvPoint2D32f* PointArray2D32f;
  62. // Create dynamic structure and sequence.
  63. stor = cvCreateMemStorage(0);
  64. cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , stor);
  65. // Threshold the source image. This needful for cvFindContours().
  66. cvThreshold( image03, image02, slider_pos, 255, CV_THRESH_BINARY );
  67. // Find all contours.
  68. cvFindContours( image02, stor, &cont, sizeof(CvContour),
  69. CV_RETR_LIST, CV_CHAIN_APPROX_NONE, cvPoint(0,0));
  70. // Clear images. IPL use.
  71. cvZero(image02);
  72. cvZero(image04);
  73. // This cycle draw all contours and approximate it by ellipses.
  74. for(;cont;cont = cont->h_next)
  75. {
  76. int i; // Indicator of cycle.
  77. int count = cont->total; // This is number point in contour
  78. CvPoint center;
  79. CvSize size;
  80. // Number point must be more than or equal to 6 (for cvFitEllipse_32f).
  81. if( count < 6 )
  82. continue;
  83. // Alloc memory for contour point set.
  84. PointArray = (CvPoint*)malloc( count*sizeof(CvPoint) );
  85. PointArray2D32f= (CvPoint2D32f*)malloc( count*sizeof(CvPoint2D32f) );
  86. // Alloc memory for ellipse data.
  87. box = (CvBox2D32f*)malloc(sizeof(CvBox2D32f));
  88. // Get contour point set.
  89. cvCvtSeqToArray(cont, PointArray, CV_WHOLE_SEQ);
  90. // Convert CvPoint set to CvBox2D32f set.
  91. for(i=0; i<count; i++)
  92. {
  93. PointArray2D32f[i].x = (float)PointArray[i].x;
  94. PointArray2D32f[i].y = (float)PointArray[i].y;
  95. }
  96. // Fits ellipse to current contour.
  97. cvFitEllipse(PointArray2D32f, count, box);
  98. // Draw current contour.
  99. cvDrawContours(image04,cont,CV_RGB(255,255,255),CV_RGB(255,255,255),0,1,8,cvPoint(0,0));
  100. // Convert ellipse data from float to integer representation.
  101. center.x = cvRound(box->center.x);
  102. center.y = cvRound(box->center.y);
  103. size.width = cvRound(box->size.width*0.5);
  104. size.height = cvRound(box->size.height*0.5);
  105. box->angle = -box->angle;
  106. // Draw ellipse.
  107. cvEllipse(image04, center, size,
  108. box->angle, 0, 360,
  109. CV_RGB(0,0,255), 1, CV_AA, 0);
  110. // Free memory.
  111. free(PointArray);
  112. free(PointArray2D32f);
  113. free(box);
  114. }
  115. // Show image. HighGUI use.
  116. cvShowImage( "Result", image04 );
  117. }
  118. #ifdef _EiC
  119. main(1,"fitellipse.c");
  120. #endif