您的位置:首页 > 教育 > 锐评 > 足球比赛直播平台app免费_网店美工与视觉设计_培训心得体会范文500字_搜索引擎营销成功的案例

足球比赛直播平台app免费_网店美工与视觉设计_培训心得体会范文500字_搜索引擎营销成功的案例

2025/5/9 0:19:23 来源:https://blog.csdn.net/huweijun_2012/article/details/145078791  浏览:    关键词:足球比赛直播平台app免费_网店美工与视觉设计_培训心得体会范文500字_搜索引擎营销成功的案例
足球比赛直播平台app免费_网店美工与视觉设计_培训心得体会范文500字_搜索引擎营销成功的案例

在windows下面,使用python opencv 进行识别,获取到坐标。

依赖安装:


pip install opencv-python
pip install numpy
pip install pyautogui
pip install pywin32

代码:

import cv2
import numpy as np
import pyautogui
import os
import sysdef resource_path(relative_path):""" 获取资源的绝对路径 """try:# PyInstaller 创建的临时文件夹,或者当以 --onefile 模式打包时的路径base_path = sys._MEIPASSexcept Exception:# 如果不是打包后的环境,则使用当前工作目录base_path = os.path.abspath(".")rp = os.path.join(base_path, relative_path)return rpdef preprocess_image(image):"""将图像转换为8位无符号整数类型的灰度图像"""gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)return cv2.convertScaleAbs(gray)def find_image_on_screen(template_path, scale_range=(0.5, 2.0), step=0.1):# 获取屏幕截图并预处理screen = pyautogui.screenshot()screen_np = np.array(screen)screen_gray = preprocess_image(screen_np)# 读取模板图像并预处理template = cv2.imread(template_path, cv2.IMREAD_GRAYSCALE)if template is None:raise ValueError("无法加载模板图像")template = cv2.convertScaleAbs(template)found = Nonefor scale in np.arange(scale_range[0], scale_range[1] + step, step):resized = cv2.resize(screen_gray, (0, 0), fx=scale, fy=scale)if resized.shape[0] < template.shape[0] or resized.shape[1] < template.shape[1]:breakres = cv2.matchTemplate(resized, template, cv2.TM_CCOEFF_NORMED)min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)if found is None or max_val > found[0]:found = (max_val, max_loc, scale)if found:_, max_loc, scale = foundtop_left = (int(max_loc[0] / scale), int(max_loc[1] / scale))bottom_right = (int((max_loc[0] + template.shape[1]) / scale), int((max_loc[1] + template.shape[0]) / scale))center = ((top_left[0] + bottom_right[0]) // 2, (top_left[1] + bottom_right[1]) // 2)return centerelse:return None# 使用函数
template_path = resource_path('send_chat2.png')
pyautogui.sleep(2)
center = find_image_on_screen(template_path)
if center:print(f"Found image at: {center}")pyautogui.moveTo(center[0], center[1])
else:print("Image not found.")

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com