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