当前位置: 首页 > news >正文

PythonOpenCV图片识别

在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.")
http://www.lryc.cn/news/521265.html

相关文章:

  • 构建优雅、高效的 Nodejs 命令行工具 - Archons
  • 你喜欢用什么编辑器?
  • 鸿蒙报错Init keystore failed: keystore password was incorrect
  • 【Flink】Flink内存管理
  • JVM:ZGC详解(染色指针,内存管理,算法流程,分代ZGC)
  • Docker常用命令大全
  • (12)springMVC文件的上传
  • 在Linux系统中无网络安装Nginx并配置负载均衡
  • Android车机DIY开发之软件篇(三)编译Automotive OS错误(1)
  • 基于网络爬虫技术的网络新闻分析【源码+文档+部署讲解】
  • uniapp区域滚动——上划进行分页加载数据(详细教程)
  • 机器学习(1):线性回归概念
  • 关于编写测试用例的细枝末节
  • 《计算机网络》课后探研题书面报告_了解PPPoE协议
  • Linux Centos 安装Jenkins到服务
  • 解决“无法定位程序输入点 av_buffer_create 于动态链接库 XXX\Obsidian.exe 上”问题
  • 基于考研概率论知识解读 Transformer:为何自注意力机制要除以根号 dk
  • 网络安全学习81天(记录)
  • MATLAB学习笔记-table
  • mybatisPlus(条件构造器API)
  • 5G+工业互联网迈入规模化发展新阶段
  • 【CI/CD构建】关于不小心将springMVC注解写在service层
  • 《鸿蒙Next ArkTS:开启人工智能应用开发高效新旅程》
  • Unity 3D游戏开发从入门进阶到高级
  • 什么是SSH登录?SSH客户端软件有哪些?
  • Ubuntu服务器提示:检测到存在恶意文件,补救思路
  • 【学习计算机视觉算法的基础及基本编码-基于Python语言--实例教程】
  • 从零搭建一个Vue3 + Typescript的脚手架——day1
  • Pgsql存储占用分析
  • 51c自动驾驶~合集46