Reputation: 133
I have searched everywhere on the net and on SO for this answer but I am not able to find the answers to my problems so I am posting it here.
I am trying to use the solvePnP function to find the relative camera pose between two images by obtaining the rvecs and tvecs .I here are the links to those two images. refernce image ,query image. I have detected and matched the keypoints and have them stored in vector Point3f (2D coordinates of refernce image+depth) and vector Point2f (2D coordinates for the query image in the environment). I already have the camera intrinsics and distortion matrices for the camera. Here are the code snippets I used.
drawMatches(fin,keypoints_1,fin1,keypoints_2,good_matches,Img_matches,Scalar::all(-1),vector<char>(),DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
imshow("mm",img_matches);
vector<Point2f> obj;
vector<Point2f> scene;
vector<Point3f> obj1;
for( int i = 0; i <good_matches.size(); i++ )
{
obj.push_back( keypoints_1[ good_matches[i].queryIdx ].pt);
scene.push_back( keypoints_2[ good_matches[i].trainIdx ].pt );}
for( int i = 0; i < obj.size(); i++ )
{raw=depth.at<unsigned char>(int(obj[i].x),int(obj[i].y));
distan=(raw*1798)/255;//this is just to get the actual distance of a point in a depth image
obj1.push_back(Point3f(int(obj[i].x),int(obj[i].y),distan));}//transfering to a Point3f vector.
Mat H = findHomography( obj, scene, CV_RANSAC );
solvePnP(obj1,scene,intrinsics,distortion,rvec,tvec);
NOTE: There are more than the usual 4 pairs of points in the vector obj1. Will this create problems?
The problem which I have is a run time error where I get this:
Unhandled exception at 0x75dc812f (KernelBase.dll) in test.exe: Microsoft C++ exception: cv::Exception at memory location 0x0015b800.
I have got the camera matrices from the xml cofiguration files that I had calibrated earlier
UPDATE: I found that the intrinsics and the distortion matrix have not been loaded properly.I wasted all this time looking into the object and scene matrices.I checked the matrice values using breakpoints and both of them show 0 columns and rows.I thought the matrices had not been stored properly during calibration and I checked the xml files ;but they have data. This is the piece of code I used to transfer the matrices into the program.
Mat intrinsics;
Mat distortion;
FileStorage fsIntrinsic("intrinsics.xml", FileStorage::READ);
fsIntrinsic["intrinsics"] >> intrinsics;
FileStorage fsDistortion("distortion.xml", FileStorage::READ);
fsDistortion["distortion"] >> distortion;
if(!fsInrinsic.isOpened())
return -1;
if(!fsDistortion.isOpened())
return -1;
fsIntrinsic.release();
fsDistortion.release();
could anyone help me out here. Everything I know seems right,here,in this code.Should I include something else in the code to make it work?
Upvotes: 0
Views: 755
Reputation: 133
I finally managed to clear up my doubts regarding the question. Analyzing through the breakpoints I found that the intrinsics matrix had loaded properly but the distortion coefficients matrix showed zero columns and rows. One basic mistake was that I had initialised 2 file storage variables which can be used only one at a time (i.e. after releasing one with the .release() function.
So the code snippet would be :
fsIntrinsic.release();//releasing the first filestorage variable.
FileStorage fsDistortion("distortion.xml", FileStorage::READ);
.
.
.
fsDistortion.release();
But this was a tedious task so I stored both the intrinsics and distortion coefficients matrix to the same file and accessed them using the same file storage variable as shown below:
FileStorage fs("intrinsicsdist.xml", FileStorage::READ);
fs["intrinsic"]>>intrinsics;
fs["dist"]>>distortion;
fs.release();
NOTE:
Always remember to put in the same name inside the square brackets as in intrinsic in fs["intrinsic"] from the code where you calibrated and saved your matrices. For example say after calibrating you save it as:
fs<<"intrinsic"<
the bold word should be the same when you are reading the matrix into the program. I wasted a lot of time ignoring this piece of information. Cheers!
Upvotes: 2