在触摸图像边框的对象中填充孔 [英] Filling holes in objects that touch the border of an image

查看:164
本文介绍了在触摸图像边框的对象中填充孔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图填写下图中的漏洞。

I'm trying to fill holes in the below image.

当我使用SciPy的binary_fill_holes()时,我通常会成功,除了触摸图像边框的对象。

When I use SciPy's binary_fill_holes(), I am generally successful, with the exception of objects that touch the image's border.

是否有任何现有的Python函数可以填充触摸边框的对象中的漏洞?我尝试在图像周围添加白色边框,但这只会导致整个图像被填充。

Are there any existing Python functions that can fill holes in objects that touch the border? I tried adding a white border around the image, but that just resulted in the entire image being filled.

推荐答案

这假设背景比其他东西多。它基本上对图像进行连通分量分析。提取最大的组件(假设为背景),并将其他所有组件设置为白色。

This assumes that there is more background than other stuff. It basically does a connected component analysis on the image. Extract the largest component (assumed to be the background), and sets everything else to white.

import numpy as np
import matplotlib.pyplot as plt
import skimage.morphology, skimage.data

img = skimage.data.imread('j1ESv.png', 1)
labels = skimage.morphology.label(img)
labelCount = np.bincount(labels.ravel())
background = np.argmax(labelCount)
img[labels != background] = 255
plt.imshow(img, cmap=plt.cm.gray)
plt.show()

这篇关于在触摸图像边框的对象中填充孔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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