Assertion failed while using findEssentialMat()
I am using findEssentialMat() function so I can later recover the relative pose (translation and rotation) between two following frames of a webcam with recoverPose(). However, I get an asserion failed error:
OpenCV Error: Assertion failed(!fixedType() && !fixedSize()) in create, file /home/patrchri/opencv/modules/core/src/matrix.cpp, line 2297
To be more specific, I have written the following in a while loop which reads the following frames :
vector<KeyPoint> kpointsframe1;
vector<KeyPoint> kpointsframe2;
vector<Point2f> pointsframe1;
vector<Point2f> pointsframe2;
vector<Point2f> velocityvect;
vector<uchar> status;
vector<float> err;
Size winSize = Size(21,21);
TermCriteria termcrit = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS,30,0.01);
Mat Essential,R,T;
capture.read(frame1);
capture.read(frame2);
undistort(frame1,frame1undist,CameraMatrix,distCoeffs);
undistort(frame2,frame2undist,CameraMatrix,distCoeffs);
cvtColor(frame1undist,frame1gray,CV_BGR2GRAY);
cvtColor(frame2undist,frame2gray,CV_BGR2GRAY);
//goodFeaturesToTrack(frame1gray,corners,500,qualityLevel,minDistance,Mat(),blockSize,useHarrisDetector);
FAST(frame1gray,kpointsframe1,THRESHOLD_DIFFERENCE,false,FastFeatureDetector::TYPE_9_16);
for(int i=0;i<kpointsframe1.size();i++) pointsframe1.push_back(kpointsframe1.at(i).pt);
calcOpticalFlowPyrLK(frame1gray,frame2gray,pointsframe1,pointsframe2,status,err,winSize,3,termcrit,0,0.001);
int ind = 0;
for( int i=0; i<status.size(); i++){
Point2f pt = pointsframe2.at(i- ind);
if ((status.at(i) == 0)||(pt.x<0)||(pt.y<0)){
if((pt.x<0)||(pt.y<0)){
status.at(i) = 0;
}
pointsframe1.erase(pointsframe1.begin()+(i-ind));
pointsframe2.erase(pointsframe2.begin()+(i-ind));
ind++;
}
}
Point2d pp(320,240);
double focal = 9.5327626068874099e+02;
Essential = findEssentialMat(pointsframe1,pointsframe2,CameraMatrix,RANSAC,0.999,1,Mat());
recoverPose(Essential,pointsframe1,pointsframe2,CameraMatrix,R,T,Mat());
I have declared the CameraMatrix outside this while loop.
The issue arises from the findEssentialMat() but I am confused on what is wrong with it. I searched for similar assertion failures but I couldn't find something that it would be helpful. Surely the error comes from the sizes of the Mats used. What could be wrong with it?
Thank you for your answers and for your time in advance,
Chris
1st EDIT:
The CameraMatrix has been declared as follows after the callibration of the webcam:
double data[3][3];
data[0][0] = 9.5327626068874099e+02;
data[0][1] = 0.0;
data[0][2] = 320.;
data[1][0] = 0.0;
data[1][1] = 9.5327626068874099e+02;
data[1][2] = 240.0;
data[2][0] = 0.0;
data[2][1] = 0.0;
data[2][2] = 1.0;
double avg_reprojection_error = 3.5640854681839190e-01;
Mat CameraMatrix(3,3,CV_64F,&data);
2nd EDIT:
The full printscreen of the error is the following:
If I follow the trace I see that it comes from:
void _OutputArray::create(int d, const int* sizes, int mtype, int i,
bool allowTransposed, int fixedDepthMask) const
{
int k = kind();
mtype = CV_MAT_TYPE(mtype);
if( k == MAT )
{
CV_Assert( i < 0 );
Mat& m = *(Mat*)obj;
if( allowTransposed )
{
if( !m.isContinuous() )
{
--->CV_Assert(!fixedType() && !fixedSize());<---- (That is line 2297)
m.release();
}
if( d == 2 && m.dims == 2 && m.data &&
m.type() == mtype && m.rows == sizes[1] && m.cols == sizes[0] )
return;
}
if(fixedType())
{
if(CV_MAT_CN(mtype) == m.channels() && ((1 << CV_MAT_TYPE(flags)) & fixedDepthMask) != 0 ...
Can we get more information from the stack trace? That's coming from inside the Mat::create method. Can you go up to where that's being called from inside findEssentialMat? Which line there?
I'm sorry, I meant the Call Stack. I'm not sure how to access it there, but it should show every method that has been called, from main down to the create() function. I want to know at what line in findEssentialMat the create that's failing is coming from. There are many creates called in that function, and which one matters.
I can find the decleration of the function in calib3d.hpp, but I cannot find the .cpp file that the findEssentialMat() is being implemented in, because it is not being used with this name in the file.
EDITED: Oops, nevermind I found it...wait for me to reedit please.
I have marked with arrows the possible locations of the failure, but I cannot see why the error happens.
No, the error is definitely from the Assert in Mat::create, but we want to know why. Do you know how to use GDB? Run your program with GDB, have it die, and use backtrace to get the call stack at the segfault.
I don't know how to use gdb or generally debugging tools, although the tutorial is quite helpful. I am in codeblocks, so I guess I will use the debugger of codeblocks for which I saw there is the GDB/CDB debugger. I will try this...
Ok, Codeblocks? Google shows me that it has a Call Stack window. I want a screenshot of that. Then right click and "Jump to this File/Line" on the one with the findEssentialMat function in it and see what line it is.
I have posted a screenshot with the call stack window, but I see no line when I am trying to jump on the line of the file generating the error. I am reediting again to show you what I mean.