赞
踩
- import threading
- import cv2 # opencv库
- import numpy as np # 将图片转换成数组,opencv无法直接处理mss的截图,用它进行转换
- import mss # 截屏库
- from concurrent.futures import ThreadPoolExecutor
- import os
-
-
- def findpic(x1, y1, x2, y2, lujin, xiangsidu):
- chang = x2 - x1
- kuan = y2 - y1
- quyu = {"left": x1, "top": y1, "width": chang, "height": kuan}
- with mss.mss() as sct:
- # 获取大图片
- quyujieping = sct.grab(quyu) # 对屏幕的左上角800*600进行截图
- quyujieping = np.array(quyujieping) # 调用numpy库将mss库的截屏转换一下,并赋值给变量quyujieping
- datupian_huidu = cv2.cvtColor(quyujieping, cv2.COLOR_BGR2GRAY)
-
- xiaotupian = cv2.imread(lujin)
- xiaotupian_huidu = cv2.cvtColor(xiaotupian, cv2.COLOR_BGR2GRAY)
-
- # 在大图片中查找小图片
- result = cv2.matchTemplate(datupian_huidu, xiaotupian_huidu, cv2.TM_CCOEFF_NORMED)
-
- # 筛选结果
- locations = np.where(result >= xiangsidu) # 用numpy筛选一下结果高于0.85的
- locations = list(zip(*locations[::-1])) # 将结果再次处理一下,仅保留坐标
-
- if len(locations) > 0:
- return (locations[0][0] + x1, locations[0][1] + y1)
- return (-1, -1)
- def zuijin_zuobiao(coordinates_list, given_coordinate):
- # 确保给定的坐标包含两个元素
- if len(given_coordinate) != 2:
- raise ValueError("给定的坐标必须包含两个元素 (x, y)")
-
- # 初始化最小距离和对应坐标的坐标
- min_distance = float('inf') # 使用正无穷大作为初始值
- zuijin_zuobiao = None
-
- # 计算曼哈顿距离并找到最近坐标
- for coord in coordinates_list:
- # 确保列表中的每个坐标也包含两个元素
- if len(coord) != 2:
- raise ValueError("坐标列表中的每个坐标必须包含两个元素 (x, y)")
-
- x1, y1 = given_coordinate
- x2, y2 = coord
- distance = abs(x1 - x2) + abs(y1 - y2)
-
- # 如果是第一个坐标或者距离更近,则更新最小距离和最近距离的坐标
- if distance < min_distance:
- min_distance = distance
- zuijin_zuobiao = coord
- if zuijin_zuobiao==None:
- return -1,-1
- else:
- return zuijin_zuobiao
- def chazhao_tupian(datupian_huidu, xiaotupian_path, xiangsidu):
- xiaotupian = cv2.imread(xiaotupian_path)
- if xiaotupian is None:
- return [] # 返回空列表表示未找到图片或路径错误
- xiaotupian_huidu = cv2.cvtColor(xiaotupian, cv2.COLOR_BGR2GRAY)
- result = cv2.matchTemplate(datupian_huidu, xiaotupian_huidu, cv2.TM_CCOEFF_NORMED)
- locations = np.where(result >= xiangsidu)
- locations = list(zip(*locations[::-1]))
- return [(loc[0] + int(os.path.splitext(os.path.basename(xiaotupian_path))[0].split("_")[1]),
- loc[1] + int(os.path.splitext(os.path.basename(xiaotupian_path))[0].split("_")[2]))
- for loc in locations]
- def findpicE(x1, y1, x2, y2, liebiao_dir, xiangsidu):
- chang = x2 - x1
- kuan = y2 - y1
- quyu = {"left": x1, "top": y1, "width": chang, "height": kuan}
-
- with mss.mss() as sct:
- quyujieping = sct.grab(quyu)
- quyujieping = np.array(quyujieping)
- datupian_huidu = cv2.cvtColor(quyujieping, cv2.COLOR_BGR2GRAY)
-
- # 使用线程池并行处理图片匹配
- with ThreadPoolExecutor() as executor:
- futures = []
- for xiaotupian_name in os.listdir(liebiao_dir):
- xiaotupian_path = os.path.join(liebiao_dir, xiaotupian_name)
- future = executor.submit(chazhao_tupian, datupian_huidu, xiaotupian_path, xiangsidu)
- futures.append(future)
-
- # 收集所有线程的结果
- zuobiao_liebiao = []
- for future in futures:
- zuobiao_liebiao.extend(future.result())
- juese_zuobiao = (0,0) # 假设这个函数已定义并返回角色坐标
- weiyi_zuobiao = zuijin_zuobiao(zuobiao_liebiao, juese_zuobiao) # 假设这个函数已定义并返回最近坐标
- if weiyi_zuobiao[0]>0:
- return weiyi_zuobiao
- return -1,-1
- def findpicEX(x1, y1, x2, y2, liebiao_dir, xiangsidu):
- chang = x2 - x1
- kuan = y2 - y1
- quyu = {"left": x1, "top": y1, "width": chang, "height": kuan}
- with mss.mss() as sct:
- quyujieping = sct.grab(quyu)
- quyujieping = np.array(quyujieping)
- datupian_huidu = cv2.cvtColor(quyujieping, cv2.COLOR_BGR2GRAY)
-
- for xiaotupian_mingcheng in os.listdir(liebiao_dir):
- if not xiaotupian_mingcheng.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
- continue # 跳过非图片文件
- xiaotupian_path = os.path.join(liebiao_dir, xiaotupian_mingcheng)
- xiaotupian = cv2.imread(xiaotupian_path)
- if xiaotupian is None:
- continue # 跳过无法读取的图片
- xiaotupian_huidu = cv2.cvtColor(xiaotupian, cv2.COLOR_BGR2GRAY)
- result = cv2.matchTemplate(datupian_huidu, xiaotupian_huidu, cv2.TM_CCOEFF_NORMED)
- locations = np.where(result >= xiangsidu)
- if locations is not None and len(locations[0]) > 0:
- return (locations[0][0] + x1, locations[1][0] + y1) # 注意这里要分别取locations[0]和locations[1]
- return (-1, -1)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。