gpt4 book ai didi

opencv - 使用分水岭算法对对象数进行计数-Scikit-Image

转载 作者:行者123 更新时间:2023-12-02 16:21:46 26 4
gpt4 key购买 nike

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

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

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

最佳答案

这是您的代码的一种版本,该代码以两种方式之一对硬币进行计数: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()

关于opencv - 使用分水岭算法对对象数进行计数-Scikit-Image,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28242274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com