25 def on_mouse( event, x, y, flags, param ):
30 if event == cv.CV_EVENT_LBUTTONDOWN:
35 flags = connectivity + (new_mask_val << 8)
39 flags = connectivity + (new_mask_val << 8) + cv.CV_FLOODFILL_FIXED_RANGE
40 b = random.randint(0,255)
41 g = random.randint(0,255)
42 r = random.randint(0,255)
46 cv.Threshold( mask, mask, 1, 128, cv.CV_THRESH_BINARY );
50 color = cv.CV_RGB( r, g, b );
51 comp = cv.FloodFill( color_img, seed, color, cv.CV_RGB( lo, lo, lo ),
52 cv.CV_RGB( up, up, up ), flags, my_mask );
53 cv.ShowImage( "image", color_img );
57 brightness = cv.RealScalar((r*2 + g*7 + b + 5)/10);
58 comp = cv.FloodFill( gray_img, seed, brightness, cv.RealScalar(lo),
59 cv.RealScalar(up), flags, my_mask );
60 cv.ShowImage( "image", gray_img );
63 print "%g pixels were repainted" % comp[0]
66 cv.ShowImage( "mask", mask );
71 if __name__ == "__main__":
74 im = cv.LoadImage( sys.argv[1], cv.CV_LOAD_IMAGE_COLOR)
76 url = 'http://code.opencv.org/svn/opencv/trunk/opencv/samples/c/fruits.jpg'
77 filedata = urllib2.urlopen(url).read()
78 imagefiledata = cv.CreateMatHeader(1, len(filedata), cv.CV_8UC1)
79 cv.SetData(imagefiledata, filedata, len(filedata))
80 im = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR)
83 print "\tESC - quit the program"
84 print "\tc - switch color/grayscale mode"
85 print "\tm - switch mask mode"
86 print "\tr - restore the original image"
87 print "\ts - use null-range floodfill"
88 print "\tf - use gradient floodfill with fixed(absolute) range"
89 print "\tg - use gradient floodfill with floating(relative) range"
90 print "\t4 - use 4-connectivity mode"
91 print "\t8 - use 8-connectivity mode"
93 color_img = cv.CloneImage( im );
94 gray_img0 = cv.CreateImage( (color_img.width, color_img.height), 8, 1 );
95 cv.CvtColor( color_img, gray_img0, cv.CV_BGR2GRAY );
96 gray_img = cv.CloneImage( gray_img0 );
97 mask = cv.CreateImage( (color_img.width + 2, color_img.height + 2), 8, 1 );
99 cv.NamedWindow( "image", 1 );
100 cv.CreateTrackbar( "lo_diff", "image", lo_diff, 255, update_lo);
101 cv.CreateTrackbar( "up_diff", "image", up_diff, 255, update_up);
103 cv.SetMouseCallback( "image", on_mouse );
107 cv.ShowImage( "image", color_img );
109 cv.ShowImage( "image", gray_img );
111 c = cv.WaitKey(0) % 0x100
113 print("Exiting ...");
118 print("Grayscale mode is set");
119 cv.CvtColor( color_img, gray_img, cv.CV_BGR2GRAY );
124 print("Color mode is set");
125 cv.Copy( im, color_img, None );
131 cv.DestroyWindow( "mask" );
135 cv.NamedWindow( "mask", 0 );
137 cv.ShowImage( "mask", mask );
141 print("Original image is restored");
142 cv.Copy( im, color_img, None );
143 cv.Copy( gray_img0, gray_img, None );
146 print("Simple floodfill mode is set");
149 print("Fixed Range floodfill mode is set");
152 print("Gradient (floating range) floodfill mode is set");
155 print("4-connectivity mode is set");
158 print("8-connectivity mode is set");
160 cv.DestroyAllWindows()