python - OpenCV example error - TypeError: 'NoneType' object is not subscriptable -
i'm trying run python example opencv site:
http://docs.opencv.org/trunk/d7/d8b/tutorial_py_lucas_kanade.html
import numpy np import cv2 cap = cv2.videocapture('slow.flv') # params shitomasi corner detection feature_params = dict( maxcorners = 100, qualitylevel = 0.3, mindistance = 7, blocksize = 7 ) # parameters 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 random colors color = np.random.randint(0,255,(100,3)) # take first frame , find corners in ret, old_frame = cap.read() old_gray = cv2.cvtcolor(old_frame, cv2.color_bgr2gray) p0 = cv2.goodfeaturestotrack(old_gray, mask = none, **feature_params) # create mask image 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, p0, none, **lk_params) # select points good_new = p1[st==1] good_old = p0[st==1] # draw tracks 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), color[i].tolist(), 2) frame = cv2.circle(frame,(a,b),5,color[i].tolist(),-1) img = cv2.add(frame,mask) cv2.imshow('frame',img) k = cv2.waitkey(30) & 0xff if k == 27: break # update previous frame , previous points old_gray = frame_gray.copy() p0 = good_new.reshape(-1,1,2) cv2.destroyallwindows() cap.release() i use python 3 run example
it works 5-15 seconds videos , stops next error:
traceback (most recent call last): file "o.py", line 28, in good_new = p1[st==1] typeerror: 'nonetype' object not subscriptable
what can wrong in example?
that happens if optical flow objects (the color dots on screen) go out of frame. this- if array p1 empty, find features again , calculate optical flow. should work.
add in while loop( fills entire screen lines on time) :
if p1 none: p0 = cv2.goodfeaturestotrack(old_gray, mask = none, **feature_params) p1, st, err = cv2.calcopticalflowpyrlk(...., **lk_params)
Comments
Post a Comment