网上找的opencv相机标定代码,直接用手机拍照测试了一下,简单的测试只用了6张图片,踩到了坑:
报错信息:
0x00007FF8ADE94F69 处(位于 video.exe 中)有未经处理的异常: Microsoft C++ 异常:
cv::Exception,位于内存位置 0x000000E91C2FDBC0 处。
原因是图像的大小超过计算机屏幕分辨率大小。
用手机拍的分辨率太高了,4032*3024的大小,试着截图改成小分辨率后即可。
参考:https://blog.csdn.net/qq_34985235/article/details/106278369(文章对抛异常的总结很到位)
代码:
#include <iostream>
#include <fstream>
#include <io.h>
#include "opencv2/opencv.hpp"
#include <opencv2/imgproc/types_c.h>
using namespace cv;
using namespace std;
void LoadImages_TXT(string filename, vector<string>& imageNames)
{
// step 1 : open file
ifstream file(filename);
if (!file)
{
cout << "Wrong! File " << filename << " dose not exist!" << endl;
return;
}
// step 2 : load data
imageNames.clear();
while (!file.eof())
{
string imageName;
file >> imageName;
if (file.eof())
break;
imageNames.push_back(imageName);
cout << "Load Image, imageName: " << imageName << endl;
}
file.close();
return;
}
int main()
{
std::ofstream fout("caliberation_result.txt"); /* 保存标定结果的文件 */
//读取每一幅图像,从中提取出角点,然后对角点进行亚像素精确化
std::cout << "开始提取角点………………\n";
int image_count = 0; /* 图像数量 */
Size image_size; /* 图像的尺寸 */
Size board_size = Size(8, 5); /* 标定板上每行、列的角点数 */
vector<Point2f> image_points_buf; /* 缓存每幅图像上检测到的角点 */
vector<vector<Point2f>> image_points_seq; /* 保存检测到的所有角点 */
string filename;
int count = -1;//用于存储角点个数。
string dataFileName = "calibdata.txt"; /* 标定所用图像文件的路径 */
vector<string> imageNames;
LoadImages_TXT(dataFileName, imageNames);
for (int i = 0; i < imageNames.size(); i++)
{
image_count++;
// 用于观察检验输出
cout << "image_count = " << image_count << endl;
/* 输出检验*/
cout << "-->count = " << count << endl;
string imageName = imageNames[i];
Mat imageInput = imread(imageName);
//imshow("Camera Calibration", imageInput);//显示图片
//waitKey(100);//暂停0.1S
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)); //对粗提取的角点进行精确化
cv::cornerSubPix(view_gray, image_points_buf, cv::Size(11, 11), cv::Size(-1, -1), cv::TermCriteria(CV_TERMCRIT_ITER + CV_TERMCRIT_EPS, 20, 0.01));
image_points_seq.push_back(image_points_buf); //尾插,保存亚像素角点
/* 在图像上显示角点位置 */
//drawChessboardCorners(view_gray,board_size,image_points_buf,true); //用于在图片中标记角点
drawChessboardCorners(imageInput, board_size, image_points_buf, true); //用于在图片中标记角点
//imshow("Camera Calibration", imageInput);//显示图片
imwrite("Calibration" + to_string(image_count) + ".jpg", imageInput);//显示图片
//waitKey(10);//暂停0.01S
}
}
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)// 24 是每幅图片的角点个数。此判断语句是为了输出 图片号,便于控制台观看
{
int i = -1;
i = ii / CornerNum;
int j = i + 1;
cout << "--> 第 " << j << "图片的数据 --> : " << endl;
}
if (0 == ii % 3) // 此判断语句,格式化输出,便于控制台查看(3个图片信息显示一行)
{
cout << endl;
}
else
{
cout.width(10);
}
//输出所有的角点
cout << " -->" << image_points_seq[ii][0].x;
cout << " -->" << image_points_seq[ii][0].y;
}
cout << "角点提取完成!\n";
//
// step 2 : 2D calibration
//
//以下是摄像机标定
cout << "开始标定………………" << endl;
/*棋盘三维信息*/
Size square_size = Size(26, 26); /* 实际测量得到的标定板上每个棋盘格的大小 */
cout << "!!!!!!!!!!!!!!!!!!!!!!!1" << endl;
cout << square_size.height << endl;
cout << square_size.width << endl;
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)); /* 摄像机的5个畸变系数:k1,k2,p1,p2,k3 */
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;
/* 假设标定板放在世界坐标系中z=0的平面上 */
realPoint.x = i * square_size.height;
realPoint.y = j * square_size.width;
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<<tvecsMat[0]<<endl;
cout << "标定完成!\n";
//对标定结果进行评价
//cout << "开始评价标定结果………………\n";
double total_err = 0.0; /* 所有图像的平均误差的总和 */
double err = 0.0; /* 每幅图像的平均误差 */
vector<Point2f> image_points2; /* 保存重新计算得到的投影点 */
cout << "\t每幅图像的标定误差:\n";
fout << "每幅图像的标定误差:\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); //32位浮点型2通道
for (int j = 0; j < tempImagePoint.size(); j++) //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;
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;
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;
}
std::cout << "完成保存" << endl;
fout << endl;
}
运行结果:
Load Image, imageName: D:\pros_yx\Project4\video\video\images\1.png
Load Image, imageName: D:\pros_yx\Project4\video\video\images\2.png
Load Image, imageName: D:\pros_yx\Project4\video\video\images\3.png
Load Image, imageName: D:\pros_yx\Project4\video\video\images\4.png
Load Image, imageName: D:\pros_yx\Project4\video\video\images\5.png
Load Image, imageName: D:\pros_yx\Project4\video\video\images\6.png
image_count = 1
–>count = -1
image_size.width = 728
image_size.height = 971
image_count = 2
–>count = -1
image_count = 3
–>count = -1
image_count = 4
–>count = -1
image_count = 5
–>count = -1
image_count = 6
–>count = -1
total = 6
–> 第 1图片的数据 --> :
–>317.387 -->840.777 -->429.779 -->818.572 -->221.189 -->736.079
–>357.432 -->817.382 -->174.462 -->816.548 -->120.915 -->733.664角点提取完成!
开始标定………………
!!!1
26
26
[-15.76650163528249;
108.449633276219;
243.017432350663]
标定完成!
每幅图像的标定误差:
第1幅图像的平均误差:0.0681329像素
第2幅图像的平均误差:0.0681653像素
第3幅图像的平均误差:0.0789504像素
第4幅图像的平均误差:0.0882829像素
第5幅图像的平均误差:0.0723034像素
第6幅图像的平均误差:0.0625599像素
总体平均误差:0.0730658像素
评价完成!
开始保存定标结果………………
完成保存
保存的标定图像如下:
保存的结果文件如下: