Flood fill method for color image segmentation (source code)

Source: Internet
Author: User

The following is an example of the source code of the flood fill algorithm that comes with opencv b4.0. This allows for simple color image segmentation.

# Ifdef _ CH _
# Pragma package <opencv>
# Endif

# Ifndef _ EIC
# Include "cv. H"
# Include "highgui. H"
# Include <stdio. h>
# Include <stdlib. h>
# Endif

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;
Cvconnectedcomp;

If (is_mask)
Cvthreshold (mask, mask, 1,128, cv_thresh_binary );

If (is_color)
{
Cvscalar color = cv_rgb (R, G, B );
Cvfloodfill (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", comp. 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", & amp; 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 restored/N ");
Cvcopy (color_img0, color_img, null );
Cvcopy (gray_img0, gray_img, null );
Cvzero (mask );
Break;
Case's ':
Printf ("simple floodfill mode is set/N ");
Ffill_case = 0;
Break;
Case 'F ':
Printf ("fixed range floodfill mode is set/N ");
Ffill_case = 1;
Break;
Case 'G ':
Printf ("gradient (floating range) floodfill 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 ");
Connectivity = 8;
Break;
}
}

Exit_main:

Cvdestroywindow ("test ");
Cvreleaseimage (& gray_img );
Cvreleaseimage (& gray_img0 );
Cvreleaseimage (& color_img );
Cvreleaseimage (& color_img0 );
Cvreleaseimage (& Mask );

Return 1;
}

# Ifdef _ EIC
Main (1, "ffilldemo. c ");
# Endif

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.