/santa/OpenCV/samples/c/pyramid_segmentation.c

https://github.com/siegleal/iSanta · C · 100 lines · 62 code · 21 blank · 17 comment · 3 complexity · d8b92a561eee73a546fa11e6bca58da8 MD5 · raw file

  1. #ifdef _CH_
  2. #pragma package <opencv>
  3. #endif
  4. #ifndef _EiC
  5. #include "cv.h"
  6. #include "highgui.h"
  7. #include <math.h>
  8. #endif
  9. IplImage* image[2] = { 0, 0 }, *image0 = 0, *image1 = 0;
  10. CvSize size;
  11. int w0, h0,i;
  12. int threshold1, threshold2;
  13. int l,level = 4;
  14. int sthreshold1, sthreshold2;
  15. int l_comp;
  16. int block_size = 1000;
  17. float parameter;
  18. double threshold;
  19. double rezult, min_rezult;
  20. CvFilter filter = CV_GAUSSIAN_5x5;
  21. CvConnectedComp *cur_comp, min_comp;
  22. CvSeq *comp;
  23. CvMemStorage *storage;
  24. CvPoint pt1, pt2;
  25. void ON_SEGMENT(int a)
  26. {
  27. cvPyrSegmentation(image0, image1, storage, &comp,
  28. level, threshold1+1, threshold2+1);
  29. /*l_comp = comp->total;
  30. i = 0;
  31. min_comp.value = cvScalarAll(0);
  32. while(i<l_comp)
  33. {
  34. cur_comp = (CvConnectedComp*)cvGetSeqElem ( comp, i );
  35. if(fabs(255- min_comp.value.val[0])>
  36. fabs(255- cur_comp->value.val[0]) &&
  37. fabs(min_comp.value.val[1])>
  38. fabs(cur_comp->value.val[1]) &&
  39. fabs(min_comp.value.val[2])>
  40. fabs(cur_comp->value.val[2]) )
  41. min_comp = *cur_comp;
  42. i++;
  43. }*/
  44. cvShowImage("Segmentation", image1);
  45. }
  46. int main( int argc, char** argv )
  47. {
  48. char* filename = argc == 2 ? argv[1] : (char*)"fruits.jpg";
  49. if( (image[0] = cvLoadImage( filename, 1)) == 0 )
  50. return -1;
  51. cvNamedWindow("Source", 0);
  52. cvShowImage("Source", image[0]);
  53. cvNamedWindow("Segmentation", 0);
  54. storage = cvCreateMemStorage ( block_size );
  55. image[0]->width &= -(1<<level);
  56. image[0]->height &= -(1<<level);
  57. image0 = cvCloneImage( image[0] );
  58. image1 = cvCloneImage( image[0] );
  59. // segmentation of the color image
  60. l = 1;
  61. threshold1 =255;
  62. threshold2 =30;
  63. ON_SEGMENT(1);
  64. sthreshold1 = cvCreateTrackbar("Threshold1", "Segmentation", &threshold1, 255, ON_SEGMENT);
  65. sthreshold2 = cvCreateTrackbar("Threshold2", "Segmentation", &threshold2, 255, ON_SEGMENT);
  66. cvShowImage("Segmentation", image1);
  67. cvWaitKey(0);
  68. cvDestroyWindow("Segmentation");
  69. cvDestroyWindow("Source");
  70. cvReleaseMemStorage(&storage );
  71. cvReleaseImage(&image[0]);
  72. cvReleaseImage(&image0);
  73. cvReleaseImage(&image1);
  74. return 0;
  75. }
  76. #ifdef _EiC
  77. main(1,"pyramid_segmentation.c");
  78. #endif