opencv - 使用分水岭算法计算对象数量 - Scikit-image

标签 opencv image-processing scikit-image watershed

我正在尝试使用分水岭分割查找给定图像中的对象数量。例如考虑 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/

相关文章:

qt - 如何将 cv::Mat 转换为 QImage 或 QPixmap?

python - 图像的 3d 旋转

python - cv2.remap 或 scipy.interpolate.map_coordinates 在 Tensorflow 中的等效/实现?

python - 使用修正的Hausdorff距离查找形状

python - 将 PIL.Image 转换为 skimage

python - 在skimage中绘制渐变椭圆

python - skimage - 导入错误 : DLL load failed: The specified module could not be found

javascript - requestAnimFrame 被多次执行

c++ - CvCapture结构和VideoCapture结构有什么区别?

c++ - OpenCV 调整质量