converted some more samples to C++
[profile/ivi/opencv.git] / samples / c / pyramid_segmentation.c
1 #include "opencv2/imgproc/imgproc.hpp"
2 #include "opencv2/highgui/highgui.hpp"
3
4 IplImage*  image[2] = { 0, 0 }, *image0 = 0, *image1 = 0;
5 CvSize size;
6
7 int  w0, h0,i;
8 int  threshold1, threshold2;
9 int  l,level = 4;
10 int sthreshold1, sthreshold2;
11 int  l_comp;
12 int block_size = 1000;
13 float  parameter;
14 double threshold;
15 double rezult, min_rezult;
16 int filter = CV_GAUSSIAN_5x5;
17 CvConnectedComp *cur_comp, min_comp;
18 CvSeq *comp;
19 CvMemStorage *storage;
20
21 CvPoint pt1, pt2;
22
23 void ON_SEGMENT(int a)
24 {
25     cvPyrSegmentation(image0, image1, storage, &comp,
26                       level, threshold1+1, threshold2+1);
27
28     /*l_comp = comp->total;
29
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
47 int main( int argc, char** argv )
48 {
49     char* filename = argc == 2 ? argv[1] : (char*)"fruits.jpg";
50
51     if( (image[0] = cvLoadImage( filename, 1)) == 0 )
52         return -1;
53
54     cvNamedWindow("Source", 0);
55     cvShowImage("Source", image[0]);
56
57     cvNamedWindow("Segmentation", 0);
58
59     storage = cvCreateMemStorage ( block_size );
60
61     image[0]->width &= -(1<<level);
62     image[0]->height &= -(1<<level);
63
64     image0 = cvCloneImage( image[0] );
65     image1 = cvCloneImage( image[0] );
66     // segmentation of the color image
67     l = 1;
68     threshold1 =255;
69     threshold2 =30;
70
71     ON_SEGMENT(1);
72
73     sthreshold1 = cvCreateTrackbar("Threshold1", "Segmentation", &threshold1, 255, ON_SEGMENT);
74     sthreshold2 = cvCreateTrackbar("Threshold2", "Segmentation",  &threshold2, 255, ON_SEGMENT);
75
76     cvShowImage("Segmentation", image1);
77     cvWaitKey(0);
78
79     cvDestroyWindow("Segmentation");
80     cvDestroyWindow("Source");
81
82     cvReleaseMemStorage(&storage );
83
84     cvReleaseImage(&image[0]);
85     cvReleaseImage(&image0);
86     cvReleaseImage(&image1);
87
88     return 0;
89 }
90
91 #ifdef _EiC
92 main(1,"pyramid_segmentation.c");
93 #endif