简体   繁体   中英

OpenCV Optical Flow assertion

I'm attempting to track landmarks along the contour of facial features obtained via dsift with python 2.7 and openCV 2.4.11. I want to track these features between frames.

However I am receiving the following error. I have checked the input images are 1-channel equal dimensions (and unsigned 8bit type), and likewise with the prev points:

OpenCV Error: Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, tru
e)) >= 0) in cv::calcOpticalFlowPyrLK, file ..\..\..\modules\video\src\lkpyramid.cpp
cv2.error: ..\..\..\modules\video\src\lkpyramid.cpp:845: error: (-215) (npoints
= prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function cv::calcOpticalFlowP
yrLK

Line causing issue:

new_pts, ttl, err = cv2.calcOpticalFlowPyrLK(self.old_img, i_img, i_old_pts, None)

Does anyone have any idea where I can begin debugging this?

I had the same problem when I did optical flow based tracking. I tried many many different ways to solve this. But in vain.

Finally, there was an example program in which they tracked using shi-tomsi corner points detection and those points were used in the LK algorithm and it worked perfectly. So i probed into the data types and dimensions of the output of the Shi-Tomsi detector and I made sure my points to be tracked were of the same type. It woked!

Here's what you need to know.

  • make sure the images are grayscale.
  • your coordinate parameter that is i_old_pts should be single precision float meaning float32. This type is available in numpy use that. the float in python is float64
  • the coordinate parameter i_old_pts(from your program) should be a numpy array with the dimension (n,1,2) where n represents the number of points.

This should work.

I was following opencv sample for optical flow had the same problem. In my case the problem was the type of video file from which I was reading. I was reading from mkv file which didn't work.

I am not sure why, but after a lot of code evaluation and debugging, I found that the first frame of my video (I captured video from the webcam) was an empty BLACK image. So, I used the second frame:

ret, prevframe = cap.read();
ret, prevframe = cap.read(); # the first frame was black and I was not sure why!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM