当前位置:网站首页>基于opencv实现桌面图标识别
基于opencv实现桌面图标识别
2022-07-03 09:03:00 【蒙蒂锅巴】
一、必要的库
numy和opencv。直接使用pip安装即可;
二、在指定区域截图
直接贴代码:
from ctypes import windll, byref, c_ubyte
from ctypes.wintypes import RECT, HWND
import numpy as np
import time
from win32com.client import Dispatch
# import mouse as mouse
GetDC = windll.user32.GetDC
CreateCompatibleDC = windll.gdi32.CreateCompatibleDC
GetClientRect = windll.user32.GetClientRect
CreateCompatibleBitmap = windll.gdi32.CreateCompatibleBitmap
SelectObject = windll.gdi32.SelectObject
BitBlt = windll.gdi32.BitBlt
SRCCOPY = 0x00CC0020
GetBitmapBits = windll.gdi32.GetBitmapBits
DeleteObject = windll.gdi32.DeleteObject
ReleaseDC = windll.user32.ReleaseDC
# 防止UI放大导致截图不完整
#windll.user32.SetProcessDPIAware()
def capture(handle: HWND):
"""窗口客户区截图
Args:
handle (HWND): 要截图的窗口句柄
Returns:
numpy.ndarray: 截图数据
"""
# 获取窗口客户区的大小
r = RECT()
GetClientRect(handle, byref(r))
width, height = r.right, r.bottom
print("width,height:", width, height)
# 开始截图
dc = GetDC(handle)
cdc = CreateCompatibleDC(dc)
# 内存中创建,防止屏幕在截图的时候闪烁,同时,如果知道需要识别的图标的大致范围,则可以
# 在这个地方将width和height改成对应区域的宽度和高度信息
bitmap = CreateCompatibleBitmap(dc, width, height)
SelectObject(cdc, bitmap)
# 这里可以设置截图的区域,要和上面的宽度和高度对应,后续的宽度和高度都要对应起来,否则会不匹配
BitBlt(cdc, 0, 0, width, height, dc, 0, 0, SRCCOPY)
# 截图是BGRA排列,因此总元素个数需要乘以4
total_bytes = width*height*4
buffer = bytearray(total_bytes)
byte_array = c_ubyte*total_bytes
GetBitmapBits(bitmap, total_bytes, byte_array.from_buffer(buffer))
DeleteObject(bitmap)
DeleteObject(cdc)
ReleaseDC(handle, dc)
# 返回截图数据为numpy.ndarray
return np.frombuffer(buffer, dtype=np.uint8).reshape( width, height, 4)
if __name__ == "__main__":
import cv2
op=Dispatch("op.opsoft")
handle = windll.user32.FindWindowW(None, "这里是窗口句柄或者名字")
# 截图时要保证游戏窗口的客户区大小是1334×750
print(handle)
image = capture(handle)
# 转为灰度图
gray = cv2.cvtColor(image, cv2.COLOR_BGRA2GRAY)
# 读取图片,并保留Alpha通道,若要排除背景干扰,需要将源对比图背景设置成透明的,即Alpha通道置为0.
#这里替换成自己的想要抓的图,可以是jpg格式的截图
template = cv2.imread('23.PNG', cv2.IMREAD_UNCHANGED)
# 转为灰度图
template_gray = cv2.cvtColor(template, cv2.COLOR_BGRA2GRAY)
# 取出Alpha通道
alpha = template[:,:,3]
# 模板匹配,将alpha作为mask,TM_CCORR_NORMED方法的计算结果范围为[0, 1],越接近1越匹配 TM_CCOEFF_NORMED
result = cv2.matchTemplate(gray, template_gray, cv2.TM_CCOEFF_NORMED,mask=alpha)
# TM_SQDIFF 平方差匹配法:该方法采用平方差来进行匹配;最好的匹配值为0;匹配越差,匹配值越大。
# TM_CCORR 相关匹配法:该方法采用乘法操作;数值越大表明匹配程度越好。
# TM_CCOEFF 相关系数匹配法:1表示完美的匹配;-1表示最差的匹配。
# TM_SQDIFF_NORMED 归一化平方差匹配法
# TM_CCORR_NORMED 归一化相关匹配法
# TM_CCOEFF_NORMED 归一化相关系数匹配法
# result = cv2.matchTemplate(gray, template, cv2.TM_CCOEFF_NORMED)
print(cv2.minMaxLoc(result))
# 获取结果中最大值和最小值以及他们的坐标
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
# 匹配最大值的坐标,左上角
top_left = max_loc
print("左上角坐标值:",top_left[0],top_left[1])
# 模板的高度和宽度
h, w = template.shape[:2]
# 右下角的坐标
bottom_right = top_left[0] + w, top_left[1] + h
if max_val>0.8:
# 在窗口截图中匹配位置画红色方框,在image上画出左上角为top_left,右下角为bottom_right的坐标的矩形
cv2.rectangle(image, top_left, bottom_right, (0,0,255), 2)
#大漠插件的鼠标移动,也可以使用win32的鼠标控制
op.MoveTo(top_left[0]+4,top_left[1]+4)
cv2.imshow('Match Template', image)
cv2.waitKey()
然后,依据此可以改成的自动化操作。
边栏推荐
- Numerical analysis notes (I): equation root
- Explanation of the answers to the three questions
- Logstash+jdbc data synchronization +head display problems
- Trial of the combination of RDS and crawler
- Use the interface colmap interface of openmvs to generate the pose file required by openmvs mvs
- Run flash demo on ECS
- 用Redis实现分布式锁
- Idea uses the MVN command to package and report an error, which is not available
- Beego learning - Tencent cloud upload pictures
- Basic knowledge of database design
猜你喜欢

