2016-04-06 11 views
0

私はしきい値でシャドウを取り除き、ライブカメラの動きを検出しようとしています。そうでない場合は、このエラーで停止等高線で検出してエラーを表示する

in line (cv2.imshow("detect",img)) which stop with this error : 
    cv2.imshow("thresh",img) 

error: /home/rayannik/opencv-2.4.10/modules/highgui/src/window.cpp:261:  
error: (-215) size.width>0 && size.height>0 in function imshow 

我々はその行を無視した場合、何かがカメラの前に常に移動するちょうどその時、それは仕事になります。

import cv2 
import numpy as np 

cam = cv2.VideoCapture(0) 

while True: 

    t=cam.read()[1] 
    t0=cv2.cvtColor(cam.read()[1],cv2.COLOR_RGB2GRAY) 
    t1=cv2.cvtColor(cam.read()[1],cv2.COLOR_RGB2GRAY) 
    t2=cv2.cvtColor(cam.read()[1],cv2.COLOR_RGB2GRAY) 

    a1=cv2.absdiff(t0,t1) 
    a2=cv2.absdiff(t1,t2) 
    b=cv2.bitwise_and(a1,a2) 

    ret,thresh0=cv2.threshold(b,30,255,cv2.THRESH_BINARY) 
    thresh1=cv2.bitwise_and(t1,thresh0) 
    contours,hierarchy=cv2.findContours(thresh0,1,2) 
    cnt=contours[0] 
    x,y,w,h=cv2.boundingRect(cnt) 
    img=cv2.rectangle(t1,(x,y),(x+w,y+h),(0,255,0),2) 

    cv2.imshow("winName",t1) 
    cv2.imshow("detect",img) 


    key=cv2.waitKey(10) 
    if key==27: 
     cv2.destroyAllwindow() 
     break 
print "End" 

しかし、私は次のエラーが発生する:ここ は私のプログラムです:

cnt=contours[0] 
IndexError: list index out of range 

答えて

0

問題はラインである:

img=cv2.rectangle(t1,(x,y),(x+w,y+h),(0,255,0),2) 

は基本的にcv2.rectangle()方法は、インプレース入力マットを変更してNoneを返すので、パラメータt1はその場で変更され、戻り値の型がNoneので、imshow()で使用している変数imgNoneからです。

ウィンドウに既に変更が反映されているため、cv2.imshow("detect",img)を削除してください。

第2に、入力マットにcv2.findCountours()の輪郭が見つかりませんでしたので、はエラーIndexOutOfBoundsを発生しています。あなたはいつもcv2.findCountours方法にのみ二値を渡すことはなく、また、としてcountoursにアクセスする前に確認する必要があります。

if (len(contours) > 0): # this condition would prevent the IndexOutOfBounds error 
    cnt=contours[0] 
関連する問題