Ask Your Question
0

Python: Pyramids not working for calcOpticalFlowPyrLK

asked 2016-09-05 12:34:24 -0500

jads_96 gravatar image

System information (version)

OpenCV => 3.1-dev

Operating System / Platform => Windows 64 Bit

Compiler => Visual Studio 2015

Detailed description:

When trying to run pyramids as the input for the cv2.calcOpticalFlowPyrLK function the program gives out an error. when I run the cv2.buildOpticalFlowPyramid method it returns a pyramid, without any errors but as soon as I use it as input it doesn't run.

Steps to reproduce:

lk_params = dict(winSize=(21, 21),  # Parameters used for cv2.calcOpticalFlowPyrLK (KLT tracker)
         maxLevel=3,
         criteria=(cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 30, 0.01))

def KLT_featureTracking(image_ref, image_cur, px_ref):

   image_ref = cv2.buildOpticalFlowPyramid(image_ref, (512, 512), 2)
   image_cur = cv2.buildOpticalFlowPyramid(image_cur, (512, 512), 2)

   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)

Error:

kp2, st, err = cv2.calcOpticalFlowPyrLK(image_ref, image_cur, px_ref, None, **lk_params)
TypeError: prevImg is not a numerical tuple
edit retag flag offensive close merge delete

Comments

did those pyramids actually work from anywhere, ever ?

berak gravatar imageberak ( 2016-09-05 12:39:43 -0500 )edit

what do you mean from anywhere? If I input an image to buildOpticalFlowPyramid it returns the image without error, the problem is when I input the pyramid to the calcOptFlowPyrLK

jads_96 gravatar imagejads_96 ( 2016-09-05 12:42:30 -0500 )edit

1 answer

Sort by » oldest newest most voted
0

answered 2019-08-28 08:27:07 -0500

lars27 gravatar image

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!

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-09-05 12:34:24 -0500

Seen: 178 times

Last updated: Sep 05 '16