当相机校正完成后,会得到相机的外参、内参、畸变参数。
通过内参和畸变参数可以得到相机校正后的图像。两种方法,程序如下:
#include
#include
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
VideoCapture capture(0);
while(1)
{
Mat frame;
capture >> frame;
flip(frame, frame, 1);
Mat distortion = frame.clone();
Mat camera_matrix = Mat(3,3,CV_32FC1);
Mat distortion_coefficients;
////导入相机内参和畸变系数矩阵
FileStorage file_storage("out_camera_data.yml", FileStorage::READ);
file_storage["camera_matrix"] >> camera_matrix;
file_storage["distortion_coefficients"]>>distortion_coefficients;
file_storage.release();
undistort(frame, distortion, camera_matrix, distortion_coefficients);
//第一个参数src,输入参数,代表畸变的原始图像;
//第二个参数dst,矫正后的输出图像,跟输入图像具有相同的类型和大小;
//第三个参数cameraMatrix为之前求得的相机的内参矩阵;
//第四个参数distCoeffs为之前求得的相机畸变矩阵;
//第五个参数newCameraMatrix,默认跟cameraMatrix保持一致;
//方法一相比方法二执行效率更高一些,推荐使用
imshow("原图", frame);
imshow("校正图", distortion);
if (waitKey(30) > 0) break;
}
return 0;
}
#include
#include
using namespace std;
using namespace cv;
int test(int argc, char** argv)
{
VideoCapture capture(0);
while(1)
{
Mat frame;
capture >> frame;
Size image_size;
image_size = frame.size();
flip(frame, frame, 1);
Mat distortion = frame.clone();
Mat R = Mat::eye(3, 3, CV_32FC1);
Mat camera_matrix = Mat(3,3,CV_32FC1);
Mat distortion_coefficients;
////导入相机内参和畸变系数矩阵
Mat mapx = Mat(image_size, CV_32FC1);
Mat mapy = Mat(image_size, CV_32FC1);
FileStorage file_storage("out_camera_data.yml", FileStorage::READ);
file_storage["camera_matrix"] >> camera_matrix;
file_storage["distortion_coefficients"]>>distortion_coefficients;
file_storage.release();
initUndistortRectifyMap(camera_matrix, distortion_coefficients, R, camera_matrix,
image_size, CV_32FC1, mapx, mapy);
//第一个参数cameraMatrix为之前求得的相机的内参矩阵;
//第二个参数distCoeffs为之前求得的相机畸变矩阵;
//第三个参数R,可选的输入,是第一和第二相机坐标之间的旋转矩阵;
//第四个参数newCameraMatrix,输入的校正后的3X3摄像机矩阵;
//第五个参数size,摄像机采集的无失真的图像尺寸;
//第六个参数m1type,定义map1的数据类型,可以是CV_32FC1或者CV_16SC2;
//第七个参数map1和第八个参数map2,输出的X / Y坐标重映射参数
remap(frame, distortion, mapx, mapy, INTER_LINEAR);
//第一个参数src,输入参数,代表畸变的原始图像;
//第二个参数dst,矫正后的输出图像,跟输入图像具有相同的类型和大小;
//第三个参数map1和第四个参数map2,X坐标和Y坐标的映射;
//第五个参数interpolation,定义图像的插值方式;
//第六个参数borderMode,定义边界填充方式
imshow("原图", frame);
imshow("校正图", distortion);
if (waitKey(30) > 0) break;
}
return 0;
}