Opencv学习——漫水填充(floodfill)

漫水填充法是一种用特定的颜色填充连通区域。经常被用来标记或分离图像的一部分,以便对其进行进一步处理或分析。

原型:

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

加入鼠标回调函数:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值