File: pyramid_segmentation.py

package info (click to toggle)
opencv 2.1.0-3%2Bsqueeze1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 68,800 kB
  • ctags: 52,010
  • sloc: cpp: 554,793; xml: 475,942; ansic: 153,396; python: 18,622; sh: 428; makefile: 111
file content (59 lines) | stat: -rwxr-xr-x 1,609 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python
import sys
from opencv.cv import *
from opencv.highgui import *
image =  [None, None]
image0 = None
image1 = None
threshold1 = 255
threshold2 = 30
l = level = 4;
block_size = 1000;
filter = CV_GAUSSIAN_5x5;
storage = None
min_comp = CvConnectedComp()

def set_thresh1( val ):
    global threshold1
    threshold1 = val
    ON_SEGMENT()

def set_thresh2( val ):
    global threshold2
    threshold2 = val
    ON_SEGMENT()

def ON_SEGMENT():
    global storage
    global min_comp
    comp = cvPyrSegmentation(image0, image1, storage, level, threshold1+1, threshold2+1);
    cvShowImage("Segmentation", image1);

if __name__ == "__main__":
    filename = "../c/fruits.jpg";
    if len(sys.argv) == 2:
        filename = sys.argv[1]
    image[0] = cvLoadImage( filename, 1)
    if not image[0]:
        print "Error opening %s" % filename
        sys.exit(-1)

    cvNamedWindow("Source", 0);
    cvShowImage("Source", image[0]);
    cvNamedWindow("Segmentation", 0);
    storage = cvCreateMemStorage ( block_size );
    image[0].width &= -(1<<level);
    image[0].height &= -(1<<level);
    image0 = cvCloneImage( image[0] );
    image1 = cvCloneImage( image[0] );
    # segmentation of the color image
    l = 1;
    threshold1 =255;
    threshold2 =30;
    ON_SEGMENT();
    sthreshold1 = cvCreateTrackbar("Threshold1", "Segmentation", threshold1, 255, set_thresh1);
    sthreshold2 = cvCreateTrackbar("Threshold2", "Segmentation",  threshold2, 255, set_thresh2);
    cvShowImage("Segmentation", image1);
    cvWaitKey(0);
    cvDestroyWindow("Segmentation");
    cvDestroyWindow("Source");