Skip to content

Instantly share code, notes, and snippets.

/gist:6586653
Created Sep 16, 2013

Embed
What would you like to do?
#include <iostream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <fstream>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat img1 = imread("img_1.jpg"); //image
Mat img2 = imread("img_2.jpg");
//camera intrinsics
Mat M = Mat(3,3,CV_32FC1);
M.at<float>(0,0) = 3547.32095;
M.at<float>(0,1) = 0;
M.at<float>(0,2) = 2296.10068;
M.at<float>(1,0) = 0;
M.at<float>(1,1) = 3593.17957;
M.at<float>(1,2) = 2374.71460;
M.at<float>(2,0) = 0;
M.at<float>(2,1) = 0;
M.at<float>(2,2) = 1;
Mat D = Mat(5,1,CV_32FC1); //distortions
D.at<float>(0,0) = -0.24966;
D.at<float>(1,0) = 0.10080;
D.at<float>(2,0) = 0.00353;
D.at<float>(3,0) = -0.00097;
D.at<float>(4,0) = 0;
//Extrinsics
Mat R = Mat(3,3,CV_32FC1);
R.at<float>(0,0) = 0.9753;
R.at<float>(0,1) = 0.1494;
R.at<float>(0,2) = -0.1625;
R.at<float>(1,0) = -0.1482;
R.at<float>(1,1) = 0.9888;
R.at<float>(1,2) = 0.0200;
R.at<float>(2,0) = 0.1636;
R.at<float>(2,1) = 0.0046;
R.at<float>(2,2) = 0.9865;
Mat T = Mat(3,1,CV_32FC1);
T.at<float>(0,0) = -2376.6;
T.at<float>(1,0) = -74.7;
T.at<float>(2,0) = 293;
Mat R1, R2, P1, P2, Q;
stereoRectify(M,D,M,D,img1.size(),R,T,R1,R2,P1,P2,Q);
Mat map1x, map1y, map2x, map2y;
Mat imgdst1, imgdst2;
initUndistortRectifyMap(M,D,R1,P1,img1.size(),CV_32FC1,map1x,map1y);
initUndistortRectifyMap(M,D,R2,P2,img1.size(),CV_32FC1,map2x,map2y);
remap(img1,imgdst1,map1x,map1y,INTER_LINEAR,BORDER_CONSTANT,Scalar());
remap(img2,imgdst2,map2x,map2y,INTER_LINEAR,BORDER_CONSTANT,Scalar());
namedWindow("image1");
namedWindow("image2");
imshow("image1",imgdst1);
imshow("image2",imgdst2);
imwrite("DSC_0906_rect.jpg",imgdst1);
imwrite("DSC_0913_rect.jpg",imgdst2);
waitKey();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.