【问题标题】:Merging close objects after gaussian filter高斯滤波后合并近物体
【发布时间】:2016-12-17 04:27:44
【问题描述】:

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

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()

但是它输出以下图像:

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

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

【问题讨论】:

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


    【解决方案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)
    

    【讨论】:

    • 能否请您详细说明一下并展示一个我可以遵循的示例?
    • 我添加了伪代码,我现在无法测试它,但它应该可以工作。不要犹豫,使用参数来达到预期的结果。
    • 您好,我将值更改为max_dilat=9, sz_small=90, sz_big=10000,结果如下:results,您可以看到其中一个对象在扩张后消失了,您知道如何应对吗?非常感谢!
    • 我的错,最后一个对象没有被处理(对于 obj_id in range(1, nr_objects +1))
    • 谢谢,我会尽快接受答案,感谢您的帮助:)
    猜你喜欢
    • 2018-10-03
    • 1970-01-01
    • 2013-03-29
    • 2018-01-26
    • 2013-02-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    • 2014-10-02
    相关资源
    最近更新 更多