【问题标题】:opencv and old games graphics python 3opencv和旧游戏图形python 3
【发布时间】:2021-08-11 22:10:15
【问题描述】:

我正在尝试匹配以下图片

使用以下模板,使用cv2.matchTemplate

#elementachercher = path to template png file
#screenascanner = path to global picture where to find template


import pyautogui
import time
import cv2
import numpy as np
from matplotlib import pyplot as plt
import os
import imutils

def tryfoundobject(elementachercher, screenascanner):
    img_rgb = cv2.imread(screenascanner)
    img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
    template = cv2.imread(elementachercher,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,0,255), 2)
        if pt != None:
            print(elementachercher+" matched")
            break
        else:
            continue
    cv2.imwrite(elementachercher,img_rgb)
    
  tryfoundobject("/home/noway/template.png", "/home/noway/mypicture.png")  
    

但没有找到匹配项。

我做错了什么? 第一个屏幕是我想要的结果,第二个屏幕是我在脚本之后得到的结果

【问题讨论】:

  • 请解释您的期望和没有得到什么
  • 我正在尝试在大图上使用matchtemplate,小图是我获取模板坐标的模板,其中X,Y但是opencv从不匹配我在主图上的模板
  • 会发生什么?请编辑问题以显示res(首先标准化为[0,255]),然后显示loc
  • 这可能只是缩进错误吗?
  • 我编辑了我的帖子,以展示我想用屏幕做什么、我想要什么以及我得到什么

标签: python python-3.x numpy opencv


【解决方案1】:

关注the chat discussion

res_normed = (res - np.min(res)) / (np.max(res) - np.min(res))
res_uint = np.array(res_normed * 255, dtype=np.uint8)
cv2.imshow("res_uint ", res_uint)

res 看起来像这样

您必须在此图像上寻找局部最大值才能获得匹配的坐标。

请参阅 this 了解非常简单的 POC,参阅 this 了解完整解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 1970-01-01
    • 2013-06-26
    相关资源
    最近更新 更多