关于相机标定的坐标变换和镜头畸变等理论知识可以参考《学习OpenCV》(有很多印刷错误,尤其是公式,看的时候要注意)的第十一章----相机模型与标定或者参考《学习OpenCV3》(最好是英文版,中文的一言难尽,谷歌翻译,错别字,错标题)第十八章----相机模型与标定。里面讲的相当清楚,包括理论知识和OpenCV中提供的函数等。
话不多说,直接上代码(因为本人使用的是工业相机,没有考虑率太多畸变内容,所以此代码只只含有标定部分,没有矫正部分,如果需要可以根据上面提供的参考书籍加上矫正代码):
注意:将标定图像和保存标定图像的文本文件放在同一目录下!
说明:为了让代码更好理解,代码中的注释和输出语句可能表较多,望见谅!
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <fstream>
using namespace std;
using namespace cv;
int main()
{
ifstream fin("caliberation_pics.txt"); // 存放所有标定图片名称的文本
ofstream fout("caliberation_results.txt"); // 用于存放标定结果的文本
cout << "----------------------------提取角点--------------------------------" << endl << endl;
// 读取一副标定图像,提取其中的角点并进行亚像素精确化
cout << ">>>开始提取角点" << endl << endl << endl;
int image_count = 0; // 图像的数量
Size image_size; // 图像的大小
Size board_size = Size(9, 6); // 标定板上,每行每列角点的个数
vector<Point2f> corners; // 缓存每幅图像上检测到的角点数据
vector<vector<Point2f>> corners_seq; // 缓存所有检测到的角点序列
string filename; // 用于接收图像的名称
int count = 0; // 存储角点的个数
while (getline(fin, filename)) // 获取文件中每一行的数据,即获取文件中的图像名字
{
cout << filename << endl;
image_count++;
Mat imageInput = imread(filename); // 读取图像
Mat gray_image;
cout << ">>>正在读取第" << image_count << "张图像的信息......" << endl;
if (image_count > 0)
{
image_size.width = imageInput.cols;
image_size.height = imageInput.rows;
cout << "图像的宽度为: " << image_size.width << endl;
cout << "图像的高度为: " << image_size.height << endl;
cout << "图像的通道数为:" << imageInput.channels() << endl;
if (imageInput.channels() == 3)
{
cout << "是否需要灰度化: 是" << endl;
cvtColor(imageInput, gray_image, CV_RGB2GRAY); // 灰度化
}
else
{
cout << "是否需要灰度化: 否" << endl;
}
}
cout << "正在提取第" << image_count << "张图像的角点" << endl << ">>>" << endl;
// 提取角点
int patternfound = findChessboardCorners(gray_image, board_size, corners, CV_CALIB_CB_ADAPTIVE_THRESH);
cout <<"已找到"<< corners.size()<<"个角点" << endl;
if (patternfound)
{
cout << ">>>进行亚像素精确化........" << endl;
// 亚像素精确化
find4QuadCornerSubpix(gray_image, corners, Size(11, 11)); //对粗提取的角点进行精确化
cout << "亚像素精确化完成!" << endl << endl;
corners_seq.push_back(corners);
// 在图像上显示角点位置
//cout << ">>>正在绘制角点" << endl;
//drawChessboardCorners(gray_image, board_size, corners, true); //用于在图片中标记角点
//cout << ">>>正在显示角点" << endl << endl;
//imshow("Image Corners", gray_image);//显示图片
//waitKey(500);//暂停0.5S
//destroyWindow("Image Corners");
}
}
int total_corners = corners_seq.size(); // 所有图片的总角点数
cout << "存放角点的序列大小为: " << total_corners << endl;
int corners_per_image = board_size.width * board_size.height; // 每张图片上的角点数
cout << "每张图片上的角点数为: " << corners_per_image <<endl;
for (int i = 0; i < image_count; i++)
{
cout << ">>>正在输出第" << i + 1 << "张图片的第一个角点数据........" << endl;
cout << corners_seq[i][0].x << endl;
cout << corners_seq[i][0].y << endl << endl;
}
cout << "角点提取完成!" << endl << endl;
cout << "---------------------------相机标定---------------------------------" << endl << endl;
// 摄像机标定
cout << ">>>开始进行相机标定" << endl << endl;
// 棋盘的三维信息
Size square_size = Size(30, 30); // 实际测量得到的标定板上每个棋盘格的大小
vector<vector<Point3f>> object_points; // 用于保存板上角点的三维坐标
// 声明内外参数
Mat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); // 初始化相机的内参矩阵
vector<int> corners_perimage; // 每幅图像的角点数量
Mat distCoeffs = Mat(1, 5, CV_32FC1, Scalar::all(0)); // 初始化相机的畸变系数矩阵
vector<Mat> rMat; // 每幅图像的旋转向量
vector<Mat> tMat; // 每幅图像的平移向量
// 初始化标定板上角点的三维坐标
int i, j, t;
for (t = 0; t < image_count; t++)
{
vector<Point3f> tempPointSet;
for (i = 0; i < board_size.height; i++)
{
for (j = 0; j < board_size.width; j++)
{
Point3f realPoint;
// 假设标定板放在世界坐标系中Z=0的平面上
realPoint.x = i * square_size.width;
realPoint.y = j * square_size.height;
realPoint.z = 0;
tempPointSet.push_back(realPoint);
}
}
object_points.push_back(tempPointSet);
}
/* 初始化每幅图像中的角点数量,假定每幅图像中都可以看到完整的标定板 */
for (i = 0; i < image_count; i++)
{
corners_perimage.push_back(board_size.width*board_size.height);
}
cout << ">>>正在调用calibrateCamera函数........." << endl;
calibrateCamera(object_points, corners_seq, image_size, cameraMatrix, distCoeffs, rMat, tMat, 0);
cout << "标定完成!" << endl << endl;
cout << "---------------------------评价标定结果---------------------------------" << endl << endl;
cout << ">>>开始评价标定结果.............." << endl;
double total_err = 0.0; /* 所有图像的平均误差的总和 */
double err = 0.0; /* 每幅图像的平均误差 */
vector<Point2f> image_points2; /* 保存重新计算得到的投影点 */
cout << "每幅图像的标定误差:\n";
fout << "每幅图像的标定误差:\n";
for (i = 0; i < image_count; i++)
{
vector<Point3f> tempPointSet = object_points[i];
/* 通过得到的摄像机内外参数,对空间的三维点进行重新投影计算,得到新的投影点 */
projectPoints(tempPointSet, rMat[i], tMat[i], cameraMatrix, distCoeffs, image_points2);
/* 计算新的投影点和旧的投影点之间的误差*/
vector<Point2f> tempImagePoint = corners_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 /= corners_perimage[i];
cout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;
fout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;
}
cout << "总体平均误差: " << total_err / image_count << "像素" << endl;
fout << "总体平均误差: " << total_err / image_count << "像素" << endl << endl;
cout << "评价完成!" << endl << endl;
cout << "---------------------------保存标定结果---------------------------------" << endl << endl;
std::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 << rMat[i] << endl;
/* 将旋转向量转换为相对应的旋转矩阵 */
Rodrigues(rMat[i], rotation_matrix);
fout << "第" << i + 1 << "幅图像的旋转矩阵:" << endl;
fout << rotation_matrix << endl;
fout << "第" << i + 1 << "幅图像的平移向量:" << endl;
fout << tMat[i] << endl << endl;
}
cout << "完成保存!" << endl;
fout << endl;
cout << "标定完成,按任意键退出!" << endl;
getchar();
destroyAllWindows();
return 0;
}