Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hello,

This is a very late answer to your problem, you probably solved it yourself already. I thought it might be of help for others (like me) who are struggling with this method in OpenCV.

It seems to me that you do NOT need to use the function cv2.buildOpticalFlowPyramid() AT ALL to work with pyramids. The parameter maxLevel in cv2.OpticalFlowPyrLK() is used to determine the maximum level of the pyramid intrinsically built by the function.

This is quite confusing in the documentation of OpenCV, I hope they will change it soon. Took me way too long to figure this out:

  • maxLevel – 0-based maximal pyramid level number; if set to 0, pyramids are not used (single level), if set to 1, two levels are used, and so on; if pyramids are passed to input then algorithm will use as many levels as pyramids have but no more than maxLevel. It seems just wrong how it is written there, at least for the latest version of OpenCV

`

lk_params = dict(winSize=(21, 21),  
         maxLevel=3, # number oflayers of your pyramid
         criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 30, 0.01))

def KLT_featureTracking(image_ref, image_cur, px_ref):

   kp2, st, err = cv2.calcOpticalFlowPyrLK(image_ref, image_cur, px_ref, None, **lk_params)
   kp1, st, err = cv2.calcOpticalFlowPyrLK(image_cur, image_ref, kp2, None, **lk_params)

` This should work. For older versions of OpenCV: cv2.buildOpticalFlowPyramid() returns two values, number of layers and pyramid itself. This was probably the problem of @jads_96

Best wishes!