【发布时间】: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