qingfeng__
qingfeng__
2017-08-07 13:49

openCV中使用模板匹配函数时遇到如下错误

  • python
  • opencv

错误提示如下:
Traceback (most recent call last):
File "D:/学习办公盘/python学习代码/opencv/moban.py", line 12, in
res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
error: ........\opencv\modules\imgproc\src\templmatch.cpp:249: error: (-215) (img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type() in function cv::matchTemplate

代码如下:
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread('d:/学习办公盘/python学习代码/opencv/捕获.png')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

template = cv2.imread('d:/学习办公盘/python学习代码/opencv/text3.jpg')
w= template.shape[0]
w= template.shape[1]

res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED)
threshold = 0.8

loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)

#cv2.imwrite('res.png',img_rgb)
cv2.imshow('res',img)
cv2.waitKey(0)
cv2.destroyAllWindows()


  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答