Ask Your Question

Revision history [back]


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.

In conclusion, just change this line:

res = cv2.calcOpticalFlowPyrLK(pyr1, pyr2, prev_features)

to this one:

res = cv2.calcOpticalFlowPyrLK(im, im2, prev_features, maxLevel = 10)

and you are using pyramids in your optical flow (up to 11 levels).

Best wishes!