【问题标题】:Error at template matching in OpenCV-PythonOpenCV-Python中的模板匹配错误
【发布时间】:2016-10-28 13:21:34
【问题描述】:

我正在尝试使用我的网络摄像头进行模板匹配。我使用网络摄像头提要作为源,并将模板用作从网络摄像头拍摄的小图像。模板和源都具有相同的位深度。(uint8)。

我在 VS 2013 IDE 中使用 OpenCV3.0 和 python 2.7 我收到了这个错误:

C:\builds\master_PackSlaveAddon-win32-vc12-static\opencv\modules\imgproc\src\templmatch.cpp:1062: 错误:(-215) (depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims()

这是我的代码:

import cv2
import numpy as np

cam=cv2.VideoCapture(1)
template = cv2.imread("C:/Users/user/Desktop/ttt.jpg",0)
w, h = template.shape[::-1]

method=cv2.TM_CCOEFF_NORMED

while 1:
    _,img=cam.read()
    res = cv2.matchTemplate(img,template,method)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

    # If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
    if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
        top_left = min_loc
    else:
        top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)

    cv2.rectangle(img,top_left, bottom_right, 255, 2)

    cv2.imshow('img',img)


    if cv2.waitKey(1) & 0xFF == ord('q'):
        cv2.destroyAllWindows

【问题讨论】:

  • 你需要发布你的代码
  • @EdChum 代码已添加

标签: python opencv templates


【解决方案1】:

将源图像转换为灰色

while 1:
_,img=cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # !!
res = cv2.matchTemplate(gray, template,method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多