【问题标题】:cv2.matchTemplate gives an error: (-215:Assertion failed)cv2.matchTemplate 给出错误:(-215:断言失败)
【发布时间】:2021-03-12 13:36:54
【问题描述】:
import cv2
import numpy as np


kamera = cv2.VideoCapture(0)

while True :
    ret,kare = kamera.read()

    gri_kare = cv2.cvtColor(kare,cv2.COLOR_BGR2GRAY)

    nesne = cv2.imread("nesn.jpg",0)

    w,h = nesne.shape

    res = cv2.matchTemplate(gri_kare,nesne,cv2.TM_CCOEFF_NORMED)

    esikdeger = 0.8

    loc = np.where(res > esikdeger)

    for n in zip(loc) :
        cv2.rectangle(nesne,n,(n[0]+h,n[1]+w),(255,255,255),2,)

    cv2.imshow("asd",kare)


    if cv2.waitKey(25) & 0xFF == ord("q"):
        break


kamera.release()
cv2.destroyAllWindows()

我得到这个错误:

Traceback (most recent call last):
  File "C:/Users/abrakadabra/Desktop/python/CV/nesnetanima1.py", line 16, in <module>
    res = cv2.matchTemplate(gri_kare,nesne,cv2.TM_CCOEFF_NORMED)
cv2.error: OpenCV(3.4.2) C:\projects\opencv-python\opencv\modules\imgproc\src\templmatch.cpp:1107: error: (-215:Assertion failed) _img.size().height <= _templ.size().height && _img.size().width <= _templ.size().width in function 'cv::matchTemplate'

【问题讨论】:

  • 你确定你的nesne图片已经加载了吗?该错误意味着模板的大小大于相机的帧大小。 nesne和kare的分辨率是多少?
  • 谢谢我解决了这个问题,但有一个新问题。你能帮我解决这个问题吗? w,h = nesne.shape AttributeError: 'NoneType' object has no attribute 'shape' [WARN:1] terminating async callback 我确定图像的名称就在那儿
  • 它没有用,但我还是解决了,有一个新的,你可以看看这个:res = cv2.matchTemplate(gri_kare,nesne,cv2.TM_CCOEFF_NORMED) cv2.error: OpenCV(4.0.0) C :\projects\opencv-python\opencv\modules\imgproc\src\templmatch.cpp:1107: 错误: (-215:Assertion failed) (depth == CV_8U || depth == CV_32F) && type == _templ.type () && _img.dims()
  • 请提供相机中模板和图像中的数据类型。此错误意味着数据的类型必须是 8U(无符号字符)或 32F(浮点数),并且在图像和模板中必须相同。

标签: python opencv matchtemplate


【解决方案1】:

cv2.matchTemplate 的输入图像均为灰度时有效。

读取图片后添加以下行nesne = cv2.cvtColor(nesne, cv2.COLOR_BGR2GRAY)即可。

【讨论】:

  • 有人能解释一下为什么会出现这个错误以及为什么转换为 BW 可以解决这个问题吗?
猜你喜欢
  • 2018-08-29
  • 2021-03-14
  • 2021-06-27
  • 2021-02-13
  • 2022-10-15
  • 1970-01-01
  • 2020-03-29
  • 2020-02-03
  • 1970-01-01
相关资源
最近更新 更多