Update the changelog
[opencv] / samples / python / pyramid_segmentation.py
1 #!/usr/bin/python
2 import sys
3 from opencv.cv import *
4 from opencv.highgui import *
5 image =  [None, None]
6 image0 = None
7 image1 = None
8 threshold1 = 255
9 threshold2 = 30
10 l = level = 4;
11 block_size = 1000;
12 filter = CV_GAUSSIAN_5x5;
13 storage = None
14 min_comp = CvConnectedComp()
15
16 def set_thresh1( val ):
17     global threshold1
18     threshold1 = val
19     ON_SEGMENT()
20
21 def set_thresh2( val ):
22     global threshold2
23     threshold2 = val
24     ON_SEGMENT()
25
26 def ON_SEGMENT():
27     global storage
28     global min_comp
29     comp = cvPyrSegmentation(image0, image1, storage, level, threshold1+1, threshold2+1);
30     cvShowImage("Segmentation", image1);
31
32 if __name__ == "__main__":
33     filename = "../c/fruits.jpg";
34     if len(sys.argv) == 2:
35         filename = sys.argv[1]
36     image[0] = cvLoadImage( filename, 1)
37     if not image[0]:
38         print "Error opening %s" % filename
39         sys.exit(-1)
40
41     cvNamedWindow("Source", 0);
42     cvShowImage("Source", image[0]);
43     cvNamedWindow("Segmentation", 0);
44     storage = cvCreateMemStorage ( block_size );
45     image[0].width &= -(1<<level);
46     image[0].height &= -(1<<level);
47     image0 = cvCloneImage( image[0] );
48     image1 = cvCloneImage( image[0] );
49     # segmentation of the color image
50     l = 1;
51     threshold1 =255;
52     threshold2 =30;
53     ON_SEGMENT();
54     sthreshold1 = cvCreateTrackbar("Threshold1", "Segmentation", threshold1, 255, set_thresh1);
55     sthreshold2 = cvCreateTrackbar("Threshold2", "Segmentation",  threshold2, 255, set_thresh2);
56     cvShowImage("Segmentation", image1);
57     cvWaitKey(0);
58     cvDestroyWindow("Segmentation");
59     cvDestroyWindow("Source");