文章内容:
- 读取棋盘格图片进行标定
- 生成棋盘格图片
- 保存标定后的内容
#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 cv;
using namespace std;
int main()
{
std::vector<cv::String> images;
std::string path = "./images/*.jpg";
cv::glob(path, images);
if(images.size() == 0)
{
cout << "path is error" << endl;
return 0;
}
int image_count = 0;
Size image_size;
Size board_size = Size(9, 6);
vector<Point2f> image_points_buf;
vector<vector<Point2f>> image_points_seq;
for (int i = 0; i < images.size(); i++)
{
image_count++;
cout << "image_count: " << image_count << endl;
Mat imageInput = cv::imread(images[i]);
if(imageInput.empty())
{
cout << "read error" << endl;
return 0;
}
if (image_count == 1)
{
image_size.width = imageInput.cols;
image_size.height = imageInput.rows;
cout << "image_size.width = " << image_size.width << endl;
cout << "image_size.height = " << image_size.height << endl;
}
if (0 == findChessboardCorners(imageInput, board_size, image_points_buf))
{
cout << "can not find chessboard corners!\n";
exit(1);
}
else
{
Mat view_gray;
cvtColor(imageInput, view_gray, COLOR_RGB2GRAY);
find4QuadCornerSubpix(view_gray, image_points_buf, Size(5, 5));
image_points_seq.push_back(image_points_buf);
drawChessboardCorners(view_gray, board_size, image_points_buf, false);
imshow("Camera Calibration", view_gray);
waitKey(500);
}
}
int total = image_points_seq.size();
cout << "total = " << total << endl;
int CornerNum = board_size.width * board_size.height;
for (int ii = 0 ; ii < total ; ii++)
{
if (0 == ii % CornerNum)
{
int i = -1;
i = ii / CornerNum;
int j = i + 1;
cout << "--> 第 " << j << "图片的数据 --> : " << endl;
}
if (0 == ii % 3)
{
cout << endl;
}
else
{
cout.width(10);
}
cout << " -->" << image_points_seq[ii][0].x;
cout << " -->" << image_points_seq[ii][0].y;
}
cout << "角点提取完成!\n";
cout << "开始标定………………";
Size square_size = Size(10, 10);
vector<vector<Point3f>> object_points;
Mat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0));
vector<int> point_counts;
Mat distCoeffs = Mat(1, 5, CV_32FC1, Scalar::all(0));
vector<Mat> tvecsMat;
vector<Mat> rvecsMat;
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;
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++)
{
point_counts.push_back(board_size.width * board_size.height);
}
calibrateCamera(object_points, image_points_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";
cout << "每幅图像的标定误差:\n";
for (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_points_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];
std::cout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;
cout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;
}
std::cout << "总体平均误差:" << total_err / image_count << "像素" << endl;
cout << "总体平均误差:" << total_err / image_count << "像素" << endl << endl;
std::cout << "评价完成!" << endl;
std::cout << "开始保存定标结果………………" << endl;
Mat rotation_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0));
cout << "相机内参数矩阵:" << endl;
cout << cameraMatrix << endl << endl;
cout << "畸变系数:\n";
cout << distCoeffs << endl << endl << endl;
for (int i = 0; i < image_count; i++)
{
cout << "第" << i + 1 << "幅图像的旋转向量:" << endl;
cout << rvecsMat[i] << endl;
Rodrigues(rvecsMat[i], rotation_matrix);
cout << "第" << i + 1 << "幅图像的旋转矩阵:" << endl;
cout << rotation_matrix << endl;
cout << "第" << i + 1 << "幅图像的平移向量:" << endl;
cout << tvecsMat[i] << endl << endl;
}
std::cout << "完成保存" << endl;
cout << endl;
Mat mapx = Mat(image_size, CV_32FC1);
Mat mapy = Mat(image_size, CV_32FC1);
Mat R = Mat::eye(3, 3, CV_32F);
std::cout << "保存矫正图像" << endl;
string imageFileName;
std::stringstream StrStm;
for (int i = 0 ; i < image_count ; i++)
{
std::cout << "Frame #" << i + 1 << "..." << endl;
initUndistortRectifyMap(cameraMatrix, distCoeffs, R, cameraMatrix, image_size, CV_32FC1, mapx, mapy);
StrStm.clear();
cout << images[i] << endl;
Mat imageSource = imread(images[i]);
Mat newimage = imageSource.clone();
remap(imageSource, newimage, mapx, mapy, INTER_LINEAR);
StrStm.clear();
StrStm << i + 1;
StrStm >> imageFileName;
imageFileName += "_d.jpg";
imwrite(imageFileName, newimage);
}
std::cout << "保存结束" << endl;
}