【问题标题】:Count number of objects using watershed algorithm - Scikit-image使用分水岭算法计算对象数量 - Scikit-image
【发布时间】:2015-03-30 07:20:05
【问题描述】:

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

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

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

【问题讨论】:

  • 你能提供一个示例图像吗?
  • 我已在问题本身中附加了输入图像的链接。最终结果可在link 获得。这是代码link

标签: opencv image-processing scikit-image watershed


【解决方案1】:

这是您的代码版本,它通过以下两种方式之一计算硬币: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()

【讨论】:

    猜你喜欢
    • 2021-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多