前提:摄像头固定因为摄像头一动,内参不变(畸变系数),但是外参(坐标变换)会变。
通过拍摄几张标定板的照片,然后得到畸变系数和相机内外参系数,然后每次读取摄像机图片时,将这些系数带进去,计算之后就可以得到矫正后的图片了。
效果如下:
显然上面图片四周直线都是弯曲的,被矫正后,变得效果不错了。
标定图:
程序在第三部分,具体步骤如下:
1、将第三步的代码复制到工程里
2、 在工程目录下(主函数.cpp相同目录下)建立一个caliberation文件夹,采集10——20张照片(不同角度,方向,但是要把角点全部显示出来),将照片放入该文件夹下。
效果如下:
3、新建一个calibdata.txt文件,将步骤2的图片路径写进去格式如下:
./caliberation/1.jpg
./caliberation/2.jpg
./caliberation/3.jpg
./caliberation/4.jpg
./caliberation/5.jpg
./caliberation/6.jpg
./caliberation/7.jpg
./caliberation/8.jpg
./caliberation/9.jpg
./caliberation/10.jpg
./caliberation/11.jpg
./caliberation/12.jpg
4、新建一个chess文件夹(名字随便,记得在程序里改),用于保存畸变校正后的图片。
5、运行程序,会生成一个caliberation_result.txt文件,里面保存了内外参等一些参数。比如畸变系数,旋转矩阵,平移矩阵等。
//2018.6.19:畸变校正
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include
#include
using namespace cv;
using namespace std;
void main()
{
ifstream fin("calibdata.txt"); /* 标定所用图像文件的路径 */
ofstream fout("caliberation_result.txt"); /* 保存标定结果的文件 */
//读取每一幅图像,从中提取出角点,然后对角点进行亚像素精确化
cout << "开始提取角点………………";
int image_count = 0; /* 图像数量 */
Size image_size; /* 图像的尺寸 */
Size board_size = Size(4, 6); /* 标定板上每行、列的角点数 */
vector image_points_buf; /* 缓存每幅图像上检测到的角点 */
vector> image_points_seq; /* 保存检测到的所有角点 */
string filename;
int count = -1;//用于存储角点个数。
while (getline(fin, filename))
{
image_count++;
// 用于观察检验输出
cout << "image_count = " << image_count << endl;
/* 输出检验*/
cout << "-->count = " << count;
Mat imageInput = imread(filename);
if (imageInput.empty())
{
cout << "can not open pic!\n";
exit(-1);
}
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, CV_RGB2GRAY);
/* 亚像素精确化 */
find4QuadCornerSubpix(view_gray, image_points_buf, Size(5, 5)); //对粗提取的角点进行精确化
//cornerSubPix(view_gray,image_points_buf,Size(5,5),Size(-1,-1),TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,0.1));
image_points_seq.push_back(image_points_buf); //保存亚像素角点
/* 在图像上显示角点位置 */
drawChessboardCorners(view_gray, board_size, image_points_buf, false); //用于在图片中标记角点
namedWindow("Camera Calibration", 0);//创建窗口
imshow("Camera Calibration", view_gray);//显示图片
waitKey(500);//暂停0.5S
}
}
int total = image_points_seq.size();
cout << "total = " << total << endl;
int CornerNum = board_size.width*board_size.height; //每张图片上总的角点数
for (int ii = 0; ii 第 " << 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> object_points; /* 保存标定板上角点的三维坐标 */
/*内外参数*/
Mat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); /* 摄像机内参数矩阵 */
vector point_counts; // 每幅图像中角点的数量
Mat distCoeffs = Mat(1, 5, CV_32FC1, Scalar::all(0)); /* 摄像机的5个畸变系数:k1,k2,p1,p2,k3 */
vector tvecsMat; /* 每幅图像的旋转向量 */
vector rvecsMat; /* 每幅图像的平移向量 */
/* 初始化标定板上角点的三维坐标 */
int i, j, t;
for (t = 0; t tempPointSet;
for (i = 0; i image_points2; /* 保存重新计算得到的投影点 */
cout << "\t每幅图像的标定误差:\n";
fout << "每幅图像的标定误差:\n";
for (i = 0; i tempPointSet = object_points[i];
/* 通过得到的摄像机内外参数,对空间的三维点进行重新投影计算,得到新的投影点 */
projectPoints(tempPointSet, rvecsMat[i], tvecsMat[i], cameraMatrix, distCoeffs, image_points2);
/* 计算新的投影点和旧的投影点之间的误差*/
vector 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(0, j) = Vec2f(image_points2[j].x, image_points2[j].y);
tempImagePointMat.at(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;
fout << "第" << i + 1 << "幅图像的平均误差:" << err << "像素" << endl;
}
std::cout << "总体平均误差:" << total_err / image_count << "像素" << endl;
fout << "总体平均误差:" << total_err / image_count << "像素" << endl << endl;
std::cout << "评价完成!" << 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> imageFileName;
filePath += imageFileName;
filePath += ".jpg";
Mat imageSource = imread("1.jpg"); //读取畸变图片
Mat newimage = imageSource.clone(); //校正后输出图片
//另一种不需要转换矩阵的方式
// undistort(imageSource,newimage,cameraMatrix,distCoeffs);
remap(imageSource, newimage, mapx, mapy, INTER_LINEAR);
StrStm.clear();
filePath.clear();
StrStm << i + 1;
StrStm >> imageFileName;
imageFileName += "_d.jpg";
imwrite(imageFileName, newimage);
}
std::cout << "保存结束" << endl;
return;
}
这个部分就是将得到的参数,应用到具体的程序中,不用每次进行标定,只要摄像头位置不变,就可以将畸变参数带进去就可以矫正。
void InitMat(Mat& m, float* num)
{
for (int i = 0; i(i, j) = *(num + i * m.rows + j);
}
int main()
{
int i = 1000;
int n = 1;
Mat edges;
Mat frame = imread("2.jpg"); //读取畸变图片
Mat R = Mat::eye(3, 3, CV_32F);
Size image_size; /* 图像的尺寸 */
//获取图像大小
image_size.width = 1920;
image_size.height = 1080;
//cameraMatrix为 "相机内参数矩阵:" << endl;
Mat mapx = Mat(image_size, CV_32FC1);
Mat mapy = Mat(image_size, CV_32FC1);
//参数矩阵
float neican_data[] = { 9558.649257742036, 0, 959.3165310990756, 0, 9435.752651759443, 532.7507141910969, 0, 0, 1 };
Mat cameraMatrix(3, 3, CV_32FC1);
InitMat(cameraMatrix, neican_data);
cout << "cameraMatrix= " << endl << " " << cameraMatrix << endl << endl;
//测得的畸变系数
float jibian_data[] = { -6.956561513881647, -68.83902522804168, -0.004834538444671919, 0.01471273691928269, -0.4916103704308509 };
Mat distCoeffs(1, 5, CV_32FC1); /* 摄像机的5个畸变系数:k1,k2,p1,p2,k3 */
InitMat(distCoeffs, jibian_data);
cout << "distCoeffs= " << endl << " " << distCoeffs << endl << endl;
i = 0;
namedWindow("【原始图】", 0);//参数为零,则可以自由拖动
imshow("【原始图】", frame);
/********相机矫正*******************************************************************************/
initUndistortRectifyMap(cameraMatrix, distCoeffs, R, cameraMatrix, image_size, CV_32FC1, mapx, mapy);
Mat imageSource = frame; //读取畸变图片
Mat newimage = imageSource.clone(); //校正后输出图片
remap(imageSource, newimage, mapx, mapy, INTER_LINEAR);
namedWindow("畸变校正后的图片", 0);//参数为零,则可以自由拖动
imshow("畸变校正后的图片", newimage);
}
上面只是矫正部分的代码