11

I tried to use the following function in OpenCV (C++)

calcOpticalFlowPyrLK(prev_frame_gray, frame_gray, points[0], points[1], status, err, winSize, 3, termcrit, 0, 0.001);

and I get this error

OpenCV Error: Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0) in calcOpticalFlowPyrLK,
file /home/rohit/OpenCV_src/opencv-2.4.9/modules/video/src/lkpyramid.cpp, line 845
terminate called after throwing an instance of 'cv::Exception'
what():  /home/rohit/OpenCV_src/opencv-2.4.9/modules/video/src/lkpyramid.cpp:845:
error: (-215) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function calcOpticalFlowPyrLK

Both of the following return -1

frame_gray.checkVector(2, CV_32F, true)
prev_frame_gray.checkVector(2, CV_32F, true)

I wanted to know what checkVector actually does because it is leading to the assertion error as you can see above.

rohit-biswas
  • 815
  • 1
  • 11
  • 23
  • 1
    Please provide the definition of the input variables/parameters of `calcOpticalFlowPyrLK` – Antonio May 06 '15 at 21:09
  • I forgot to add the definitions. But, It's solved now. Basically, I hadn't initialized points[0] and hence the error. I would still like to know what checkVector does as I couldn't find it in the documentation. – rohit-biswas May 06 '15 at 21:52

2 Answers2

15

The official OpenCV's doc says:

cv::Mat::checkVector() returns N if the matrix is 1-channel (N x ptdim) or ptdim-channel (1 x N) or (N x 1); negative number otherwise

OpenCV considers some data types equivalent in case of some functions i.e. objectPoints of cv::solvePnP() can be:

  • 1xN/Nx1 1-channel cv::Mat
  • 3xN/Nx3 3-channel cv::Mat
  • std::vector<cv::Point3f>

With checkVector you can make sure that you are passing the correct representation of your data.

MM.
  • 4,224
  • 5
  • 37
  • 74
Kornel
  • 5,264
  • 2
  • 21
  • 28
0

I had a similar issue with cv2.projectPoints function (-215:Assertion failed) because openCV was expecting a nx3 matrix and i was passing an 1D array of length 3. Try:

points[0].reshape(-1,3)

As argument to the function. It changes the shape (3,) to shape (1,3).

Gaussiano
  • 1
  • 2