some samples updated according to new CommandLineParser class
[profile/ivi/opencv.git] / samples / c / pyramid_segmentation.c
1 #include "opencv2/imgproc/imgproc.hpp"
2 #include "opencv2/highgui/highgui.hpp"
3 #include "opencv2/imgproc/imgproc_c.h"
4 #include <stdio.h>
5
6 void help()
7 {
8         printf("\nThis program demonstrated color pyramid segmentation cvcvPyrSegmentation() which is controlled\n"
9                         "by two trhesholds which can be manipulated by a trackbar. It can take an image file name or defaults to 'fruits.jpg'\n"
10                         "Call:\n"
11                         "./pyaramid_segmentation [image_path_filename -- Defaults to fruits.jpg]\n\n"
12                         );
13 }
14
15 IplImage*  image[2] = { 0, 0 }, *image0 = 0, *image1 = 0;
16 CvSize size;
17
18 int  w0, h0,i;
19 int  threshold1, threshold2;
20 int  l,level = 4;
21 int sthreshold1, sthreshold2;
22 int  l_comp;
23 int block_size = 1000;
24 float  parameter;
25 double threshold;
26 double rezult, min_rezult;
27 int filter = CV_GAUSSIAN_5x5;
28 CvConnectedComp *cur_comp, min_comp;
29 CvSeq *comp;
30 CvMemStorage *storage;
31
32 CvPoint pt1, pt2;
33
34 void ON_SEGMENT(int a)
35 {
36     cvPyrSegmentation(image0, image1, storage, &comp,
37                       level, threshold1+1, threshold2+1);
38
39     /*l_comp = comp->total;
40
41     i = 0;
42     min_comp.value = cvScalarAll(0);
43     while(i<l_comp)
44     {
45         cur_comp = (CvConnectedComp*)cvGetSeqElem ( comp, i );
46         if(fabs(255- min_comp.value.val[0])>
47            fabs(255- cur_comp->value.val[0]) &&
48            fabs(min_comp.value.val[1])>
49            fabs(cur_comp->value.val[1]) &&
50            fabs(min_comp.value.val[2])>
51            fabs(cur_comp->value.val[2]) )
52            min_comp = *cur_comp;
53         i++;
54     }*/
55     cvShowImage("Segmentation", image1);
56 }
57
58
59 int main( int argc, char** argv )
60 {
61     char* filename = argc == 2 ? argv[1] : (char*)"fruits.jpg";
62
63     if( (image[0] = cvLoadImage( filename, 1)) == 0 )
64         return -1;
65
66     cvNamedWindow("Source", 0);
67     cvShowImage("Source", image[0]);
68
69     cvNamedWindow("Segmentation", 0);
70
71     storage = cvCreateMemStorage ( block_size );
72
73     image[0]->width &= -(1<<level);
74     image[0]->height &= -(1<<level);
75
76     image0 = cvCloneImage( image[0] );
77     image1 = cvCloneImage( image[0] );
78     // segmentation of the color image
79     l = 1;
80     threshold1 =255;
81     threshold2 =30;
82
83     ON_SEGMENT(1);
84
85     sthreshold1 = cvCreateTrackbar("Threshold1", "Segmentation", &threshold1, 255, ON_SEGMENT);
86     sthreshold2 = cvCreateTrackbar("Threshold2", "Segmentation",  &threshold2, 255, ON_SEGMENT);
87
88     cvShowImage("Segmentation", image1);
89     cvWaitKey(0);
90
91     cvDestroyWindow("Segmentation");
92     cvDestroyWindow("Source");
93
94     cvReleaseMemStorage(&storage );
95
96     cvReleaseImage(&image[0]);
97     cvReleaseImage(&image0);
98     cvReleaseImage(&image1);
99
100     return 0;
101 }
102
103 #ifdef _EiC
104 main(1,"pyramid_segmentation.c");
105 #endif