使用Python与OpenCV的cv2.matchTemplate时触发断言错误
运行Runescape机器人脚本触发OpenCV断言错误
机器人截图
运行用于Runescape的bot.py脚本时触发了断言错误,优化移动逻辑时遇到此问题,试过Stack Overflow上的解决方案但无效。本人是编程和Stack Overflow新手。
相关代码
def __init__(self, window_offset, window_size): self.lock = Lock() self.window_offset = window_offset self.window_w = window_size[0] self.window_h = window_size[1] self.limestone_tooltip = cv.imread('limestone_tooltip.jpg', cv.IMREAD_UNCHANGED) self.state = BotState.INITIALIZING self.timestamp = time() def click_next_target(self): targets = self.targets_ordered_by_distance(self.targets) target_i = 0 found_limestone = False while not found_limestone and target_i < len(targets): # if we stopped our script, exit this loop if self.stopped: break target_pos = targets[target_i] screen_x, screen_y = self.get_screen_position(target_pos) print('Moving mouse to x:{} y:{}'.format(screen_x, screen_y)) if self.confirm_tooltip(target_pos): print('Click on confirmed target at x:{} y:{}'.format(screen_x, screen_y)) found_limestone = True pyautogui.click() # save this position to the click history self.click_history.append(target_pos) target_i += 1 return found_limestone def confirm_tooltip(self, target_position): result = cv.matchTemplate(self.screenshot, self.limestone_tooltip, cv.TM_CCOEFF_NORMED) min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result) # object found if max_val >= self.TOOLTIP_MATCH_THRESHOLD: return True # print('Tooltip not found.') return False def get_screen_position(self, pos): return (pos[0] + self.window_offset[0], pos[1] + self.window_offset[1])
错误信息
Traceback (most recent call last): File "C:\Python311\Lib\threading.py", line 1038, in _bootstrap_inner self.run() File "C:\Python311\Lib\threading.py", line 975, in run self._target(*self._args, **self._kwargs) File "D:\DEV\OpenCvTeste\teste_de_fogo\bot.py", line 227, in run success = self.click_next_target() ^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\DEV\OpenCvTeste\teste_de_fogo\bot.py", line 90, in click_next_target if self.confirm_tooltip(target_pos): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\DEV\OpenCvTeste\teste_de_fogo\bot.py", line 149, in confirm_tooltip result = cv.matchTemplate(self.screenshot, self.limestone_tooltip, cv.TM_CCOEFF_NORMED) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cv2.error: OpenCV(4.9.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'
问题原因与解决方法
原因
OpenCV的matchTemplate函数有三个硬性要求:
- 源图像和模板图像的深度必须是
CV_8U(8位无符号整数)或CV_32F(32位浮点数) - 两者的图像类型(通道数、数据类型)必须完全一致
- 图像维度不能超过2(不能是3D数组)
你的代码中,用cv.IMREAD_UNCHANGED读取的模板图可能包含Alpha通道(4通道),而self.screenshot大概率是3通道的BGR图像,导致类型不匹配;也可能存在图像深度不一致的情况。
解决步骤
- 统一图像通道数:修改模板图读取方式,去掉Alpha通道,改用
cv.IMREAD_COLOR读取:self.limestone_tooltip = cv.imread('limestone_tooltip.jpg', cv.IMREAD_COLOR) - 确保截图格式正确:如果
self.screenshot是通过pyautogui.screenshot()获取的,需要转换成OpenCV的BGR格式(pyautogui返回的是RGB):# 假设截图代码示例,需转换格式 screenshot_pil = pyautogui.screenshot(region=...) self.screenshot = cv.cvtColor(np.array(screenshot_pil), cv.COLOR_RGB2BGR) - 检查图像尺寸:确保模板图的宽高小于等于源图像(截图)的宽高,否则
matchTemplate也会触发断言错误。
内容的提问来源于stack exchange,提问作者Rupelio
相关产品推荐
相关产品推荐

