Ошибка OpenCV: (-215:Assertion failed)

Код:

import pyautogui
import cv2
import numpy as np

screenshot = pyautogui.screenshot(region=(1380, 9, 536, 1067))
screenshot_array = np.array(screenshot)

sample_image = cv2.imread('WAS/ggt.png')
sample_image_gray = cv2.cvtColor(sample_image, cv2.COLOR_BGR2GRAY)
result = cv2.matchTemplate(screenshot_array, sample_image_gray, cv2.TM_CCOEFF_NORMED)
threshold = 0.8

locations = np.where(result >= threshold)
if len(locations[0]) > 0:
    x = locations[1][0] + sample_image.shape[1] // 2
    y = locations[0][0] + sample_image.shape[0] // 2

    pyautogui.click(x, y)

Но выводится такая ошибка:

Traceback (most recent call last):
  File "C:\PythonProject\pythonProject\ad.py", line 10, in <module>
    result = cv2.matchTemplate(screenshot_array, sample_image_gray, cv2.TM_CCOEFF_NORMED)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
cv2.error: OpenCV(4.10.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'

Помогите пожалуйста


Ответы (1 шт):

Автор решения: Vlad Chapl

screenshot цветной, а sample_image_gray 8 bit. Может в этом дело? Приведи к одному типу (число каналов, возможно сконвертировать в float 32).

→ Ссылка