【发布时间】: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