int _tmain(int argc, _TCHAR* argv[])
{
Mat cam_left;
Mat cam_right;
cam_left = imread("left.jpg");
cam_right = imread("right.jpg");
Size img_size = cam_left.size();
/*---相机内参-----
事先标定好的相机的参数
fx 0 cx
0 fy cy
0 0 1
---------------*/
double M1[9] = { 3.3013e+03, 0, 1.5684e+03,
0, 3.3141e+03, 2.0528e+03,
0, 0, 1 };
double D1[5] = { 0.0901, -0.0408, 0, 0, 0 };
Mat cameraMatrix_L = Mat(3, 3, CV_64FC1, M1);
Mat distortion_L = Mat(5, 1, CV_64FC1, D1);
double M2[9] = { 3.3245e+03, 0, 1.5400e+03,
0, 3.3099e+03, 2.0466e+03,
0, 0, 1 };
double D2[5] = { 0.1364, -0.3457, -8.9793e-04, -0.0036, 0.2408 };
Mat cameraMatrix_R = Mat(3, 3, CV_64FC1, M1);
Mat distortion_R = Mat(5, 1, CV_64FC1, D1);
Mat T = (Mat_<double>(3, 1) << -65.47301045400828, 4.226403576803492, -2.926971574200593);//T平移向量
Mat R = (Mat_<double>(3, 3) << 0.9981990515951071, 0.02792030732749352, -0.05309529012414368,
-0.0275811104170411, 0.9995942783087172, 0.007110634334427643,
0.05327227930914599, -0.005633401389326498, 0.9985641336669343);//rec旋转向量
//畸变校正
Mat mapLx, mapLy, mapRx, mapRy;
Mat h = Mat::eye(3, 3, CV_32F);
initUndistortRectifyMap(cameraMatrix_L, distortion_L, h, cameraMatrix_L, img_size, CV_32FC1, mapLx, mapLy);
initUndistortRectifyMap(cameraMatrix_R, distortion_R, h, cameraMatrix_R, img_size, CV_32FC1, mapRx, mapRy);
Mat rectify_L;
cam_left.copyTo(rectify_L);
Mat rectify_R = cam_right.clone();;
remap(rectify_L, rectify_L, mapLx, mapLy, INTER_LINEAR);
remap(rectify_R, rectify_R, mapRx, mapRy, INTER_LINEAR);
/*
立体校正
*/
Rect validROIL;//图像校正之后,会对图像进行裁剪,这里的validROI就是指裁剪之后的区域
Rect validROIR;
Mat mapLx, mapLy, mapRx, mapRy; //映射表
Mat Rl, Rr, Pl, Pr, Q; //校正旋转矩阵R,投影矩阵P 重投影矩阵Q
stereoRectify(cameraMatrix_L, distortion_L, cameraMatrix_R, distortion_R, img_size, R, T, Rl, Rr, Pl, Pr, Q, CALIB_ZERO_DISPARITY,
0, img_size, &validROIL, &validROIR);
initUndistortRectifyMap(cameraMatrix_L, distortion_L, Rl, Pl, img_size, CV_32FC1, mapLx, mapLy);
initUndistortRectifyMap(cameraMatrix_R, distortion_R, Rr, Pr, img_size, CV_32FC1, mapRx, mapRy);
Mat rectify_L;
cam_left.copyTo(rectify_L);
Mat rectify_R = cam_right.clone();;
remap(rectify_L, rectify_L, mapLx, mapLy, INTER_LINEAR);
remap(rectify_R, rectify_R, mapRx, mapRy, INTER_LINEAR);
return 0;
}