pyramid_segmentation.c
上传用户:banwdc
上传日期:2016-06-25
资源大小:2871k
文件大小:2k
源码类别:

OpenCV

开发平台:

Visual C++

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