Choose my own points instead of corner points in implementing optical flow in opencv python -
the current algorithm uses goodfeaturestotrack select corner points want choose own points. secondly, want save data pixels has point moved to. how go solving these 2 problems? code using :-
import numpy np import cv2 cap = cv2.videocapture('output.avi') # params shitomasi corner detection # throw every other corners below quality level. sort rest in descending order. pick greatest, throw rest in min , pick n greatest feature_params = dict( maxcorners = 1, # how many pts. locate qualitylevel = 0.3, # b/w 0 & 1, min. quality below rejected mindistance = 7, # min eucledian distance b/w corners detected blocksize = 7 ) # # parameters lucas kanade optical flow lk_params = dict( winsize = (15,15), # size of search window @ each pyramid level maxlevel = 2, # 0, pyramids not used (single level), if set 1, 2 levels used, , on criteria = (cv2.term_criteria_eps | cv2.term_criteria_count, 10, 0.03)) # criteria : termination criteria iterative search algorithm. # after maxcount { criteria_count } : no. of max iterations. # or after { criteria epsilon } : search window moves less epsilon # create random color pt. chosen color = np.random.randint(0,255,(1,3)) # take first frame , find corners in ret, old_frame = cap.read() #read frame old_gray = cv2.cvtcolor(old_frame, cv2.color_bgr2gray) #convert grayscale p0 = cv2.goodfeaturestotrack(old_gray, mask = none, **feature_params) #use goodfeaturestotrack find location of corner. #cvpoint pl = new cvpoint(2,3) # create mask image drawing purposes filed zeros mask = np.zeros_like(old_frame) while(1): ret,frame = cap.read() frame_gray = cv2.cvtcolor(frame, cv2.color_bgr2gray) # calculate optical flow # err kind of gives correlation error(matching error) 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) # ## release , destroy windows. cv2.destroyallwindows() cv2.release()
just mark miller mentioned, can use feature vector input points. cv2.calcopticalflowpyrlk use them find on new image new locations of features comparing patches of pixels around given coordinates.
to save new ones need catch returned values of cv2.calcopticalflowpyrlk
a little bit other features: cv2.goodfeaturestotrack (shi - thomasi or harris corners) used because 2 perpendicular edges easy find gives lots of features, if of them occluded don't end nothing. more advanced features orb have lot more information in descriptors can used link feature on different images 2 parameters aquired in shi - thomasi or harris corners, , bit of waste use them simple lucas - kanade algorithm.
Comments
Post a Comment