彩色图像分割的FLOOD FILL方法(源代码)[1]

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 hunnish 的 blog

下面是opencv b4.0 附带的 flood fill 算法的源代码样例,可以实现简单的彩色图像分割。

#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 comp;

            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"

本文关键:彩色图像分割的FLOOD FILL方法(源代码)
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top