KL_feature跟踪无法正常工作

时间:2016-07-01 08:25:47

标签: python-2.7 opencv ubuntu-14.04

嘿伙计们我不知道为什么但是由于某种原因我在实施这个代码时遇到了问题我已经通过各种网站上的所有帖子来摆脱这个问题,但大多数是基于语法在我的情况下不是错误:

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()

给出错误消息:

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正在使用掩码和帧正常工作

1 个答案:

答案 0 :(得分: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(帧,(A,B),5,(0,0,255), - 1)