相机矫正与显示

1、注意事项;
2、Code;
3、效果;
4、参考;

说明

1、相机标定的棋盘格宽高要对应,棋盘格角点识别只寻找内角点(自带例程里是w*h = 6 * 9);
2、注意棋盘格默认坐标系在左上角,drawChessboardCorners()会默认将x方向绘制为红***r>
3、findChessboardCorners()之后必须进行粗角点提取(调用find4QuadCornerSubpix()函数),不然数据有问题;
4、棋盘格空间坐标系表示方法:(0,0,0) 、(50,0,0)、(100,0,0) 、…(0,50,0)、(50,50,0)…;这里使用OpenCV自带的棋盘格图像,棋盘格大小50mm;
5、通过重新计算空间点投影坐标(projectPoints())与计算矩阵范数来衡量相机标定的结果;
6、旋转向量需要调用Rodrigues(tvecsMat[i], rotation_matrix);转换为旋转矩阵;

7、畸变矩阵:(k1,k2,p1,p2,k3)(径向畸变,切向畸变);相机内参矩阵:相机内参矩阵总是以像素为单位表示的


Code

#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
   
	ifstream fin("calibdata.txt");   /*标定用图像文件路径*/
	ofstream fout("calibresult.txt"); /*保存标定结果的文件*/

	//读取每一幅图像,从中提取角点,然后对角点进行亚像素精确化

	cout << "开始提取角点...";
	int image_count = 0; /*图像数量*/
	Size image_size; /*图像尺寸*/
	Size board_size = Size(6, 9);  //标定板每行列角点数

	vector<Point2f> image_points_buf; /*缓存每幅图像上检测到的角点*/
	vector<vector<Point2f>> image_point_seq;  /*保存所有检测到的角点*/

	string filename;
	while (getline(fin,filename))
	{
   
		image_count++;
		// 用于观察检验输出
		cout << "image_count = " << image_count << endl;		
		Mat grayImg = imread(filename,IMREAD_GRAYSCALE);  //读入灰度图
		
		if (image_count == 1)
		{
   
			// 读入第一张图片时获取图像的宽高信息
			image_size.width = grayImg.cols;
			image_size.height = grayImg.rows;

			cout << "image_size.width = " << image_size.width << endl;
			cout << "image_size.height = " << image_size.height << endl;
		}
		if (0 == findChessboardCorners(grayImg, board_size, image_points_buf)) /*输入图像必须是8位灰度或彩色图像*/
		{
   
			cout << "can not find chessboard corners!\n";  //没有找到全部角点
			exit(1);
		}
		else
		{
   
			Mat rgbImg;
			cvtColor(grayImg, rgbImg, CV_GRAY2BGR);
			/*亚像素精确化*/
			find4QuadCornerSubpix(grayImg, image_points_buf, Size(11, 11));  //粗提取角点 精确化
			/*保存亚像素点*/
			image_point_seq.push_back(image_points_buf); 
			/*绘制出检测到的角点(彩色)*/
			drawChessboardCorners(rgbImg, board_size, image_points_buf, true);

			imshow("Camera Calibration", rgbImg);
			waitKey(500);  //暂停0.5s
		}
	}
	int total = image_point_seq.size();  //读取的总的图像数
	cout << "total = " << total << endl;
	int CornerNum = board_size.width * board_size.height;  /*总的角点数*/

	for (int i = 0; i < total; i++)
	{
   
		if (0 == i % CornerNum)
		{
   
			int j = -1;
			j = i / CornerNum;
			int k = j + 1;
			cout << "--> 第 " << k << "图片的数据 -->" << endl;
		}
		if (0 == i % 3)
		{
   
			cout << endl;
		}
		else
		{
   
			cout.width(10);
		}
		//输出所有图像中第一个角点坐标
		cout << "-->" << image_point_seq[i][0].x;
		cout << "-->" << image_point_seq[i][0].y;
	}
	cout << "角点提取完成!\n";

	//摄像机标定
	cout << "开始标定..." << endl;
	Size square_size = Size(50, 50); /*棋盘格大小50mm*/

	vector<vector<Point3f>>  object_points; /*保存棋盘上的坐标点*/
	vector<int>  point_counts; //每幅图像中的角点的数量

	Mat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0));  /*摄像机内参数矩阵*/
	Mat distCoeffs = Mat(1, 5, CV_32FC1, Scalar::all(0));  /*摄像机的5个畸变系数:k1,k2,p1,p2,k3*/

	vector<Mat> tvecsMat; /*存储每幅图像的旋转向量*/
	vector<Mat> rvecsMat; /*存储每幅图像的平移向量*/


	/*设置棋盘点的三维坐标*/
	for (int t = 0; t < image_count; t++)
	{
   
		vector<Point3f> tempPointSet;
		for (int i = 0; i < board_size.height; i++)   //一定从行开始(刚开始没注意写错了,找了半天)
		{
   
			for (int j = 0; j < board_size.width; j++)
			{
   
				Point3f realPoint;
				//注意x,y对应关系
				realPoint.x = j * square_size.width;  //注意棋盘格默认坐标系的位置
				realPoint.y = i * square_size.height;
				realPoint.z = 0;
				tempPointSet.push_back(realPoint);  /*添加一幅图像的所有角点坐标*/
			}
		}
		object_points.push_back(tempPointSet);
	}
	/*初始化每幅图像中的角点数量,假定每幅图像中都可以看到完整的标定板*/
	for (int i = 0; i < image_count; i++)
	{
   
		point_counts.push_back(board_size.width * board_size.height);
	}
	/*calibrate camera*/
	calibrateCamera(object_points, image_point_seq, image_size, cameraMatrix, distCoeffs, rvecsMat, tvecsMat, 0);

	cout << "标定完成!\n";

	//对标定结果进行评价 可以写成一个单独的函数
	cout << "开始评价标定结果.........\n";
	double total_err = 0.0;  //所有图像的平均误差的总和
	double err = 0.0;    /*每幅图像的平均误差*/
	vector<Point2f> image_points2; /*保存重新计算得到的投影点*/

	cout << "\t每幅图像的标定误差:\n";
	fout << "每幅图像的标定误差:\n";

	for (int i = 0; i < image_count; i++)
	{
   
		vector<Point3f> tempPointSet = object_points[i];
		/*通过得到摄像机内外参数、对空间的三维点进行重新投影计算,得到新的投影点*/
		projectPoints(tempPointSet, rvecsMat[i], tvecsMat[i], cameraMatrix, distCoeffs, image_points2);
		/*计算新的投影点和旧的投影点之间的误差*/
		vector<Point2f> tempImagePoint = image_point_seq[i];
		Mat tempImagePointMat = Mat(1, tempImagePoint.size(),CV_32FC2); //存储未矫正角点的坐标值
		Mat image_points2Mat = Mat(1, image_points2.size(), CV_32FC2);  //存储重新投影计算得到的投影角点坐标值

		for (int j = 0; j < tempImagePoint.size(); j++)
		{
   
			image_points2Mat.at<Vec2f>(0, j) = Vec2f(image_points2[j].x, image_points2[j].y);
			tempImagePointMat.at<Vec2f>(0, j) = Vec2f(tempImagePoint[j].x, tempImagePoint[j].y);
		}
		err = norm(image_points2Mat, tempImagePointMat, NORM_L2); /*矩阵范数运算,用来表征矩阵变化的大小*/
		total_err += (err /= point_counts[i]);
		cout << "第" << i + 1 << "幅图像平均误差: " << err << " 像素" << endl;
		fout << "第" << i + 1 << "幅图像平均误差: " << err << " 像素" << endl;  /*输出到对应文件*/

	}
	cout << "总体平均误差: " << total_err / image_count << "像素" << endl;
	fout << "总体平均误差: " << total_err / image_count << "像素" << endl;
	cout << "评价完成" << endl;

	/*保存标定结果*/
	cout << "开始保存标定结果...." << endl;
	Mat rotation_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); /*保存每幅图像的旋转矩阵*/

	fout << "相机内参数矩阵: " << endl;
	fout << cameraMatrix << endl << endl;

	fout << "畸变系数:\n";
	fout << distCoeffs << endl << endl << endl;

	for (int i = 0; i < image_count; i++)
	{
   
		fout << "第" << i + 1 << "幅图像的旋转向量:" << endl;
		fout << rvecsMat[i] << endl;
		/*将旋转向量对应的转换为旋转矩阵*/
		Rodrigues(rvecsMat[i], rotation_matrix);
		fout << "第" << i + 1 << "幅图像的旋转矩阵: " << endl;
		fout << rotation_matrix << endl;
		fout << "第" << i + 1 << "幅图像的平移矩阵: " << endl;
		fout << tvecsMat[i] << endl << endl;
	}
	cout << "保存完成" << endl;
	fout << endl;

	//显示矫正图像
	Mat src,dst, map1, map2;
	initUndistortRectifyMap(cameraMatrix, distCoeffs, Mat(),Mat(), image_size,CV_16SC2,map1,map2); /*计算一次数据,多次使用*/
	fin.clear();/*清除读到文件尾标志位*/
	fin.seekg(0, ios::beg);/*定位到文件开头*/
	
	while (getline(fin, filename))
	{
   
		src = imread(filename, IMREAD_GRAYSCALE);
		remap(src, dst, map1, map2,INTER_LINEAR);
		imshow("矫正图像", dst);
		waitKey(0);
	}	

	return 0;
}

