【发布时间】:2019-11-17 01:41:39
【问题描述】:
我一直在尝试使用 OpenCvs 匹配模板功能在我的屏幕上简单地应用掩码来定位模板,这只是一个正方形。目标是定位盒子,并忽略里面的内容。
模板看起来像:
1
我的方法是简单地对该模板进行二值化和反转,并将其用作掩码以输入 matchTemplate(),但是我不断遇到难以解释的错误。
这是我的代码:
import cv2
import numpy as np
template = cv2.imread(path + 'tile_outline_pink.png')
image = cv2.imread(path + 'test_screenshot.png')
w, h = template.shape[:-1]
#data = np.zeros((h, w, 3), dtype=np.uint8)
templateGray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
#cv2.imwrite("imghatGray.png", imghatGray)
ret, mask = cv2.threshold(templateGray, 200, 255, cv2.THRESH_BINARY)
#cv2.imwrite("orig_mask.png", orig_mask)
mask_inv = cv2.bitwise_not(mask)
#mask_inv = cv2.cvtColor(mask_inv,cv2.COLOR_GRAY2RGB)
print(image.shape, image.dtype)
print(template.shape, template.dtype)
print(mask_inv.shape, mask_inv.dtype)
method = cv2.TM_SQDIFF
result = cv2.matchTemplate(template, image, method, None, mask=mask_inv)
输出如下:
(674, 969, 3) uint8 (41, 45, 3) uint8 (41, 45) uint8
错误回溯(最近一次调用最后一次) 在 31 方法 = cv2.TM_SQDIFF 32 ---> 33 结果 = cv2.matchTemplate(模板,图像,方法,无,掩码 = mask_inv) 34 35
错误:OpenCV(4.1.0) C:\projects\opencv-python\opencv\modules\core\src\matrix.cpp:235: error: (-215:Assertion failed) s >= 0 in function ' cv::setSize'
我也尝试将模板图像转换为 RGBA 图像,但也没有成功,我不确定如何继续
感谢您的帮助
【问题讨论】: