c15c7cd0c0bb75c1ce90b1a4b33ee70671dcf5fd
[platform/upstream/opencv.git] / samples / c / pyramid_segmentation.c
1 #include "opencv2/imgproc/imgproc_c.h"
2 #include "opencv2/highgui/highgui_c.h"
3 #include "opencv2/legacy.hpp"
4 #include <stdio.h>
5
6 static void help(void)
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             "Usage :\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 static void ON_SEGMENT(int a)
35 {
36     (void)a;
37     cvPyrSegmentation(image0, image1, storage, &comp,
38                       level, threshold1+1, threshold2+1);
39
40     cvShowImage("Segmentation", image1);
41 }
42
43
44 int main( int argc, char** argv )
45 {
46     char* filename;
47
48     help();
49
50     filename = argc == 2 ? argv[1] : (char*)"fruits.jpg";
51
52     if( (image[0] = cvLoadImage( filename, 1)) == 0 )
53     {
54         help();
55         printf("Cannot load fileimage - %s\n", filename);
56         return -1;
57     }
58
59     cvNamedWindow("Source", 0);
60     cvShowImage("Source", image[0]);
61
62     cvNamedWindow("Segmentation", 0);
63
64     storage = cvCreateMemStorage ( block_size );
65
66     image[0]->width &= -(1<<level);
67     image[0]->height &= -(1<<level);
68
69     image0 = cvCloneImage( image[0] );
70     image1 = cvCloneImage( image[0] );
71     // segmentation of the color image
72     l = 1;
73     threshold1 =255;
74     threshold2 =30;
75
76     ON_SEGMENT(1);
77
78     sthreshold1 = cvCreateTrackbar("Threshold1", "Segmentation", &threshold1, 255, ON_SEGMENT);
79     sthreshold2 = cvCreateTrackbar("Threshold2", "Segmentation",  &threshold2, 255, ON_SEGMENT);
80
81     cvShowImage("Segmentation", image1);
82     cvWaitKey(0);
83
84     cvDestroyWindow("Segmentation");
85     cvDestroyWindow("Source");
86
87     cvReleaseMemStorage(&storage );
88
89     cvReleaseImage(&image[0]);
90     cvReleaseImage(&image0);
91     cvReleaseImage(&image1);
92
93     return 0;
94 }
95
96 #ifdef _EiC
97 main(1,"pyramid_segmentation.c");
98 #endif