user3377126
user3377126

Reputation: 2171

Cannot figure out what input to use to get cv2.calcOpticalFlowPyrLK method to work

I am trying to make a program in Python OpenCV where the user can define points on a video and optical flow would track it.

I tried to create my own coordinates in the form of a numpy array and tried to pass it into the calcOpticalFlowPyrLK method in OpenCV, but I get an error instead:

Traceback (most recent call last):
    p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, d, None, **lk_params)
error: /build/buildd/opencv-2.4.8+dfsg1/modules/video/src/lkpyramid.cpp:593:
error: (-215) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function calcOpticalFlowPyrLK

My code:

# params for ShiTomasi corner detection
feature_params = dict( maxCorners = 1,
                       qualityLevel = 0.01,
                       minDistance = 10,
                       blockSize = 7 )


p0 = np.array([[[348.0, 251.0]]])
# calculate optical flow
p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params)

I know that this is caused by the p0 variable because if I make p0 this:

p0 = cv2.goodFeaturesToTrack(old_gray, mask = mask_use, **feature_params)

And pass it into the calcOpticalFlowPyrLK parameter, it works. However I am trying to make a program in which the user defines the points, so if I create my own coordinates and pass p0 into the calcOpticalFlowPyrLK parameter like this:

d = np.array([[[348.0, 251.0]]])
p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, d, None, **lk_params)

Then I end up getting the error.

What numpy array do I have to create that the calcOpticalFlowPyrLK method would accept?

Upvotes: 1

Views: 2406

Answers (2)

Anagnostou John
Anagnostou John

Reputation: 514

For opencv3 use p0=np.array([[348.0, 251.0]],np.float32)

Upvotes: 1

s-low
s-low

Reputation: 706

From the docs:

prevPts – vector of 2D points for which the flow needs to be found; point coordinates must be single-precision floating-point numbers

so p0 must be a vector of 2D points:

p0 = [[x0, y0], [x1, y1], [x2, y2]]

So with just your one point, I would expect this to work:

p0 = [[348.0, 251.0]]

I think you've just used too many brackets and then too few.

Upvotes: 1

Related Questions