函数原型:
/* Fills the connected component until the color difference gets large enough */ CVAPI(void) cvFloodFill( CvArr* image, CvPoint seed_point, CvScalar new_val, CvScalar lo_diff CV_DEFAULT(cvScalarAll(0)), CvScalar up_diff CV_DEFAULT(cvScalarAll(0)), CvConnectedComp* comp CV_DEFAULT(NULL), int flags CV_DEFAULT(4), CvArr* mask CV_DEFAULT(NULL));
image:被填充的图像
seed_point:开始填充的点
new_val:填充的像素值
lo_diff和up_diff:x,y为当前要填充的点,x',y'为x,y的4或者8邻域的点,当src(x',y')-lo_diff<=src(x,y)<=src(x',y')+up_diff时x,y将被填充
comp:设置为被填充区域的统计属性
flag:|高8位第16~23位|中间8位第8~15(掩码填充的值,未设置时,mask将被填充为1)|低8位第0~7位(表示4或者8邻域)|
flag的高8为可取值为:
CV_FLOODFILL_FIXED_RANGE比较当前要填充的点与种子点的像素否则比较4或者8邻域的点
CV_FLOODFILL_MASK_ONLY只填充掩码
mask:掩码,为单通道,8位,像素宽度和高度是image的2倍,mask(x+1,y+1)与image(x,y)相对应。cvFloodFill不会覆盖mask为非0像素点
示例:
#include <cv.h> #include <highgui.h> #include <stdio.h> void InitImage(IplImage* image); int main() { IplImage* image = cvCreateImage(cvSize(200, 200), 8, 3); InitImage(image); cvFloodFill(image,//被填充的图像 cvPoint(0, 0),//开始填充的点 cvScalar(255, 0, 0),//填充的像素值 cvScalar(0, 0, 0)/*=lo_diff*/, //x,y为当前要填充的点,x',y'为x,y的4或者8邻域的点 cvScalar(0, 0, 0)/*=up_diff*/, //当src(x',y')-lo_diff<=src(x,y)<=src(x',y')+up_diff时x,y将被填充 NULL/*=flag*/, // |高8位第16~23位|中间8位第8~15(掩码填充的值,未设置时,mask将被填充为1)|低8位第0~7位(表示4或者8邻域)| // flag的高8为可取值为: // CV_FLOODFILL_FIXED_RANGE 比较当前要填充的点与种子点的像素 // CV_FLOODFILL_MASK_ONLY 只填充掩码 8 | CV_FLOODFILL_FIXED_RANGE, NULL //掩码,为单通道,8位,像素宽度和高度是image的2倍,mask(x+1,y+1)与image(x,y)相对应。cvFloodFill不会覆盖mask为非0像素点 ); cvNamedWindow("image"); cvShowImage("image", image); cvWaitKey(0); cvReleaseImage(&image); cvDestroyWindow("image"); return 0; } void InitImage(IplImage* image) { assert(image != NULL); assert(image->nChannels == 3); cvZero(image); int interval = 10; int width = image->width; int height = image->height; int widthStep = image->widthStep; int i, j, k; int flag = 1; for(i = 0; i < height; i += interval) { if(flag % 2) { int up = i + interval < height ? i + interval : height; for(j = i; j < up; ++j) { for(k = 0; k < width; ++k) { image->imageData[j * widthStep + 3 * k + 0] = (char)0;//B image->imageData[j * widthStep + 3 * k + 1] = (char)0;//G image->imageData[j * widthStep + 3 * k + 2] = (char)255;//R } } } ++flag; } int flag2 = 1; int lower, uper; for(i = 0; i < height; i += interval) { if(!(flag % 2)) { int up = i + interval < height ? i + interval : height; if(flag2 % 2) { lower = 0; uper = 10; } else { lower = width - 10; uper = width; } ++flag2; for(j = i; j < up; ++j) { for(k = lower; k < uper; ++k) { image->imageData[j * widthStep + 3 * k + 0] = (char)0;//B image->imageData[j * widthStep + 3 * k + 1] = (char)0;//G image->imageData[j * widthStep + 3 * k + 2] = (char)255;//R } } } ++flag; } }