运行效果

矫正显示图:




输出部分结果:

第1幅图像平均误差: 0.061354 像素
第2幅图像平均误差: 0.0556064 像素
第3幅图像平均误差: 0.0605988 像素
第4幅图像平均误差: 0.0613993 像素
第5幅图像平均误差: 0.0539623 像素
第6幅图像平均误差: 0.0568119 像素
第7幅图像平均误差: 0.0553855 像素
第8幅图像平均误差: 0.0675104 像素
第9幅图像平均误差: 0.0552258 像素
第10幅图像平均误差: 0.0535394 像素
第11幅图像平均误差: 0.0679826 像素
第12幅图像平均误差: 0.0553539 像素
第13幅图像平均误差: 0.05476 像素
总体平均误差: 0.0584223像素
相机内参数矩阵: 
[531.7211458925117, 0, 340.5939787009914;
 0, 531.6527520201263, 232.1816395637914;
 0, 0, 1]

畸变系数:
[-0.2649624107640241, -0.06179144766431392, 0.0007444186953668426, -0.0006773403737711846, 0.3589273922760235]


第1幅图像的旋转向量:
[-147.1977864401933;
 -211.9918058168564;
 794.5893885356636]
第1幅图像的旋转矩阵: 
[0.9783750415377405, 0.2008112739236345, 0.04956924814026904;
 -0.1988157450784841, 0.9791192015449368, -0.04240151736545007;
 -0.05704890537873422, 0.03162943931169756, 0.9978702325272139]
第1幅图像的平移矩阵: 
[-1.985290932382638;
 -2.010358828096781;
 0.1155827105495813]

参考

1、https://blog.csdn.net/weixin_30244681/article/details/95617791
2、https://blog.csdn.net/t247555529/article/details/47836233
3、https://blog.csdn.net/shenxiaolu1984/article/details/50165635