漫水填充法是一种用特定的颜色填充连通区域。经常被用来标记或分离图像的一部分,以便对其进行进一步处理或分析。
原型:
int floodFill( InputOutputArray image,
Point seedPoint, Scalar newVal, CV_OUT Rect* rect = 0,
Scalar loDiff = Scalar(), Scalar upDiff = Scalar(),
int flags = 4 );
image | 输入/输出图像 |
seedPoint | 漫水填充算法的起始点 |
newVal | 像素点被染色的值 |
rect | 有默认值0,一个可选参数,重绘区域最小边界矩形区域 |
loDiff | 有默认值, 亮度或颜色之负差的最大值 |
upDiff | 有默认值, 亮度或颜色之正差的最大值 |
flages | 4:考虑垂直和水平方向的相邻点; 8:考虑垂直和水平方向的相邻点,也考虑对角线方向的相邻点。 |
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include<iostream>
using namespace cv;
Mat image;
static void onMouse(int event, int x, int y, int flags, void* param)
{
if (event != EVENT_LBUTTONDOWN)
return;
Point seed = Point(x, y);
int b = (unsigned)theRNG() & 255;
int g = (unsigned)theRNG() & 255;
int r = (unsigned)theRNG() & 255;
Scalar newVal = Scalar(b, g, r);
Rect ccomp;
floodFill(image, seed, newVal, &ccomp, Scalar(20, 20, 20), Scalar(20, 20, 20));
imshow("(效果图)",image);
}
int main()
{
image = imread("F:\\C++project\\picturetest\\4.jpg");
//namedWindow("(原图)");
namedWindow("(效果图)",WINDOW_AUTOSIZE);
setMouseCallback("(效果图)",onMouse,0);
//imshow("(效果图)", image);
waitKey(0);
return 0;
}
加入鼠标回调函数: