Move the sources to trunk
[opencv] / samples / python / ffilldemo.py
1 #!/usr/bin/python
2 import sys
3 import random
4 from opencv.cv import *
5 from opencv.highgui import *
6
7 color_img0=None;
8 mask=None;
9 color_img=None;
10 gray_img0 = None;
11 gray_img = None;
12 ffill_case = 1;
13 lo_diff = 20
14 up_diff = 20;
15 connectivity = 4;
16 is_color = 1;
17 is_mask = 0;
18 new_mask_val = 255;
19
20 def update_lo( pos ):
21     lo_diff = pos
22 def update_up( pos ):
23     up_diff = pos
24
25 def on_mouse( event, x, y, flags, param ):
26
27     if( not color_img ):
28         return;
29
30     if event==CV_EVENT_LBUTTONDOWN:
31             comp = CvConnectedComp()
32             my_mask = None
33             seed = cvPoint(x,y);
34             if ffill_case==0:
35                 lo = up = 0
36                 flags = connectivity + (new_mask_val << 8)
37             else:
38                 lo = lo_diff;
39                 up = up_diff;
40                 flags = connectivity + (new_mask_val << 8) + CV_FLOODFILL_FIXED_RANGE
41             b = random.randint(0,255)
42             g = random.randint(0,255)
43             r = random.randint(0,255)
44
45             if( is_mask ):
46                 my_mask = mask
47                 cvThreshold( mask, mask, 1, 128, CV_THRESH_BINARY );
48                
49             if( is_color ):
50             
51                 color = CV_RGB( r, g, b );
52                 cvFloodFill( color_img, seed, color, CV_RGB( lo, lo, lo ),
53                              CV_RGB( up, up, up ), comp, flags, my_mask );
54                 cvShowImage( "image", color_img );
55             
56             else:
57             
58                 brightness = cvRealScalar((r*2 + g*7 + b + 5)/10);
59                 cvFloodFill( gray_img, seed, brightness, cvRealScalar(lo),
60                              cvRealScalar(up), comp, flags, my_mask );
61                 cvShowImage( "image", gray_img );
62             
63
64             print "%g pixels were repainted" % comp.area;
65
66             if( is_mask ):
67                 cvShowImage( "mask", mask );
68         
69     
70
71
72 if __name__ == "__main__":
73     
74     filename = "../c/fruits.jpg"
75     if len(sys.argv)>1:
76         filename=argv[1]
77     
78     color_img0 = cvLoadImage(filename,1)
79     if not color_img0:
80         print "Could not open %s" % filename
81         sys.exit(-1)
82
83     print "Hot keys:"
84     print "\tESC - quit the program"
85     print "\tc - switch color/grayscale mode"
86     print "\tm - switch mask mode"
87     print "\tr - restore the original image"
88     print "\ts - use null-range floodfill"
89     print "\tf - use gradient floodfill with fixed(absolute) range"
90     print "\tg - use gradient floodfill with floating(relative) range"
91     print "\t4 - use 4-connectivity mode"
92     print "\t8 - use 8-connectivity mode"
93         
94     color_img = cvCloneImage( color_img0 );
95     gray_img0 = cvCreateImage( cvSize(color_img.width, color_img.height), 8, 1 );
96     cvCvtColor( color_img, gray_img0, CV_BGR2GRAY );
97     gray_img = cvCloneImage( gray_img0 );
98     mask = cvCreateImage( cvSize(color_img.width + 2, color_img.height + 2), 8, 1 );
99
100     cvNamedWindow( "image", 1 );
101     cvCreateTrackbar( "lo_diff", "image", lo_diff, 255, update_lo);
102     cvCreateTrackbar( "up_diff", "image", up_diff, 255, update_up);
103
104     cvSetMouseCallback( "image", on_mouse );
105
106     while True: 
107         if( is_color ):
108             cvShowImage( "image", color_img );
109         else:
110             cvShowImage( "image", gray_img );
111
112         c = cvWaitKey(0);
113         if c=='\x1b':
114             print("Exiting ...");
115             sys.exit(0)
116         elif c=='c':
117             if( is_color ):
118             
119                 print("Grayscale mode is set");
120                 cvCvtColor( color_img, gray_img, CV_BGR2GRAY );
121                 is_color = 0;
122             
123             else:
124             
125                 print("Color mode is set");
126                 cvCopy( color_img0, color_img, None );
127                 cvZero( mask );
128                 is_color = 1;
129             
130         elif c=='m':
131             if( is_mask ):
132                 cvDestroyWindow( "mask" );
133                 is_mask = 0;
134             
135             else:
136                 cvNamedWindow( "mask", 0 );
137                 cvZero( mask );
138                 cvShowImage( "mask", mask );
139                 is_mask = 1;
140             
141         elif c=='r':
142             print("Original image is restored");
143             cvCopy( color_img0, color_img, None );
144             cvCopy( gray_img0, gray_img, None );
145             cvZero( mask );
146         elif c=='s':
147             print("Simple floodfill mode is set");
148             ffill_case = 0;
149         elif c=='f':
150             print("Fixed Range floodfill mode is set");
151             ffill_case = 1;
152         elif c=='g':
153             print("Gradient (floating range) floodfill mode is set");
154             ffill_case = 2;
155         elif c=='4':
156             print("4-connectivity mode is set");
157             connectivity = 4;
158         elif c=='8':
159             print("8-connectivity mode is set");
160             connectivity = 8;