高斯滤波后合并近物

时间:2016-12-17 04:27:44

标签: python python-3.x numpy image-processing computer-vision

我试图计算一个图像中的对象数量,并偶然发现了另一个问题中的以下代码,我将其应用于我的示例图像,如下所示:

import numpy
import matplotlib.pyplot as plt
from scipy import misc, ndimage
from skimage import feature
from skimage.filters import roberts, sobel

im = misc.imread('/home/nvp/temp/kaw.png',flatten=True)
im = im.astype('int32')
edges1 = feature.canny(im, sigma=3)
plt.imshow(edges1,interpolation='nearest')
dx = ndimage.sobel(im, 1)  # horizontal derivative
dy = ndimage.sobel(im, 0)  # vertical derivative
mag = numpy.hypot(dx, dy)  # magnitude
mag *= 255.0 / numpy.max(mag)
dna = mag
dnaf = ndimage.gaussian_filter(dna, 7)
T = 27 # set threshold by hand to avoid installing `mahotas` or
       # `scipy.stsci.image` dependencies that have threshold() functions

# find connected components
labeled, nr_objects = ndimage.label(dnaf > T) # `dna[:,:,0]>T` for red-dot case
print(dnaf, labeled,len(labeled))
print("Number of objects is %d " % nr_objects)

# show labeled image
####scipy.misc.imsave('labeled_dna.png', labeled)
####scipy.misc.imshow(labeled) # black&white image
import matplotlib.pyplot as plt
plt.imsave('labeled_dna.png', labeled)
plt.imshow(labeled)
plt.show()

但是它会输出以下图像:

Output

最终的对象存储在labeled np.array中我想,我想要做的是合并此输出数组中的近距离对象。如您所见,最后一行中的第二个对象有两个部分,但它们彼此非常接近。

由于我不知道numpy,我想要一种设置阈值并合并它们之间距离较小的对象的方法。任何帮助表示赞赏。谢谢:))

1 个答案:

答案 0 :(得分:1)

您想要合并的两个对象与第一行的对象3和4一样接近......因此,仅基于对象之间的接近度的解决方案不会为您提供所需的内容。

一个解决方案可能是根据区域dilate对象(扩大更多小物体)。

示例(未测试):

import scipy.ndimage.morphology as morpho

# parameters
max_dilat = 20 # dilation (in number of pixels) for a small object
sz_small = 100 # size of a small object (max dilated)
sz_big   = 10000 # size of a big object (not dilated)

result = labeled*0

# for each detected object
for obj_id in range(1, nr_objects+1):
    # creates a binary image with the current object
    obj_img = (labeled==obj_id)
    # computes object's area
    area = numpy.sum(obj_img)
    # dilatation factor inversely proportional to area
    dfac = int( max_dilat*(1-min(1,(max(0,area-sz_small)/sz_big))) )
    # dilates object
    dilat = morpho.binary_dilation(obj_img, iterations=dfac)

    result += dilat

# result is now an int array with ones where you have a single 
# dilated object, and twos (or more) in overlapping parts
labeled, nr_objects = ndimage.label(result>0)