赞
踩
import cv2 as cv
import numpy as np
import pyautogui
screen = r"E:\down\face\screenshort.png" ##屏幕截图保存路径
tp = r"E:\down\face\face.png" ##需要的匹配图片保存路径
def function_pipei(scshort, Tp): ##三种模板匹配方法匹配图片返回左上角坐标集合
target = cv.imread(scshort)
tpl = cv.imread(Tp)
methods = [cv.TM_SQDIFF_NORMED, cv.TM_CCORR_NORMED, cv.TM_CCOEFF_NORMED]
th, tw = tpl.shape[:2]
Tback = []
for md in methods:
result = cv.matchTemplate(target, tpl, md)
# print(result)
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result)
## print(min_val, max_val, min_loc, max_loc)
if md == cv.TM_SQDIFF_NORMED:
t1 = min_loc
else:
t1 = max_loc
## br = (t1[0] + tw, t1[1] + th)
# Cut1 = target
# Z1 = t1[0]
# Z2 = t1[0] + tw
# Z3 = t1[1]
# Z4 = t1[1] + th
# Cut1 = Cut1[Z3:Z4, Z1:Z2]
# cv.imwrite(r'E:\down\cat' + np.str(md) + '.jpg', Cut1)
# print(Cut1)
# cv.imshow('cut1'+np.str(md), Cut1)
# print("左上角坐标")
# print(t1)
# print("右下角坐标")
# print(br)
# cv.rectangle(target, t1, br, [0, 0, 0])
# cv.imshow("pipei" + np.str(md), target)
Tback.append(t1)
# print(Tback)
# break;
# cv.waitKey(2)
# cv.destroyAllWindows()
return Tback
Locimage = function_pipei(screen, tp) ##开始调用
##print(Locimage)
##print(np.mean(Locimage, axis=0)) ##显示集合平均坐标值
##print(Locimage[0])
if abs(sum(Locimage[0]) - sum(np.mean(Locimage, axis=0))) < 50: ##如果坐标集合与第一个方法差值不超过50
X_mv=np.mean(Locimage, axis=0)[0]
Y_mv = np.mean(Locimage, axis=0)[0]
pyautogui.moveTo(X_mv,Y_mv) ##移动鼠标到集合平均值位置上
else:
pyautogui.moveTo(Locimage[0]) ##否则移动到第一个方法的坐标上
# for Locimage in function_pipei(screen, tp):
# print(Locimage)
#print (Ifglobal)
# try: ##一直屏幕截图匹配目标图片
# while True:
# # pyautogui.screenshot(screen)
# Locimage = function_pipei(screen,tp)
# pyautogui.sleep(15)
# pyautogui.rightClick(Locimage)
#
# if Ifglobal-0.02 < 0 or 1 - Ifglobal < 0.02 :
# pyautogui.moveTo(Locimage)
# pyautogui.press('space')
# break;
# else:
# continue;
# except KeyboardInterrupt:
# print('\nexit')
一键复制
编辑
Web IDE
原始数据
按行查看
历史
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。