Ask Your Question

Assertion failure with call to cv::calcOpticalFlowPyrLK(...) (ANSWERED)

asked 2014-04-17 15:23:36 -0500

Nate gravatar image

updated 2014-04-17 17:06:29 -0500

Greetings all,

I am having trouble integrating a call to cv::calcOpticalFlowPyrLK(...) into my (C++) code. Ultimately, I wish to create a video stabilization routine.

I get the following assertion error with the call to cv::calcOpticalFlowPyrLK(...):

OpenCV Error: Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0) in cv::calcOpticalFlowPyrLK, file C:\...\opencv\modules\video\src\lkpyramid.cpp, line 593

As far as I can tell, my code follows the example L-K optical flow code in sources\samples\cpp. My code follows. The assertion error occurs with the call directly following the "// PROBLEM OCCURS WITH THIS CALL TO THE L-K OPTICAL FLOW CODE!!!" comment.

If it makes any difference, I am using OpenCV 2.4.7 with Microsoft Visual Studio 2012. I can routinely compile and run other code that utilizes OpenCV.

#include "ConjoinImages.h"

#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/video/video.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/videostab/videostab.hpp"

#include <iostream>
#include <string>
#include <time.h>

int _tmain(int argc, _TCHAR* argv[]) {

   std::string videoFilename= "my_video.mp4";

   // Open input video.
   std::cout<< "Attempting to open input video file:  "<< videoFilename<< std::endl;
   cv::VideoCapture videoIn; videoFilename);
   if( !videoIn.isOpened())
      throw "Error when reading stream!";
   std::cout<< "Video input file open."<< std::endl;

   // Run everything through the stabilizer...
   cv::Mat frame;
   cv::Mat stabilizedFrame;
   bool initialized= false;

   for( unsigned int ii= 0; ii< 30; ++ii) { frame);

   unsigned int frameNum= 0;
   time_t tstart= time( 0);
   while( frame)) {

      // need to grayscale the image
      if( frame.type()!= CV_8U)
         cv::cvtColor( frame, frame, cv::COLOR_BGR2GRAY);

      // Get the features that need to be tracked.
      int maxCorners= 30;
      cv::vector< cv::Point2f> frameFeatures;
      cv::goodFeaturesToTrack( frame, frameFeatures, maxCorners, 0.01, 30);
      cv::vector< cv::Point2d> prevFeatures;
      cv::goodFeaturesToTrack( stabilizedFrame, prevFeatures, maxCorners, 0.01, 30);

      if( frameFeatures.size()== prevFeatures.size()) {
         // Calculate the Lucas-Kanade optical flow between the two frames.
         cv::vector< uchar> status;
         status.reserve( maxCorners);
         cv::vector< float> errors;
         errors.reserve( maxCorners);

         cv::calcOpticalFlowPyrLK( stabilizedFrame, frame, prevFeatures, frameFeatures, status, errors);

         // Find the homography transformation between the frames.
         cv::Mat H= cv::findHomography( prevFeatures, frameFeatures);

         if( !H.empty()) {
            // Warp the current frame to match the previous frame.
            cv::warpPerspective( frame, stabilizedFrame, H, frame.size(), cv::INTER_NEAREST| cv::WARP_INVERSE_MAP);
         else {
            stabilizedFrame= frame;
      else {
         stabilizedFrame= frame;

      // Display.
      cv::Mat output;
      ConjoinImages::TwoSideBySide( frame, stabilizedFrame, output);
      cv::imshow( "stabilization", output);
      cv::waitKey( 1);


   return 0;

Thank you very much for your time,


edit retag flag offensive close merge delete


Looking at the OpenCV source code in lkpyramid.cpp, the error seems like it is a problem with the data format for prevFeatures, though I'm not sure what else I could use... If you have any idea, please let me know.

Nate gravatar imageNate ( 2014-04-17 15:26:54 -0500 )edit

Alright, I apparently can't answer my own question with a proper answer, so here it is as a comment instead.

The problem was a typo. The variable prevFeatures should be of type cv::vector< cv::Point2f>, not cv::vector< cv::Point2d>. (Note the f and d.) D'oh!

Nate gravatar imageNate ( 2014-04-17 17:10:12 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2018-06-25 02:34:38 -0500

hmchung gravatar image

This is mainly to help others in the future since Nate has solved the problem himself.

The Vector has to store Points of type Float.

vector < cv::Point2f >

instead of, say,

vector < cv::Point2i >

I got into the same issue and this helped.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools


Asked: 2014-04-17 15:23:36 -0500

Seen: 3,098 times

Last updated: Apr 17 '14