Bri*_*ian 5 python numpy image image-processing scipy
我试图填补二进制图像中的漏洞.图像相当大,所以我把它分成几块进行处理.
当我使用这些scipy.ndimage.morphology.binary_fill_holes功能时,它会填充属于图像的较大孔.所以我尝试使用scipy.ndimage.morphology.binary_closing,它给出了在图像中填充小孔的理想结果.但是,当我将块重新组合在一起时,为了创建整个图像,我最终得到了接缝线,因为该binary_closing函数会从每个块的边框像素中删除任何值.
有没有办法避免这种影响?
是的。
ndimage.label(首先反转图像,孔=黑色)标记您的图像。ndimage.find_objectsbinary_fill_holes在符合您标准的切片上执行。这应该可以做到,而无需将图像切碎。例如:
输入图像:

输出图像(中等大小的洞消失了):

这是代码(不等式设置为删除中等大小的斑点):
import scipy
from scipy import ndimage
import numpy as np
im = scipy.misc.imread('cheese.png',flatten=1)
invert_im = np.where(im == 0, 1, 0)
label_im, num = ndimage.label(invert_im)
holes = ndimage.find_objects(label_im)
small_holes = [hole for hole in holes if 500 < im[hole].size < 1000]
for hole in small_holes:
a,b,c,d = (max(hole[0].start-1,0),
min(hole[0].stop+1,im.shape[0]-1),
max(hole[1].start-1,0),
min(hole[1].stop+1,im.shape[1]-1))
im[a:b,c:d] = scipy.ndimage.morphology.binary_fill_holes(im[a:b,c:d]).astype(int)*255
Run Code Online (Sandbox Code Playgroud)
另请注意,我必须增加切片的大小,以便孔始终具有边框。
| 归档时间: |
|
| 查看次数: |
1975 次 |
| 最近记录: |