Ask Your Question
2

Can't use pyramids in calcOpticalFlowPyrLK - Python

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

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

1 answer

Sort by ยป oldest newest most voted
0

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

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 -0500 )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 -0500 )edit
Login/Signup to Answer

Question Tools

Stats

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

Seen: 2,444 times

Last updated: Jul 13 '12