2

我正在尝试使用分水岭分割来查找给定图像中的对象数量。例如考虑硬币图像。在这里,我想知道图像中的硬币数量。我实现了Scikit-image文档中可用的代码,并对其进行了一些调整,得到的结果与文档页面上显示的结果相似。

在详细查看代码中使用的函数后,我发现 ndimage.label() 还返回图像中找到的唯一对象的数量(在它的文档中提到),但是当我打印该值时,我得到 53,这是非常高的与实际图像中的硬币数量相比。

有人可以建议一些方法来查找图像中的对象数量。

4

1 回答 1

2

这是您的代码的一个版本,它以两种方式之一计算硬币:a) 通过直接分割距离图像和 b) 通过首先进行分水岭并拒绝微小的相交区域。

from __future__ import print_function

import numpy as np
import matplotlib.pyplot as plt
from skimage import io, color, filter as filters
from scipy import ndimage

from skimage.morphology import watershed
from skimage.feature import peak_local_max
from skimage.measure import regionprops, label

image = color.rgb2gray(io.imread('water_coins.jpg', plugin='freeimage'))
image = image < filters.threshold_otsu(image)

distance = ndimage.distance_transform_edt(image)

# Here's one way to measure the number of coins directly
# from the distance map
coin_centres = (distance > 0.8 * distance.max())
print('Number of coins (method 1):', np.max(label(coin_centres)))

# Or you can proceed with the watershed labeling
local_maxi = peak_local_max(distance, indices=False, footprint=np.ones((3, 3)),
                            labels=image)


markers, num_features = ndimage.label(local_maxi)
labels = watershed(-distance, markers, mask=image)

# ...but then you have to clean up the tiny intersections between coins
regions = regionprops(labels)
regions = [r for r in regions if r.area > 50]

print('Number of coins (method 2):', len(regions) - 1)

fig, axes = plt.subplots(ncols=3, figsize=(8, 2.7))
ax0, ax1, ax2 = axes

ax0.imshow(image, cmap=plt.cm.gray, interpolation='nearest')
ax0.set_title('Overlapping objects')
ax1.imshow(-distance, cmap=plt.cm.jet, interpolation='nearest')
ax1.set_title('Distances')
ax2.imshow(labels, cmap=plt.cm.spectral, interpolation='nearest')
ax2.set_title('Separated objects')

for ax in axes:
    ax.axis('off')

fig.subplots_adjust(hspace=0.01, wspace=0.01, top=1, bottom=0, left=0,
                    right=1)
plt.show()
于 2015-02-03T20:21:49.077 回答