首页 > 解决方案 > OpenCV 错误 -215:断言失败,无法弄清楚如何诊断此错误

问题描述

我一直在关注为 Flash 游戏 Burrito Bison 开发模板匹配机器人的教程,github 的代码链接在这里,但即使使用原始源代码似乎也无法让机器人运行。错误似乎与res = cv2.matchTemplate(img_grayscale, template, cv2.TM_CCOEFF_NORMED)文件中的行有关vision.py

完整的错误是cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\imgproc\src\templmatch.cpp:589: error: (-215:Assertion failed) corrsize.height <= img.rows + templ.rows - 1 && corrsize.width <= img.cols + templ.cols - 1 in function 'cv::crossCorr'

没有尝试太多,我以前从未使用过 OpenCV,这对我来说很新。

def match_template(self, img_grayscale, template, threshold=0.9):
    """ Matches template image in a target grayscaled image """

    res = cv2.matchTemplate(img_grayscale, template, cv2.TM_CCOEFF_NORMED)
    matches = np.where(res >= threshold)

预期的结果是它正常工作并玩游戏。

标签: python-3.xnumpyopencvbotspynput

解决方案


推荐阅读