gpt4 book ai didi

python - 检查模板匹配为True OpenCV

转载 作者:行者123 更新时间:2023-12-02 16:37:30 25 4
gpt4 key购买 nike

如何检查是否找到模板并将其返回为true或false?以帧为单位,当对象丢失时,我希望该值变为false,否则您会知道。这是我的脚本的样子:

zmatch = cv2.matchTemplate(gray_frame, template2, cv2.TM_CCOEFF_NORMED)
loc = np.where(zmatch >= threshold)

for pt in zip(*loc[::-1]):
cv2.rectangle(gray_frame, pt, (pt[0]+w, pt[1]+ h), (0, 225, 0), 2)

我尝试了所有可以通过谷歌搜索找到的解决方案,没有帮助。
-检查pt是否有效(始终有效)
-检查zmatch.any()是否大于或小于阈值

我不知道该如何处理,请帮忙! :3

最佳答案

我知道这有点陈旧,但是对于用谷歌搜索的人来说,这是我用来解决该问题的解决方案(如果您不希望看到我的bs解释,则该解决方案位于底部):
这是opencv website中用于matchTemplate的代码:

res = cv.matchTemplate(img_gray,template,cv.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
print(pt) #this line I added myself to see the print result of pt
cv.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
现在,它找到了比赛的位置,我发现的位置的长度始终为正,并且不会真正根据比赛的数量而变化(至少在经过测试的情况下是有限的)。
它似乎是存储不同点的位置(可能不是一个)的各种数组(我不太了解zip部分或如何获取点,但确实如此)。
但是,pt部分(我从打印结果中得出的假设是大(原始,干草堆,无论您叫什么)图像上的匹配项的原点坐标),它将从无更改为某些内容。
解决方案
这是添加一个if语句来检查 if pt != None:
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,0,255), 2)
if pt != None:
#the things you want to do
break #to allow the else function to work
else:
#the things you want it to do when no template is detected

希望它能解决您的问题

关于python - 检查模板匹配为True OpenCV,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52595273/

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