2022-2-14 learning the imitation Niuke project - send email
【毕业季|进击的技术er】又到一年毕业季,一毕业就转行,从动物科学到程序员,10年程序员有话说
![[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu](/img/57/b413a93a456a1872fc19aa825c937a.jpg)
[set theory] order relation (eight special elements in partial order relation | ① maximum element | ② minimum element | ③ maximum element | ④ minimum element | ⑤ upper bound | ⑥ lower bound | ⑦ minimu

Crawler career from scratch (IV): climb the bullet curtain of station B through API
![[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion](/img/76/b92fe4549cacba15c113993a07abb8.png)
[point cloud processing paper crazy reading frontier version 11] - unsupervised point cloud pre training via occlusion completion
![[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks](/img/61/aa8d0067868ce9e28cadf5369cd65e.png)
[point cloud processing paper crazy reading classic version 13] - adaptive graph revolutionary neural networks

Construction of simple database learning environment

【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数

Run flash demo on ECS

State compression DP acwing 291 Mondrian's dream
随机推荐
Flink学习笔记(九)状态编程
Beego learning - JWT realizes user login and registration
[set theory] order relation (chain | anti chain | chain and anti chain example | chain and anti chain theorem | chain and anti chain inference | good order relation)
Excel is not as good as jnpf form for 3 minutes in an hour. Leaders must praise it when making reports like this!
Solve POM in idea Comment top line problem in XML file
Overview of database system
Django operates Excel files through openpyxl to import data into the database in batches.
Matlab dichotomy to find the optimal solution
Move anaconda, pycharm and jupyter notebook to mobile hard disk
【Kotlin学习】高阶函数的控制流——lambda的返回语句和匿名函数
专利查询网站
Database execution error: SQL_ mode only_ full_ group_ by:
[point cloud processing paper crazy reading classic version 12] - foldingnet: point cloud auto encoder via deep grid deformation
LeetCode每日一题(2212. Maximum Points in an Archery Competition)
Simple use of MATLAB
Jenkins learning (I) -- Jenkins installation
Spark 结构化流写入Hudi 实践
CATIA automation object architecture - detailed explanation of application objects (III) systemservice
About the configuration of vs2008+rade CATIA v5r22
【点云处理之论文狂读前沿版9】—Advanced Feature Learning on Point Clouds using Multi-resolution Features and Learni