我試圖做視頻穩定在OpenCV的2.4.12在Ubuntu下面的代碼,我使用calcopticalflowpyrlk會拋出一個斷言失敗錯誤: 錯誤是以下幾點:calcopticalflowpyrlk斷言失敗
OpenCV的錯誤:斷言失敗((npoints = prevPtsMat.checkVector(2,CV_32F,true))> = 0)in cv :: calcOpticalFlowPyrLK,file home ... \ opencv \ modules \ video \ src \ lkpyramid.cpp,line 845
這裏是代碼:
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <iostream>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main()
{
int rows = 240;
int cols = 240;
int i = 1;
VideoCapture cap(0);
Mat Canvass;
Canvass.create(rows, cols * 2, CV_8UC3);
for(;;)
{Mat prev,cur;
Mat prev_gray,cur_gray, aligned;
Mat TT(2, 3, CV_64F);
vector <Point2f> cur_corner, cur_corner2, prev_corner2;
vector <Point2f> prev_corner;
Mat prev_grey;
vector <uchar> status;
vector <float> err;
double dx, dy, da;
const int max_no_of_corners = 20000;
const double quality_of_corners = 0.00001;
const double min_dist_bw_corners = 5;
const int search_window_size = 80;
const int pyramid_level = 5;
Mat cur_warped;
int rows = 240;
int cols = 240;
if(i == 1)
{ //capture the prev frame,resize,grayscale and display it
for (int x = 0; x < 10;x++)
{ cap >> prev; }
resize(prev, prev,Size(240,240));
cvtColor(prev,prev_gray,CV_BGR2GRAY);
prev.copyTo(Canvass(Range::all(), Range(0,cols)));
//capture the current frame,resize and grayscale it
cap>>cur;
resize(cur,cur,Size(240,240));
cvtColor(cur,cur_gray,CV_BGR2GRAY);
i++;
}
else{
cap>>cur;
resize(cur,cur,Size(240,240));
cvtColor(cur,cur_gray,CV_BGR2GRAY);
cvtColor(prev,prev_gray,CV_BGR2GRAY);
//display the previous image in canvass
cur.copyTo(Canvass(Range::all(), Range(0,cols)));
}
///find corner points here
goodFeaturesToTrack(prev_grey, prev_corner, max_no_of_corners, quality_of_corners, min_dist_bw_corners);
//usecalcopticalflowpyrlk
calcOpticalFlowPyrLK(prev_gray, cur_gray, prev_corner, cur_corner, status, err, Size(10,10),2);
//weed out bad matches
for (int t = 0; t < status.size(); t++) {
if (int(status[t])) {
cur_corner2.push_back(cur_corner[t]);
prev_corner2.push_back(prev_corner[t]);
}
}
//estimate transform
Mat T;
T = estimateRigidTransform(prev_corner2, cur_corner2,false);
if (T.data == NULL)
{cout<<"No transform found"<<endl;
cur.copyTo(Canvass(Range::all(), Range(cols,cols * 2)));
cur.copyTo(prev);
}
else{
cout<<"transform found "<<endl;
// decompose T
dx = T.at<double>(0, 2);
dy = T.at<double>(1, 2);
da = atan2(T.at<double>(1, 0), T.at<double>(0, 0));
da = -da;
//da = 0;
TT.at<double>(0, 0) = cos(da);;
TT.at<double>(0, 1) = -sin(da);
TT.at<double>(1, 0) = sin(da);
TT.at<double>(1, 1) = cos(da);
TT.at<double>(0, 2) = -dx;///scale;
TT.at<double>(1, 2) = -dy;///scale;
//TT.copyTo(Transform);
//warp
warpAffine(cur, cur_warped, TT, cur.size(),INTER_NEAREST|WARP_INVERSE_MAP);
cur.copyTo(prev);
cur_warped.copyTo(Canvass(Range::all(), Range(cols,(cols * 2))));
}
imshow("Output",Canvass);
waitKey(1);
}
}
我認爲在calcopticalflowpyrlk中可能存在一些向量數據類型的問題,但我已經用文檔驗證過它們,但仍然遇到了這個錯誤。我該如何解決這個問題?