分享
 
 
 

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

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

下面是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"

"\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 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

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有