我需要使用python填充图像中的空洞。 这是我设法得到的对象的图像 - 它们确实是我想要的对象的边缘,所以我需要填充它们。 在图像中填充空洞
这似乎非常简单使用ndimage.binary_fill_holes(A)
,但问题是,它会产生这样(手动填充红色):
但我需要这样的:
任何方式,这可以解决?
我需要使用python填充图像中的空洞。 这是我设法得到的对象的图像 - 它们确实是我想要的对象的边缘,所以我需要填充它们。 在图像中填充空洞
这似乎非常简单使用ndimage.binary_fill_holes(A)
,但问题是,它会产生这样(手动填充红色):
但我需要这样的:
任何方式,这可以解决?
我想我已经找到了解决办法。由于时间不够,这有点漫长,但也许有帮助。我已经编码,如果只有这个问题,但应该很容易推广它的许多图像。
一些命名约定第一:
我的基本想法是比较两个子区域轮廓的长度,这两个子区域是一个关键区域的一部分。然而,我并没有比较它们的完整轮廓长度,而只是接近背景的部分。具有较短轮廓段的靠近背景的那个被认为是一个洞。
我先从结果图像开始。
我们所谈论的,vizualizing上面的命名约定的一些概述:
临界区域的两个分区域。每个接近背景的区域的两个边界线段用不同的颜色标记(非常薄,蓝色和深红色,但可见)。这些片段显然不是完美的(“瘦”的地区造成的错误),但足以来比较它们的长度:
的最终结果。如果你想让洞“关闭”,让我知道,你只需将原始的黑色轮廓分配给区域而不是背景([编辑]我已经包括三个标记的代码行,分配的边界该地区,你想):
代码接在这里。我已经使用了OpenCV轮廓功能,它非常简洁,还有一些屏蔽技术。由于其可视化,该代码很流行,对于其可读性有限而感到遗憾,但似乎没有解决这个问题的两种方法。一些最后的评论:我首先尝试使用一组点来进行轮廓匹配,这将避免循环,并允许使用set.intersection来确定靠近背景的两个轮廓线段,但由于您的黑色线条比较厚,轮廓很不协调。我尝试了轮廓的镂空,但是打开了另一个蠕虫的罐头,所以我使用了转储方法来完成轮廓点之间的循环和计算距离。做这部分可能有更好的方法,但它可行。
我也考虑过使用Shapely模块,可能有某些方法从中获得一些优势,但是我没有找到任何方法,所以我再次丢弃它。
import numpy as np
import scipy.ndimage as ndimage
from matplotlib import pyplot as plt
import cv2
img= ndimage.imread('image.png')
# Label digfferentz original regions
labels, n_regions = ndimage.label(img)
print "Original number of regions found: ", n_regions
# count the number of pixels in each region
ulabels, sizes = np.unique(labels, return_counts=True)
print sizes
# Delete all regions with size < 2 and relabel
mask_size = sizes < 2
remove_pixel = mask_size[labels]
labels[remove_pixel] = 0
labels, n_regions = ndimage.label(labels) #,s)
print "Number of regions found (region size >1): ", n_regions
# count the number of pixels in each region
ulabels, sizes = np.unique(labels, return_counts=True)
print ulabels
print sizes
# Determine large "first level" regions
first_level_regions=np.where(labels ==1, 0, 1)
labeled_first_level_regions, n_fl_regions = ndimage.label(first_level_regions)
print "Number of first level regions found: ", n_fl_regions
# Plot regions and first level regions
fig = plt.figure()
a=fig.add_subplot(2,3,1)
a.set_title('All regions')
plt.imshow(labels, cmap='Paired', vmin=0, vmax=n_regions)
plt.xticks([]), plt.yticks([]), plt.colorbar()
a=fig.add_subplot(2,3,2)
a.set_title('First level regions')
plt.imshow(labeled_first_level_regions, cmap='Paired', vmin=0, vmax=n_fl_regions)
plt.xticks([]), plt.yticks([]), plt.colorbar()
for region_label in range(1,n_fl_regions):
mask= labeled_first_level_regions!=region_label
result = np.copy(labels)
result[mask]=0
subregions = np.unique(result).tolist()[1:]
print region_label, ": ", subregions
if len(subregions) >1:
print " Element 4 is a critical element: ", region_label
print " Subregions: ", subregions
#Critical first level region
crit_first_level_region=np.ones(labels.shape)
crit_first_level_region[mask]=0
a=fig.add_subplot(2,3,4)
a.set_title('Crit. first level region')
plt.imshow(crit_first_level_region, cmap='Paired', vmin=0, vmax=n_regions)
plt.xticks([]), plt.yticks([])
#Critical Region Contour
im = np.array(crit_first_level_region * 255, dtype = np.uint8)
_, contours0, hierarchy = cv2.findContours(im.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
crit_reg_contour = [contours0[0].flatten().tolist()[i:i+2] for i in range(0, len(contours0[0].flatten().tolist()), 2)]
print crit_reg_contour
print len(crit_reg_contour)
#First Subregion
mask2= labels!=subregions[1]
first_subreg=np.ones(labels.shape)
first_subreg[mask2]=0
a=fig.add_subplot(2,3,5)
a.set_title('First subregion: '+str(subregions[0]))
plt.imshow(first_subreg, cmap='Paired', vmin=0, vmax=n_regions)
plt.xticks([]), plt.yticks([])
#First Subregion Contour
im = np.array(first_subreg * 255, dtype = np.uint8)
_, contours0, hierarchy = cv2.findContours(im.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
first_sub_contour = [contours0[0].flatten().tolist()[i:i+2] for i in range(0, len(contours0[0].flatten().tolist()), 2)]
print first_sub_contour
print len(first_sub_contour)
#Second Subregion
mask3= labels!=subregions[0]
second_subreg=np.ones(labels.shape)
second_subreg[mask3]=0
a=fig.add_subplot(2,3,6)
a.set_title('Second subregion: '+str(subregions[1]))
plt.imshow(second_subreg, cmap='Paired', vmin=0, vmax=n_regions)
plt.xticks([]), plt.yticks([])
#Second Subregion Contour
im = np.array(second_subreg * 255, dtype = np.uint8)
_, contours0, hierarchy = cv2.findContours(im.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
second_sub_contour = [contours0[0].flatten().tolist()[i:i+2] for i in range(0, len(contours0[0].flatten().tolist()), 2)]
print second_sub_contour
print len(second_sub_contour)
maxdist=6
print "Points in first subregion close to first level contour:"
close_1=[]
for p1 in first_sub_contour:
for p2 in crit_reg_contour:
if (abs(p1[0]-p2[0])+abs(p1[1]-p2[1]))<maxdist:
close_1.append(p1)
break
print close_1
print len(close_1)
print "Points in second subregion close to first level contour:"
close_2=[]
for p1 in second_sub_contour:
for p2 in crit_reg_contour:
if (abs(p1[0]-p2[0])+abs(p1[1]-p2[1]))<maxdist:
close_2.append(p1)
break
print close_2
print len(close_2)
for p in close_1:
result[p[1],p[0]]=1
for p in close_2:
result[p[1],p[0]]=2
if len(close_1)>len(close_2):
print "first subregion is considered a hole:", subregions[0]
hole=subregions[0]
else:
print "second subregion is considered a hole:", subregions[1]
hole=subregions[1]
#Plot Critical region with subregions
a=fig.add_subplot(2,3,3)
a.set_title('Critical first level region with subregions')
plt.imshow(result, cmap='Paired', vmin=0, vmax=n_regions)
plt.xticks([]), plt.yticks([])
result2=result.copy()
#Plot result
fig2 = plt.figure()
a=fig2.add_subplot(1,1,1)
a.set_title('Critical first level region with subregions and bordering contour segments')
plt.imshow(result2, cmap='flag', vmin=0, vmax=n_regions)
plt.xticks([]), plt.yticks([])
#Plot result
mask_hole=np.where(labels ==hole, True, False)
labels[mask_hole]=1
labels=np.where(labels > 1, 2, 1)
# [Edit] Next two lines include black borders into final result
mask_borders=np.where(img ==0, True, False)
labels[mask_borders]=2
fig3 = plt.figure()
a=fig3.add_subplot(1,1,1)
a.set_title('Final result')
plt.imshow(labels, cmap='flag', vmin=0, vmax=n_regions)
plt.xticks([]), plt.yticks([])
plt.show()
不错的问题。以为你可以用OpenCV的轮廓层次结构来解决它,因为这里描述了http://docs.opencv.org/3.1.0/d9/d8b/tutorial_py_contours_hierarchy.html#gsc.tab=0,但是这不起作用,因为临界轮廓是没有真正的孩子。你可以很快看到这不起作用使用示例文件在https://github.com/Itseez/opencv/blob/master/samples/python/contours.py – tfv
谢谢,@tfv!否定的结果也是一个结果... – Phlya
我想说你给的图像不允许做出你正在寻找的决定。有一个边界(如果算法预计会得到你想要的结果,那么就需要去掉边界),当你穿过另一个边界时,你仍然处在一个洞的内部,这对所有人都没有意义正常情况下。 – roadrunner66