簡體   English   中英

OpenCV錯誤:cv2.matchTemplate()中的斷言失敗

[英]OpenCV Error: Assertion failed in cv2.matchTemplate()

我目前正在一個項目中,目標是從攝像機流中識別“符號”。 為此,我正在使用cv2.matchTemplate()函數。 我正在使用RasPi 3B和RasPi相機模塊V2來獲取視頻。 我遇到了一個錯誤,我認為這是由模板路徑錯誤引起的,但是在檢查之后,我發現該路徑正確。

這是視頻形狀和模板形狀的照片

frame.shape= [480,640,3]

t0.shape= [300,300,3] (I will make this one smaller, probably to like [30,30,3])

我的主要代碼:

import numpy as np
import cv2
import time
import argparse
import imutils
from picamera.array import PiRGBArray
from picamera import PiCamera

ap = argparse.ArgumentParser()
ap.add_argument("-t0", "--template0", required = True, help = "Cesta k 
templatu pro cmd0")
args = vars(ap.parse_args())

t0 = cv2.imread(args["template0"])
w, h,_ = t0.shape[::-1]

camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))

time.sleep(0.1)

for f in camera.capture_continuous(rawCapture, format="bgr", 
use_video_port=True):
    frame = f.array

    print(frame.shape)
    print(t0.shape)

    frame = imutils.resize(frame, width = 300)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    res = cv2.matchTemplate(gray, t0, cv2.TM_CCOEFF_NORMED) #the error throwing line
    treshold = 0.8
    loc = np.where(res >= treshold)

    for pt in zip(*loc[::-1]):
       cv2.rectangle(frame, pt, (pt[0]+w, pt[1]+h), (0,255,0), 2)    

    cv2.imshow("Frame", frame)
    rawCapture.truncate(0)

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

和我得到的錯誤信息:

OpenCV Error: Assertion failed ((depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2) in matchTemplate, file /home/pi/opencv-3.3.0/modules/imgproc/src/templmatch.cpp, line 1102
Traceback (most recent call last):
  File "tmplRec_fin1.py", line 43, in <module>
    res = cv2.matchTemplate(gray, t0, cv2.TM_CCOEFF_NORMED)
cv2.error: /home/pi/opencv-3.3.0/modules/imgproc/src/templmatch.cpp:1102: error: (-215) (depth == CV_8U || depth == CV_32F) && type == _templ.type() && _img.dims() <= 2 in function matchTemplate

(代表問題作者發布)

正是導致問題的原因,老實說,我什至不明白我是怎么犯了這樣一個愚蠢的錯誤的。 希望它將對將來的人有所幫助。

簡而言之:對我造成問題的是,我嘗試將灰度視頻流與rgb模板匹配,但無法正常工作。 我認為這與圖像的深度或通道的深度有關。 確保將灰度與灰度進行比較。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM