【发布时间】:2021-08-11 22:10:15
【问题描述】:
我正在尝试匹配以下图片
使用以下模板,使用cv2.matchTemplate
#elementachercher = path to template png file
#screenascanner = path to global picture where to find template
import pyautogui
import time
import cv2
import numpy as np
from matplotlib import pyplot as plt
import os
import imutils
def tryfoundobject(elementachercher, screenascanner):
img_rgb = cv2.imread(screenascanner)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread(elementachercher,0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
if pt != None:
print(elementachercher+" matched")
break
else:
continue
cv2.imwrite(elementachercher,img_rgb)
tryfoundobject("/home/noway/template.png", "/home/noway/mypicture.png")
但没有找到匹配项。
我做错了什么? 第一个屏幕是我想要的结果,第二个屏幕是我在脚本之后得到的结果
【问题讨论】:
-
请解释您的期望和没有得到什么
-
我正在尝试在大图上使用matchtemplate,小图是我获取模板坐标的模板,其中X,Y但是opencv从不匹配我在主图上的模板
-
会发生什么?请编辑问题以显示
res(首先标准化为[0,255]),然后显示loc。 -
这可能只是缩进错误吗?
-
我编辑了我的帖子,以展示我想用屏幕做什么、我想要什么以及我得到什么
标签: python python-3.x numpy opencv