2015-01-14 578 views
21

我試圖做一個臉部跟蹤器,將Haar Cascade分類與Lucas Kanade良好的特徵檢測相結合。但是,我不斷收到一個錯誤,我無法弄清楚它的含義以及如何解決它。OpenCV錯誤:(-215)size.width> 0 && size.height> 0在函數imshow中

任何人都可以幫助我嗎?

錯誤:

line 110, in <module> 
cv2.imshow('frame',img) 
error: /build/buildd/opencv-2.4.8+dfsg1/modules/highgui/src/window.cpp:269: 
error: (-215)size.width>0 && size.height>0 in function imshow 

代碼:

from matplotlib import pyplot as plt 
import numpy as np 

import cv2 

face_classifier = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml') 


cap = cv2.VideoCapture(0) 


# params for ShiTomasi corner detection 
feature_params = dict(maxCorners = 200, 
         qualityLevel = 0.01, 
         minDistance = 10, 
         blockSize = 7) 

# Parameters for lucas kanade optical flow 
lk_params = dict(winSize = (15,15), 
        maxLevel = 2, 
        criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) 

# Create some random colors 
color = np.random.randint(0,255,(100,3)) 

# Take first frame and find corners in it 
ret, old_frame = cap.read() 



cv2.imshow('Old_Frame', old_frame) 
cv2.waitKey(0) 
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) 
restart = True 
#while restart == True: 
face = face_classifier.detectMultiScale(old_gray, 1.2, 4) 

if len(face) == 0: 
    print "This is empty" 

for (x,y,w,h) in face: 
    focused_face = old_frame[y: y+h, x: x+w] 

cv2.imshow('Old_Frame', old_frame) 

face_gray = cv2.cvtColor(old_frame,cv2.COLOR_BGR2GRAY) 

gray = cv2.cvtColor(focused_face,cv2.COLOR_BGR2GRAY) 

corners_t = cv2.goodFeaturesToTrack(gray, mask = None, **feature_params) 
corners = np.int0(corners_t) 

print corners 

for i in corners: 
    ix,iy = i.ravel() 
    cv2.circle(focused_face,(ix,iy),3,255,-1) 
    cv2.circle(old_frame,(x+ix,y+iy),3,255,-1) 

plt.imshow(old_frame),plt.show() 


# Create a mask image for drawing purposes 
mask = np.zeros_like(old_frame) 

while(1): 
    ret,frame = cap.read() 
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 

    # calculate optical flow 
    p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, corners_t, None, **lk_params) 

    # Select good points 
    good_new = p1[st==1] 
    good_old = corners_t[st==1] 

    # draw the tracks 
    print "COLORING TIME!" 
    for i,(new,old) in enumerate(zip(good_new,good_old)): 
     print i 
     print color[i] 
     a,b = new.ravel() 
     c,d = old.ravel() 
     mask = cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2) 
     frame = cv2.circle(frame,(a, b),5,color[i].tolist(),-1) 
     if i == 99: 
      break 
    img = cv2.add(frame,mask) 

    cv2.imshow('frame',img) 
    k = cv2.waitKey(30) & 0xff 
    if k == 27: 
     break 

    # Now update the previous frame and previous points 
    old_gray = frame_gray.copy() 
    p0 = good_new.reshape(-1,1,2) 

cv2.destroyAllWindows() 
cap.release() 
+1

這一行:'cv2.imshow( 'Old_Frame',old_frame_resized)'其中old_frame_resized界定? – cziemba

+0

對不起,它實際上是cv2.imshow('Old_Frame',old_frame)。這個錯誤仍然存​​在,雖然 – user3377126

+2

任何'imshow'的想法是拋出錯誤?錯誤可能意味着圖像是空的。我建議檢查'cap.read()'返回值,特別是False的'ret'值,這意味着捕獲失敗。 – cziemba

回答

10

此錯誤消息

error: (-215)size.width>0 && size.height>0 in function imshow

只是意味着imshow()是沒有得到來自輸入裝置的視頻幀。 您可以嘗試使用的

cap = cv2.VideoCapture(1) 

代替

cap = cv2.VideoCapture(0) 

&看到,如果問題仍然存在。

0
while(cap.isOpened()): 

    ret, img = cap.read() 
    print img 
    if img==None: #termino los frames? 
     break #si, entonces terminar programa 
    #gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
    cv2.imshow('img2',img) 
3

你不得不推遲

示例代碼:

import cv2 
import numpy as np 
import time 

cam = cv2.VideoCapture(0) 
time.sleep(2) 

while True: 
    ret,frame = cam.read() 
    cv2.imshow('webcam', frame) 
    if cv2.waitKey(1)&0xFF == ord('q'): 
     break 

cam.release() 
cv2.destroyAllWindows() 
1

在這兩條線:

mask = cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2) 

frame = cv2.circle(frame,(a, b),5,color[i].tolist(),-1) 

嘗試,而不是:

cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2) 

cv2.circle(frame,(a, b),5,color[i].tolist(),-1) 

我有同樣的問題,變量被返回爲空

0

cv2.circlecv2.lines不起作用。掩碼和幀都返回None。這些函數(線和圓)在opencv 3中,但不在舊版本中。

5

我有同樣的問題,解決的RET在拍攝視頻

import numpy as np 
import cv2 

# Capture video from file 
cap = cv2.VideoCapture('video1.avi') 

while True: 

    ret, frame = cap.read() 

    if ret == True: 

     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 

     cv2.imshow('frame',gray) 


     if cv2.waitKey(30) & 0xFF == ord('q'): 
      break 

    else: 
     break 

cap.release() 
cv2.destroyAllWindows() 
-1

檢查,如果你在python27根目錄或正在使用的Python版本有「opencv_ffmpeg330.dll」。如果沒有,你會發現它在「.... \ OpenCV \ opencv \ build \ bin」

選擇合適的版本和DLL複製到你的Python安裝的根目錄下,並重新運行該程序

+0

不是解決問題的辦法! –

0

我使用ssh連接到遠程服務器,並有Python代碼執行cv2.VideoCapture(0)至捕獲遠程網絡攝像頭,然後會遇到這樣的錯誤消息:

error: (-215)size.width>0 && size.height>0 in function imshow

最後,我授予與我的用戶帳戶和錯誤消息不見了訪問/ dev/video0的(這是我的攝像頭裝置)。使用usermod命令來添加用戶到組視頻

usermod -a -G video user 
0

我還會見了在樹莓派3的錯誤信息,但我的解決辦法是在谷歌搜索後重新裝入相機的核心,希望它可以幫助你。

須藤modprobe的bcm2835-用v412

順便說一句,這個錯誤,請檢查您的相機和文件路徑是可行與否

相關問題