相机内参和畸变参数进行校正程序

当相机校正完成后,会得到相机的外参、内参、畸变参数

通过内参和畸变参数可以得到相机校正后的图像。两种方法,程序如下:


#include <opencv2/opencv.hpp>
#include <opencv2/calib3d/calib3d.hpp>
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 <opencv2/opencv.hpp>
#include <opencv2/calib3d/calib3d.hpp>
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;

}

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

想想叫啥名

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值