0

every time I run this code:

import cv2 as cv
import mss
import numpy as np
import time



with mss.mss() as sct:
    area = (82,342,677,63)
    region = {"top": area[0], "left": area[1], "width": area[2], "height": area[3]}
    
    while True:
        Image_bgr = np.array(sct.grab(region))
        Image_RGB = cv.cvtColor(Image_bgr, cv.COLOR_BGR2RGB) 
        Haystack = cv.imread(str(Image_RGB),cv.IMREAD_UNCHANGED)
        Needle = cv.imread("hio.png", cv.IMREAD_UNCHANGED)
        result = cv.matchTemplate(Haystack, Needle, cv.TM_CCOEFF_NORMED)
        cv.imshow("result", result )
        if cv.waitKey(1) & 0xFF == ord('q'):
            break

cv.destroyAllWindows()`

I get this error message:

`OpenCV(4.7.0) 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'
  File "C:\Users\pmaze\OneDrive\Desktop\New folder\bavl.py", line 17, in <module>
    result = cv.matchTemplate(Haystack, Needle, cv.TM_CCOEFF_NORMED)
cv2.error: OpenCV(4.7.0) 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'

With this code I am trying to make a bot which updates it's haystack using mss to screen shot my game and then use matchtemplate to compare the template fish with a needle fish. I'm completely stumped as I'm new to python and have no idea what this error message is. Please help.

update: I switch image rgb to image bgr and now it gives this error: Haystack = cv.imread(Image_bgr, cv.IMREAD_UNCHANGED) TypeError: Can't convert object to 'str' for 'filename'

  • Check the shapes and dtypes of Haystack and Needle. Image dtypes need to be uint8 or float32. Needle needs to be smaller than Haystack. Images must be either 3 channels or 1 channel. – fmw42 Jun 17 '23 at 18:03

0 Answers0