【问题标题】:Template match a screen capture using opencv模板匹配使用 opencv 的屏幕截图
【发布时间】:2020-02-10 08:23:18
【问题描述】:

我想要实现的是实时捕获我的屏幕并让它检测框架内何时显示某个图像。到目前为止,我想出的是:

屏幕截图:

last_time = time.time()
while(True):
    screen = np.array(ImageGrab.grab(bbox=(0,40, 800, 640)))
    print('Loop took {} seconds'.format(time.time()-last_time))
    last_time = time.time()
    cv2.imshow('window', cv2.cvtColor(screen, cv2.COLOR_BGR2RGB))
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

模板匹配:

import cv2
import numpy as np

img_rgb = cv2.imread('frame.png')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)

template = cv2.imread('template.png',0)
w, h = template.shape[::-1]

res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)

for pt in zip(*loc[::-1]):
    cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,255,255), 2)

cv2.imshow('Detected',img_rgb)

cv2.waitKey(0)
cv2.destroyAllWindows()

我让它们两个单独工作,但无法将它们融合在一起。我主要努力的是 imread() 当前帧,因为它作为 nparray 从捕获中返回,而 cv2.imread() 需要图片文件(png.、jpg.等)

【问题讨论】:

标签: python opencv


【解决方案1】:
  • while(True)循环之前加载模板图片;
  • 在循环内部,确保将screen 从 RGB 转换为 GREY;
  • 然后,执行那3行模板匹配代码;
  • 最后,要使用矩形显示输出,请将它们绘制在 screen 上,而不是在对应的灰度上。

【讨论】:

  • @tribute2powerOfficial 随时!
猜你喜欢
  • 1970-01-01
  • 2019-10-06
  • 1970-01-01
  • 2017-08-30
  • 1970-01-01
  • 2014-07-30
  • 2012-12-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多