Below is the source code sample of the FLOOD Fill algorithm included with OpenCV B4.0, simple color image segmentation can be implemented.
#ifdef _ch_ # pragma package
#ifndef_eic # include "CV.H" #include "highgui.h" #include
IplImage * color_img0; IplImage * mask; IplImage * color_img; IplImage * gray_img0 = NULL; IplImage * gray_img = NULL; int ffill_case = 1; int lo_diff = 20, up_diff = 20; int connectivity = 4; int is_color = 1; int is_mask = 0; int new_mask_val = 255;
Void on_mouse (int Event, int x, int y, int flags) {if (! color_img) return;
switch (event) {case CV_EVENT_LBUTTONDOWN: {CvPoint seed = cvPoint (x, y); int lo = ffill_case == 0 0:? lo_diff; int up = ffill_case == 0 0: up_diff; int flags = connectivity (new_mask_val? << 8) (ffill_case == 1? CV_FLOODFILL_FIXED_RANGE: 0); int b = rand () & 255, g = rand () & 255, r = rand () & 255; CVConnectedCompComp;
IF (IS_MASK) CVTHRESHOLD (Mask, Mask, 1, 128, CV_THRESH_BINARY); if (is_color) {cvscalaar color = CV_RGB (R, G, B); CVFLODFILL (Color_Img, Seed, Color, CV_RGB (Lo, LO, LO) , CV_RGB (Up, Up, Up), & Comp, Flags, IS_MASK? MASK: NULL; CVSHOWIMAGE ("Image", Color_img);} else {cvscalar Brightness = CVREALSCALAR ((R * 2 g * 7 B 5 ) / 10); CVFLOODFILL (Gray_img, Seed, Brightness, CVRealscalar (LO), CVREALSCALAR (UP), & COMP, FLAGS, IS_MASK? Mask: null; cvshowimage ("image", gray_img);} printf ("% g Pixels) WERE repainted / n ", commit.area);
IF (is_mask) CvShowImage ("Mask", Mask;} Break;}}
INT main (int Argc, char ** argv) {char * filename = argc> = 2? argv [1]: (char *) "fruits.jpg";
IF ((color_img0 = cvloadimage (filename, 1)) == 0) Return 0;
Printf ("Hot Keys: / N" "/ TESC - Quit The Program / N" "/ Tc - Switch Color / Grayscale Mode / N" "/ TM - Switch Mask Mode / N" "/ Tr - Restore The Original Image / N "" / ts - use null-range floodfill / n "" / tf - use gradient floodfill with fixed (absolute) Range / N "/ TG - Use gradient floodfill with floating (relative) Range / N" "/ T4 - use 4-connectivity mode / n "" / t8 - use 8-connectivity mode / n "); color_img = cvCloneImage (color_img0); gray_img0 = cvCreateImage (cvSize (color_img-> width, color_img-> height), 8, 1) ; cvCvtColor (color_img, gray_img0, CV_BGR2GRAY); gray_img = cvCloneImage (gray_img0); mask = cvCreateImage (cvSize (color_img-> width 2, color_img-> height 2), 8, 1); cvNamedWindow ( "image", 0 ); CvcreateTrackbar ("LO_DIFF", "Image", & LO_DIFF, 255, NULL); CVCreateTrackbar ("UP_DIFF", "Image", & Up_Diff, 255, null);
CvseTMouseCallback ("image", on_mouse);
For (;;) {INT C; if (is_color) CVSHOWIMAGE ("Image", Color_img); Else CvshowImage ("Image", Gray_img);
C = cvwaitkey (0); switch (c) {case '/ x1b': Printf ("exiting ... / n"); goto exit_main; case 'c': IF (is_color) {printf ("Grayscale Mode Is Set / N "); cvcvtcolor (color_img, gray_img, cv_bgr2gray); IS_COLOR = 0;} else {printf (" color mode is set / n "); cvcopy (color_img0, color_img, null); cvzero (mask); is_color = 1 } Break; Case 'm': IF (IS_MASK) {CVDESTROYWINDOW ("Mask"); IS_MASK = 0;} else {cvnamedWindow ("Mask", 0); Cvzero (MASK); CvShowImage ("Mask", Mask) IS_MASK = 1;} Break; Case 'R': Printf ("Original Image Is Restore / N); CVCopy (Color_IMG 0, color_img, null; cvcopy; cvzero (mask); Break; case 's': printf ("Simple Floodfill Mode Is Set / N); ffill_case = 0; Break; Case' f ': "Fixed Range Floodfill Mode Is Set / N"); ffill_case = 1; Break; Case' G ': Printf ("Gradient (Floating Range) FLODFILL MODE IS SET / N"); ffill_case = 2; Break; Case '4': Printf ("4-Connectivity Mode Is Set / N);
Connectivity = 4; Break; Case '8': Printf ("8-Connectivity Mode IS Set / N); connection;}} exit_main:
CvDestroyWindow ("Test"); CVRELEASEIMAGE (& gray_img); CVRELEASEIMAGE (& gray_img0); CVRELEASEIMAGE (& Color_img); CVRELEASEIMAGE (& Color_img0); CVRELEASEIMAGE (& Mask);
Return 1;}
#ifdef _eicmain (1, "ffillDemo.c"); # eDIF