嘿,伙计们,我不知道为什么,但由于某种原因,我在实现以下代码时面临问题,我已经通过了各种网站上的所有帖子来摆脱这个问题,但其中大多数都是基于语法错误,在我的情况下不是:
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发生。直线和cv2。圆为cv2。当这些行被注释
嘿,伙计们,似乎我的问题已经解决了,我认为在openv 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,0255),1)