What does Mat::checkVector do in OpenCV?

rohit-biswas picture rohit-biswas · May 6, 2015 · Viewed 8.6k times · Source

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.

Answer

Kornel picture Kornel · May 7, 2015

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.