带掩码的 OpenCV 洪水填充 [英] OpenCV floodfill with mask

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

问题描述

OpenCV 的洪水填充功能的文档说明:

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天全站免登陆