图像处理-填充空心圆 [英] Image processing - fill in hollow circles

查看:174
本文介绍了图像处理-填充空心圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的二进制黑白图像





我想将那些白色圆圈填充为纯白色磁盘。如何在 Python 中做到这一点,最好使用 skimage

解决方案

执行



请注意分段垃圾如何融合到右下角的对象,中间对象下部的小斗篷已关闭。在此之后,您可能希望继续进行形态学侵蚀或开孔。



编辑:对以下评论的长时间回应



disk(4)只是我用来产生图像中所示结果的示例。您将需要自己找到一个合适的值。太大的值将导致小物体融合到它们附近的大物体中,就像图像的右侧簇中那样。无论是否需要,它也将缩小对象之间的间隙。值太小将导致算法无法完成圆,因此填充操作将失败。



形态腐蚀会从结构中擦除结构元素大小的区域。对象的边框。形态学上的打开是闭合的逆运算,因此,代替膨胀->腐蚀,它将腐蚀->膨胀。打开的最终效果是,所有小于结构元素的物体和斗篷都将消失。如果在填充后执行此操作,则大对象将保持相对不变。理想情况下,它应该消除由于我在代码示例中使用的形态学关闭而导致的许多分割伪像,根据您的应用程序,它们可能与您相关或不相关。


I have a binary black and white images that looks like this

I want to fill in those white circles to be solid white disks. How can I do this in Python, preferrably using skimage?

解决方案

Do a morphological closing (explanation) to fill those tiny gaps, to complete the circles. Then fill the resulting binary image.

Code :

from skimage import io
from skimage.morphology import binary_closing, disk
import scipy.ndimage as nd
import matplotlib.pyplot as plt

# Read image, binarize
I = io.imread("FillHoles.png")
bwI =I[:,:,1] > 0

fig=plt.figure(figsize=(24, 8))

# Original image
fig.add_subplot(1,3,1)
plt.imshow(bwI, cmap='gray')

# Dilate -> Erode. You might not want to use a disk in this case,
# more asymmetric structuring elements might work better
strel = disk(4)
I_closed = binary_closing(bwI, strel)

# Closed image
fig.add_subplot(1,3,2)
plt.imshow(I_closed, cmap='gray')

I_closed_filled = nd.morphology.binary_fill_holes(I_closed)

# Filled image
fig.add_subplot(1,3,3)
plt.imshow(I_closed_filled, cmap='gray')

Result :

Note how the segmentation trash has melded to your object on the lower right and the small cape on the lower part of the middle object has been closed. You might want to continue with an morphological erosion or opening after this.

EDIT: Long response to comments below

The disk(4) was just the example I used to produce the results seen in the image. You will need to find a suitable value yourself. Too big of a value will lead to small objects being melded into bigger objects near them, like on the right side cluster in the image. It will also close gaps between objects, whether you want it or not. Too small of a value will lead to the algorithm failing to complete the circles, so the filling operation will then fail.

Morphological erosion will erase a structuring element sized zone from the borders of the objects. Morphological opening is the inverse operation of closing, so instead of dilate->erode it will do erode->dilate. The net effect of opening is that all objects and capes smaller than the structuring element will vanish. If you do it after filling then the large objects will stay relatively the same. Ideally it should remove a lot of the segmentation artifacts caused by the morphological closing I used in the code example, which might or might not be pertinent to you based on your application.

这篇关于图像处理-填充空心圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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