1

Is it possible to templateMatch binary images? I've tried it and nothing helps. I always get an error :

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'

I've tried using astype(np.uint8) but it doesn't help either.

//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)

1 Answers1

1

Seems you figured it out: the template has 3 color channels, whereas img is converted to grayscale and has only one channel.
You can load the template-image as grayscale directly using
template = cv2.imread('template-match/template.png',0)

J.D.
  • 4,511
  • 2
  • 7
  • 20