19 function ret = randint(v1, v2)
20 ret = int32(rand() * (v2 - v1) + v1);
23 function update_lo( pos )
26 function update_up( pos )
30 function on_mouse( event, x, y, flags, param )
35 if( !swig_this(g.color_img) )
39 if (event == highgui.CV_EVENT_LBUTTONDOWN)
40 comp = cv.CvConnectedComp();
46 flags = g.connectivity + bitshift(g.new_mask_val,8);
50 flags = g.connectivity + bitshift(g.new_mask_val,8) + \
51 cv.CV_FLOODFILL_FIXED_RANGE;
53 color = CV_RGB( randint(0,255), randint(0,255), randint(0,255) );
57 cvThreshold( g.mask, g.mask, 1, 128, cv.CV_THRESH_BINARY );
61 cv.cvFloodFill( g.color_img, seed, color, cv.CV_RGB( lo, lo, lo ),
62 CV_RGB( up, up, up ), comp, flags, my_mask );
63 cvShowImage( "image", g.color_img );
67 brightness = cvRealScalar((r*2 + g*7 + b + 5)/10);
68 cvFloodFill( g.gray_img, seed, brightness, cvRealScalar(lo),
69 cvRealScalar(up), comp, flags, my_mask );
70 cvShowImage( "image", g.gray_img );
74 printf("%i pixels were repainted\n", comp.area);
77 cvShowImage( "mask", g.mask );
85 filename = "../c/fruits.jpg";
90 g.color_img0 = cvLoadImage(filename,1);
91 if (!swig_this(g.color_img0))
92 printf("Could not open %s\n",filename);
96 printf("Hot keys:\n");
97 printf("\tESC - quit the program\n");
98 printf("\tc - switch color/grayscale mode\n");
99 printf("\tm - switch mask mode\n");
100 printf("\tr - restore the original image\n");
101 printf("\ts - use null-range floodfill\n");
102 printf("\tf - use gradient floodfill with fixed(absolute) range\n");
103 printf("\tg - use gradient floodfill with floating(relative) range\n");
104 printf("\t4 - use 4-g.connectivity mode\n");
105 printf("\t8 - use 8-g.connectivity mode\n");
107 g.color_img = cvCloneImage( g.color_img0 );
108 g.gray_img0 = cvCreateImage( cvSize(g.color_img.width, g.color_img.height), 8, 1 );
109 cvCvtColor( g.color_img, g.gray_img0, CV_BGR2GRAY );
110 g.gray_img = cvCloneImage( g.gray_img0 );
111 g.mask = cvCreateImage( cvSize(g.color_img.width + 2, g.color_img.height + 2), 8, 1 );
113 cvNamedWindow( "image", 1 );
114 cvCreateTrackbar( "g.lo_diff", "image", g.lo_diff, 255, @update_lo);
115 cvCreateTrackbar( "g.up_diff", "image", g.up_diff, 255, @update_up);
117 cvSetMouseCallback( "image", @on_mouse );
121 cvShowImage( "image", g.color_img );
123 cvShowImage( "image", g.gray_img );
128 printf("Exiting ...\n");
133 print("Grayscale mode is set");
134 cvCvtColor( g.color_img, g.gray_img, CV_BGR2GRAY );
139 print("Color mode is set");
140 cvCopy( g.color_img0, g.color_img, [] );
147 cvDestroyWindow( "mask" );
151 cvNamedWindow( "mask", 0 );
153 cvShowImage( "mask", g.mask );
158 printf("Original image is restored");
159 cvCopy( g.color_img0, g.color_img, [] );
160 cvCopy( g.gray_img0, g.gray_img, [] );
163 printf("Simple floodfill mode is set");
166 printf("Fixed Range floodfill mode is set");
169 printf("Gradient (floating range) floodfill mode is set");
172 printf("4-g.connectivity mode is set");
175 printf("8-g.connectivity mode is set");