2016-07-01 111 views
0

嘿傢伙我不知道爲什麼,但由於某種原因,我在面對執行下面的代碼的問題,我已經通過各種網站上的所有帖子擺脫這個問題,但大多數是基於語法錯誤,在我的情況並不:KL_feature跟蹤不起作用

import cv2 
import numpy as np 
from matplotlib import pyplot as plt 

cap=cv2.VideoCapture(0) 
#parameters for ShiTomasi corner detection 
st_params=dict(maxCorners = 100,qualityLevel = 0.3,minDistance = 7,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)) 
#Take first frame and find corners in it 
ret,first_frame=cap.read() 
first_gray=cv2.cvtColor(first_frame,cv2.COLOR_BGR2GRAY) 
p0=cv2.goodFeaturesToTrack(first_gray,mask = None,**st_params) 
#create mask 
mask=np.zeros_like(first_frame) 

while True: 
    (ret,frame) = cap.read() 
    #frame_gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) 
    cv2.imshow('k',frame) 
    k=cv2.waitKey(0) & 0xff 
#calculate optical flow 
pl,st,err= cv2.calcOpticalFlowPyrLK(first_gray, frame_gray, p0, None,**lk_params) 


#Good point selection 
good_new=pl[st == 1] 
good_old=p0[st == 1] 

#draw the tracks 
for i,(new,old) in enumerate(zip(good_new,good_old)): 
    a,b = new.ravel() 
    c,d = old.ravel() 
    mask = cv2.line(mask, (a,b),(c,d),(0,255,0), 2) 
    frame = cv2.circle(frame,(a,b),5,(0,0,255),-1) 
img = cv2.add(frame,mask) 

#cv2.imwrite('detectedframe_{0}.jpg'.format(i),img) 
cv2.imshow('k',img) 
k=cv2.waitKey(30) & 0xff 
if k == 27: 
    break 

first_gray=frame_gray.copy() 
p0=good_new.reshape(-1,1,2) 

cv2.destroyAllWindows() 
cap.release() 

其給出錯誤messange:

OpenCV Error: Assertion failed (size.width>0 && size.height>0) in imshow, file /home/vasu/Documents/opencv-2.4.13/modules/highgui/src/window.cpp, line 261 
Traceback (most recent call last): 
File "LK_OpticalFlow.py", line 42, in <module> 
cv2.imshow('k',img) 
cv2.error: /home/vasu/Documents/opencv-2.4.13/modules/highgui/src/window.cpp:261: error: (-215) size.width>0 && size.height>0 in function imshow 

進行多次打擊和審判後,我已經來到了這個錯誤發生原因的結論以cv2.line和cv2.circle作爲cv2.imshow與掩碼工作正常和框架時,這些行被評論

回答

0

嘿傢伙看來我的問題已經解決了我認爲在opencv 2.4.13有一些變化。我只是改變了

mask = cv2.line(mask,(a,b),(c,d),(0,255,0),2) frame = cv2.circle(frame,(a,b), 5,(0,0,255), - 1) 到 cv2.line(mask,(a,b),(c,d),(0,255,0),2) cv2.circle(frame, b),5,(0,0,255), - 1)