微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

计算模板匹配检测到的对象数量

如何解决计算模板匹配检测到的对象数量

首先我知道这个线程存在:

How to count the number of objects detected with Template Matching?

但我不知道为什么给出的解决方案在我的应用程序中不起作用

我已关注此http://docs.opencv.org/3.1.0/d4/dc6/tutorial_py_template_matching.html

我想做什么:

https://i.imgur.com/vc2jXu0.png 我想要这张图片中的内容

https://i.imgur.com/BbKKCjv.png 所有这些符号都被计算在内。

Atm,我只成功地将它们染成蓝色:

import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt

img_rgb = cv.imread('plan3.png')
img_gray = cv.cvtColor(img_rgb,cv.COLOR_BGR2GRAY)
template = cv.imread('template2.png',0)
w,h = template.shape[::-1]
res = cv.matchTemplate(img_gray,template,cv.TM_CCOEFF_norMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
    cv.rectangle(img_rgb,pt,(pt[0] + w,pt[1] + h),(0,255),2)

cv.imwrite('res.png',img_rgb)
plt.imshow(img_rgb)
plt.show()'

https://i.imgur.com/iE10p1e.png 它给了我

所以我认为我还没有做我想做的事情,但是当我尝试在我提到的线程上调整代码时,它不起作用。

我尝试添加

 found_count = len(loc)
print(len(loc))

但它什么也没做,没有错误

我确实是个新手

预先感谢您的帮助!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。