带有面罩的OpenCV充水 [英] OpenCV floodfill with mask

查看:68
本文介绍了带有面罩的OpenCV充水的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于OpenCV的Floodfill功能状态的文档:

The documentation for OpenCV's floodfill function states:

该函数使用并更新掩码,因此您要负责 初始化遮罩内容.洪水填充不能跨越非零 遮罩中的像素.例如,可以使用边缘检测器输出 作为遮罩以停止在边缘填充.可以使用相同的 多次调用函数时要遮罩以确保填充区域 不重叠.

The function uses and updates the mask, so you take responsibility of initializing the mask content. Flood-filling cannot go across non-zero pixels in the mask. For example, an edge detector output can be used as a mask to stop filling at edges. It is possible to use the same mask in multiple calls to the function to make sure the filled area does not overlap.

该功能如何更新遮罩?是否会将泛洪填充内的所有像素都设置为某个非零值?

How does the function update the mask? Does it set all the pixels within the floodfill to some non-zero value?

推荐答案

与遮罩的种子点相同的连接组件中的所有零值像素都将替换为您指定的值.此值必须添加到flags参数中,并左移8位:

All zero-valued pixels in the same connected component as the seed point of the mask are replaced by the value you specify. This value must be added to the flags parameter, left-shifted by 8 bits:

uchar fillValue = 128;
cv::floodFill(img, mask, seed, cv::Scalar(255) ,0, cv::Scalar(), cv::Scalar(), 4 | cv::FLOODFILL_MASK_ONLY | (fillValue << 8));

下面是一个简单但可能很有启发性的示例.像这样创建图像:

A simple, but perhaps enlightening example follows. Creating an image like so:

//Create simple input image
cv::Point seed(4,4);
cv::Mat img = cv::Mat::zeros(100,100,CV_8UC1);
cv::circle(img, seed, 20, cv::Scalar(128),3);

此图片的结果:

然后,创建一个蒙版并对其进行洪水填充:

Then, creating a mask and flood-filling it:

//Create a mask from edges in the original image
cv::Mat mask;
cv::Canny(img, mask, 100, 200);
cv::copyMakeBorder(mask, mask, 1, 1, 1, 1, cv::BORDER_REPLICATE);

//Fill mask with value 128
uchar fillValue = 128;
cv::floodFill(img, mask, seed, cv::Scalar(255) ,0, cv::Scalar(), cv::Scalar(), 4 | cv::FLOODFILL_MASK_ONLY | (fillValue << 8));

给出以下结果:

蒙版中的白色像素是边缘检测的结果,而灰色像素是填充填充的结果.

The white pixels in the mask are the result of edge detection, while the grey pixels are the result of the flood-fill.

更新: 响应于该评论,标志值4指定要与之比较色值差的像素邻域.从文档中:

UPDATE: In response to the comment, flag value 4 specifies the pixel neighborhood with which to compare the color value difference. From the documentation:

低位包含在功能内使用的连接性值4(默认值)或8.连通性决定了要考虑像素的哪个邻居.

Lower bits contain a connectivity value, 4 (default) or 8, used within the function. Connectivity determines which neighbors of a pixel are considered.

当未传递cv::FLOODFILL_MASK_ONLY标志时,两者均会更新图像和遮罩,但是泛洪填充将停止在任何非零的遮罩值处.

When the cv::FLOODFILL_MASK_ONLY flag is not passed, both the image and the mask are updated, but the flood filling will stop at at any nonzero mask values.

这篇关于带有面罩的OpenCV充水的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