opencv 大图(当前页面)找小图(需要点击的地方),主要方法(cv.matchTemplate)
import aircv as ac
from PIL import ImageGrab
import win32api
import win32con
from ctypes import *
import cv2 as cv
def draw_circle(pos, circle_radius, color, line_width, src_path):
imsrc = ac.imread(src_path)
cv.circle(imsrc, pos, circle_radius, color, line_width)
cv.imshow(‘objDetect’, imsrc)
cv.waitKey(0)
cv.destroyAllWindows()
def find_image_cv(obj_path, src_path):
# basefolder = os.path.abspath(’.’) + “\source\”
#ImageGrab.grab().save(src_path)
source = cv.imread(src_path)
template = cv.imread(obj_path)
result = cv.matchTemplate(source, template, cv.TM_CCOEFF_NORMED)
print(result)
pos_start = cv.minMaxLoc(result)[3]
test = cv.minMaxLoc(result)
print(test)
print(pos_start)
x = int(pos_start[0]) + int(template.shape[1] / 2)
y = int(pos_start[1]) + int(template.shape[0] / 2)
similarity = cv.minMaxLoc(result)[1]
if similarity < 0.85:
return (-1, -1)
else:
print(“pass”)
return [(x, y),(x, y)]
def double_click(obj_path, src_path):
p, q = find_image_cv(obj_path, src_path)
x, y = p
windll.user32.SetCursorPos(x, y)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
def main1():
#调试
circle_radius = 50
color = (0, 255, 0)
line_width = 10
src_path = ‘./pic/destop.png’
obj_path = ‘./pic/action.png’
p, circle_center_pos = find_image_cv(obj_path, src_path)
print(circle_center_pos)
draw_circle(circle_center_pos, circle_radius, color, line_width, src_path) #画圈
def main2():
#找到并双击
src_path = ‘./pic/destop.png’(当前页面)
obj_path = ‘./pic/action.png’ (需要点击的地方)
print (find_image_cv(obj_path, src_path))
double_click(obj_path, src_path)
if name == “main”:
#main1()
main2()
————————————————
版权声明:本文为CSDN博主「流氓也是种气质 _Cookie」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_37550440/article/details/84134424