我有水滴图像,我想计算它的数量。
我在 OpenCV 上尝试了很多功能,但总是不正确。
您对如何做有什么想法吗?
谢谢
我得到的最好的结果是使用: (img_morph 是我的二值化图像)
rbc_bw = label(img_morph)
rbc_props = regionprops(rbc_bw)
fig, ax = plt.subplots(figsize=(18, 8))
ax.imshow(img_morph)
rbc_count = 0
for i, prop in enumerate(filter(lambda x: x.area > 250, rbc_props)):
y1, x1, y2, x2 = (prop.bbox[0], prop.bbox[1],
prop.bbox[2], prop.bbox[3])
width = x2 - x1
height = y2 - y1
r = plt.Rectangle((x1, y1), width = width, height=height,
color='b', fill=False)
ax.add_patch(r)
rbc_count += 1
print('Red Blood Cell Count:', rbc_count)
plt.show()
最佳答案
核心思想:matchTemplate
。
方法:
- 从图片中手动选择模板
- 光线不足的输入(或始终)的直方图均衡
matchTemplate
具有合适的匹配模式- 还使用
copyMakeBorder
来捕获剪切边框的实例
- 还使用
- 阈值和非极大值抑制
我将跳过无聊的部分并使用第一个示例输入。
手动选取模板:
scores = cv.matchTemplate(haystack, template, cv.TM_CCOEFF_NORMED)
阈值和 NMS:
levelmask = (scores >= 0.3)
localmax = cv.dilate(scores, None, iterations=26)
localmax = (scores == localmax)
candidates = levelmask & localmax
(nlabels, labels, stats, centroids) = cv.connectedComponentsWithStats(candidates.astype(np.uint8), connectivity=8)
print(nlabels-1, "found") # background counted too
# and then draw a circle for each centroid except label 0
这找到了 766 个实例。我看到了一些假阴性(遗漏),也看到过一次假阳性,但看起来不到 1%。
关于python - 计算图像上的水滴数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74633013/