使用 opencv 填充连接组件内的孔
我正在寻找填补二值图像最大组成部分内的漏洞。
输入二值图像:
所以我正在寻找的是填充紫色组件内的“湖泊”,但不改变任何其他像素。
我知道使用 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:
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.
I also tried different techniques from different packages (found on other questions). The result is not the one I'm looking for:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论