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;
videoIn.open( 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) {
videoIn.read( frame);
}
unsigned int frameNum= 0;
time_t tstart= time( 0);
while( videoIn.read( 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);
// PROBLEM OCCURS WITH THIS CALL TO THE L-K OPTICAL FLOW CODE!!!
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);
++frameNum;
}
return 0;
}
Thank you very much for your time,
-Nate