Ask Your Question
2

Can't use pyramids in calcOpticalFlowPyrLK - Python

asked 2012-07-13 06:06:56 -0600

anatoliy314 gravatar image

Version 2.4.1 is advertised to allow you to pass pyramids into calcOpticalFlowPyrLK. However, this doesn't work for me in Python:

>>> import cv2
>>> cv2.__version__
'2.4.1'
>>> prev_features = np.array((10, 10))
>>> im = np.zeros((100, 100), dtype = np.uint8)
>>> im2 = im.copy()
>>> pyr2 = cv2.buildOpticalFlowPyramid(im2, (8, 8), 3)
>>> pyr1 = cv2.buildOpticalFlowPyramid(im, (8, 8), 3)
>>> pyr2 = pyr2[1]
>>> pyr1 = pyr1[1]
>>> prev_features = np.array((10, 10), dtype = np.int32)
>>> res = cv2.calcOpticalFlowPyrLK(pyr1, pyr2, prev_features)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: <unknown> is not a numpy array

Whereas using simple images passes that check:

>>> res = cv2.calcOpticalFlowPyrLK(im, im2, prev_features)
OpenCV Error: Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0) in calcOpticalFlowPyrLK, file /home/anatoliy/Downloads/OpenCV-2.4.1/modules/video/src/lkpyramid.cpp, line 593
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cv2.error: /home/anatoliy/Downloads/OpenCV-2.4.1/modules/video/src/lkpyramid.cpp:593: error: (-215) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function calcOpticalFlowPyrLK

Thanks!

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
0

answered 2019-08-28 08:15:02 -0600

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.

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!

edit flag offensive delete link more
0

answered 2012-07-13 06:25:41 -0600

OpenCV 2.4.2 docs

http://docs.opencv.org/modules/video/doc/motionanalysisandobjecttracking.html?highlight=calcopticalflow#cv.CalcOpticalFlowPyrLK

In OpenCV 2.4.1 docs too, they had not mentioned about pyramids.

I had used cv2.OpticalFlowPyrLK a month ago. I too was trying to make pyramids. But in OpenCV >= 2.3.1, pyramids are not required. Here's how I use it.

template_pt = np.asarray(template_pt,dtype="float32")
target_pt = np.asarray(target_pt,dtype="float32")

target_pt, status, track_error = calcOpticalFlowPyrLK(img1, img2, template_pt, target_pt, 
                                                      winSize=(win_size_lk, win_size_lk),
                                                      flags = OPTFLOW_USE_INITIAL_FLOW,
                                                      criteria = (TERM_CRITERIA_EPS | TERM_CRITERIA_COUNT, 10, 0.03))
edit flag offensive delete link more

Comments

You are correct, pyramids are not required, but I want to use them. Since we are tracking a point, the pyramid in img2 will be needed when the next frame is processed, and img2 becomes prevImg. I am using the cv2 syntax, where the documentation you linked to says I can use the output of buildOpticalFlowPyramid.

anatoliy314 gravatar imageanatoliy314 ( 2012-07-13 07:52:57 -0600 )edit

I, too, would like to compute and reuse the pyramids. In addition to the redundant work mentioned above, the sample programs lk_homography and lk_track do both a forward and backward prediction, which means each pyramid will be computed 4 times.

headdab gravatar imageheaddab ( 2018-03-21 11:55:11 -0600 )edit

Question Tools

Stats

Asked: 2012-07-13 06:06:56 -0600

Seen: 3,676 times

Last updated: Jul 13 '12