使用 opencv 填充连接组件内的孔

发布于 2025-01-14 03:13:19 字数 1140 浏览 0 评论 0原文

我正在寻找填补二值图像最大组成部分内的漏洞。

输入二值图像:

在此处输入图像描述

所以我正在寻找的是填充紫色组件内的“湖泊”,但不改变任何其他像素。

我知道使用 opencv 中的connectedComponentsWithStats 函数或使用scikit-image 在stackoverflow 上有几个答案,但我无法让它按照我想要的方式工作。因此,请不要将其标记为重复。

这是我使用其他问题的答案所做的:

nb_components, output, stats, centroids = cv.connectedComponentsWithStats(image, connectivity=4)
sizes = stats[:, -1]

max_label = 1
max_size = sizes[1]
for i in range(2, nb_components):
    if sizes[i] > max_size:
        max_label = i
        max_size = sizes[i]

res = np.zeros(output.shape)
res[output == max_label] = res

它总是修改其余的像素,并且不会填充所有的孔。

输入图片这里的描述

我还尝试了不同包中的不同技术(在其他问题上找到)。结果不是我正在寻找的结果:

在此处输入图像描述

我希望仅修改孔的像素,而不触及其他任何内容。

我应该在脚本中更改什么才能做到这一点?

I'm looking to fill holes inside the largest component of an binary image.

Input binary image:

enter image description here

So what I'm looking for is to fill "lakes" inside the purple component, but not altering any other pixels.

I know there are several answers on stackoverflow using connectedComponentsWithStats function from opencv or with scikit-image but I cannot make it work the way I'm looking to. So please don't tag this as duplicate.

Here is what I've made using other questions' answers:

nb_components, output, stats, centroids = cv.connectedComponentsWithStats(image, connectivity=4)
sizes = stats[:, -1]

max_label = 1
max_size = sizes[1]
for i in range(2, nb_components):
    if sizes[i] > max_size:
        max_label = i
        max_size = sizes[i]

res = np.zeros(output.shape)
res[output == max_label] = res

It always modifies the rest of the pixels and is not filling all the holes.

enter image description here

I also tried different techniques from different packages (found on other questions). The result is not the one I'm looking for:

enter image description here

I'm looking to only modify the pixel of the holes and not touching anything else.

What should I change in my script to do that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文