是否可以模板匹配二进制图像?我已经尝试过了,没有任何帮助。我总是得到一个错误:
cv2.error: OpenCV(3.4.5) C:\projects\opencv-python\opencv\modules\imgproc\src\templmatch.cpp:1107: error: (-215:Assertion failed) (depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2 in function 'cv::matchTemplate'
我试过使用astype(np.uint8)
,但它也无济于事。
//template is already binary
//edited: all the code
img = cv2.imread('img/img.png')
template = cv2.imread('template-match/template.png')
img_roi=img[310:1060,510:1430]
img_gray = cv2.cvtColor(img_roi,cv2.COLOR_BGR2GRAY)
afterEnhance = cv2.GaussianBlur(img_gray, (7, 7), 0)
afterEnhance=cv2.equalizeHist(afterEnhance)
_,th1=cv2.threshold(afterEnhance,55,255,cv2.THRESH_BINARY)
result = cv2.matchTemplate(th1, template,cv2.TM_CCOEFF)