So I have 44 perflectly crop template with no background and mi goal is to see if one of the template is on a screenshot took with pyautogui. mi only problem is that I dont have any idea of how to do this. And the template on the screenshot can be oriented in anyway (but still a 2d object so its just a matter of rotation) + the template dont have the same size as on the screen.
here is how I tried to do it using opencv using only 1 image that was present on screen:
import cv2
import pyautogui
import numpy as np
objet_image = cv2.imread('traps/canon\\Cannon1.png', cv2.IMREAD_UNCHANGED)
screenshot = pyautogui.screenshot()
screenshot = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2GRAY)
objet_image = cv2.cvtColor(objet_image, cv2.COLOR_BGR2BGRA)
if objet_image.shape[2] > 1:
objet_image = cv2.cvtColor(objet_image, cv2.COLOR_BGR2GRAY)
result = cv2.matchTemplate(screenshot, objet_image, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
objet_position = max_loc
pyautogui.moveTo(objet_position[0], objet_position[1])
I converted the image in 8bits because I got prompt of error with depth:
Traceback (most recent call last):
File "c:\Users\autre\Documents\VsCode\King of Thieves bot\test.py", line 16, in <module>
result = cv2.matchTemplate(screenshot, objet_image, cv2.TM_CCOEFF_NORMED)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.8.1) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\templmatch.cpp:1164: error: (-215:Assertion failed) (depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2 in function 'cv::matchTemplate'