opencv3.3.0、vs2013、win10-64bit
采集标定数据
拍摄15张不同角度棋盘格照片
工程全部源码:
#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(6, 9); /* 标定板上每行、列的角点数 */
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 (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(11, 11)); //对粗提取的角点进行精确化
image_points_seq.push_back(image_points_buf); //保存亚像素角点
/* 在图像上显示角点位置 */
drawChessboardCorners(view_gray, board_size, image_points_buf, true); //用于在图片中标记角点
imshow("Camera Calibration", view_gray);//显示图片
//自己添加,保存提取角点的照片
imwrite("cb_pic/Calibration_" + filename, 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); /* 实际测量得到的标定板上每个棋盘格的大小 */
Size square_size = Size(26, 26);
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(filePath);
Mat newimage = imageSource.clone();
//另一种不需要转换矩阵的方式
undistort(imageSource,newimage,cameraMatrix,distCoeffs);
//remap(imageSource, newimage, mapx, mapy, INTER_LINEAR);
StrStm.clear();
filePath.clear();
imageFileName = "";
StrStm << i + 1;
StrStm >> imageFileName;
imageFileName += "_校正.jpg";
imwrite(imageFileName, newimage);
}
std::cout << "保存结束" << endl;
system("pause");
return;
}
calibdata.txt
1.jpg
2.jpg
3.jpg
4.jpg
5.jpg
6.jpg
7.jpg
8.jpg
9.jpg
10.jpg
11.jpg
12.jpg
13.jpg
14.jpg
15.jpg
caliberation_result.txt
每幅图像的标定误差:
第1幅图像的平均误差:0.241588像素
第2幅图像的平均误差:0.211429像素
第3幅图像的平均误差:0.230653像素
第4幅图像的平均误差:0.203119像素
第5幅图像的平均误差:0.185326像素
第6幅图像的平均误差:0.266489像素
第7幅图像的平均误差:0.282381像素
第8幅图像的平均误差:0.388529像素
第9幅图像的平均误差:0.245399像素
第10幅图像的平均误差:0.456876像素
第11幅图像的平均误差:0.275083像素
第12幅图像的平均误差:0.467745像素
第13幅图像的平均误差:0.397879像素
第14幅图像的平均误差:0.515555像素
第15幅图像的平均误差:0.230653像素
总体平均误差:0.30658像素
相机内参数矩阵:
[1708.632618763115, 0, 1037.618278462937;
0, 1709.935844645911, 440.4320028121206;
0, 0, 1]
畸变系数:
[-0.4149622600415354, -0.6274939218341983, 0.002038504086887947, 0.001667415316039647, 2.950620880918864]
第1幅图像的旋转向量:
[103.3620933154484;
-37.27835468631045;
486.2473110385807]
第1幅图像的旋转矩阵:
[-0.4695542457480928, -0.8475304724624051, 0.2474083841497538;
0.7999121498215795, -0.5269832004218772, -0.2871049617142388;
0.3737102659384121, 0.06309361870084823, 0.9253971214627125]
第1幅图像的平移向量:
[-0.1239260946634385;
2.860818918393754;
0.1010685927771138]
第2幅图像的旋转向量:
[96.21243113651892;
-20.36573295337693;
457.072359332248]
第2幅图像的旋转矩阵:
[-0.7666264834360005, -0.539726005933306, 0.3478213239780344;
0.5066529222328757, -0.8412485637177951, -0.1886893490254142;
0.3944447379385856, 0.03157043810319744, 0.9183771862097476]
第2幅图像的平移向量:
[0.2759862850702536;
-3.131105390733862;
-0.1288065183428999]
第3幅图像的旋转向量:
[28.62053384242577;
-48.55148071075438;
474.5826950759648]
第3幅图像的旋转矩阵:
[0.9231213608890427, -0.3829541157955537, -0.03454125454593981;
0.3820153216208212, 0.9236409355343383, -0.03084989873877765;
0.04371781235723252, 0.01528291204247574, 0.9989270170949426]
第3幅图像的平移向量:
[0.2248030157566079;
-3.187032904324877;
-0.01911963564400004]
第4幅图像的旋转向量:
[138.7957476344684;
14.02148266932927;
472.2045457422368]
第4幅图像的旋转矩阵:
[-0.5279986594504157, -0.7072464314943884, 0.4701275366929887;
0.7338953744534918, -0.6585487675513055, -0.1664665134867241;
0.427334757507897, 0.2571303286178237, 0.8667577511222831]
第4幅图像的平移向量:
[-0.4419791530461736;
3.068490520987464;
-0.02942475664636893]
第5幅图像的旋转向量:
[120.6944186069347;
-4.653812533613165;
464.3429730124558]
第5幅图像的旋转矩阵:
[-0.5416567769223797, -0.7429314079623449, 0.3932694484410748;
0.7348999870529201, -0.645647480729175, -0.2075122638729749;
0.408080807002335, 0.176613288546353, 0.8956996155325539]
第5幅图像的平移向量:
[-0.5865660610159662;
3.016597169183211;
0.03981850680835582]
第6幅图像的旋转向量:
[85.07448959350688;
-48.74649054580596;
407.8901215082103]
第6幅图像的旋转矩阵:
[0.1434384836357674, 0.945701519260925, 0.2916745410210668;
-0.9878018985485377, 0.1187622433831608, 0.100712158007379;
0.06060371800620903, -0.3025626646071474, 0.9512008322902409]
第6幅图像的平移向量:
[0.03590347871664504;
-3.147923396665936;
-0.3676343087404068]
第7幅图像的旋转向量:
[88.2351308254345;
-29.42694709256779;
413.5154617044988]
第7幅图像的旋转矩阵:
[-0.8792144433526649, -0.2865908963645607, 0.3805885189033179;
0.2298082289481878, -0.9548756620597588, -0.1881505990254469;
0.4173369627863807, -0.07796235070144331, 0.9054014200150907]
第7幅图像的平移向量:
[0.1438032281706403;
-2.867367460078865;
-0.0258296916166515]
第8幅图像的旋转向量:
[112.5964231741936;
-22.0558800210169;
393.322705671191]
第8幅图像的旋转矩阵:
[0.3148697262802874, -0.9382211263654403, 0.1435206379366444;
0.9162935511589709, 0.2610466507799482, -0.3037445871469219;
0.2475140068292281, 0.227147010013639, 0.9418816551272271]
第8幅图像的平移向量:
[0.2094317148261692;
-2.741577050993684;
-0.03049759732873267]
第9幅图像的旋转向量:
[113.2424933560801;
-34.09078893430963;
455.3626394101503]
第9幅图像的旋转矩阵:
[0.7343889799955252, 0.6687222700073555, 0.1161178352250058;
-0.6785574074992685, 0.7195342384353411, 0.1477508864513006;
0.01525355004592779, -0.1872992400291125, 0.9821844653096043]
第9幅图像的平移向量:
[-0.1707249717294847;
2.94498993841457;
0.1273829249401757]
第10幅图像的旋转向量:
[116.3789814784561;
-6.087393262386425;
467.0465847759609]
第10幅图像的旋转矩阵:
[-0.6600133342982273, 0.6217012140360262, 0.4217463681108206;
-0.6324820716562626, -0.7627857716042112, 0.1346265043428317;
0.4053995900120507, -0.1778917286000093, 0.8966636522764613]
第10幅图像的平移向量:
[0.4823518263186308;
-2.865368701950058;
0.5374596877864528]
第11幅图像的旋转向量:
[111.8759327630554;
-26.72148804453201;
419.4108626765606]
第11幅图像的旋转矩阵:
[0.2637616581287882, -0.955017330888777, 0.1355421904914786;
0.9300939439364209, 0.2145642497071693, -0.2981399641114784;
0.2556463243505495, 0.2047048618125019, 0.9448496580918989]
第11幅图像的平移向量:
[0.3232439866192054;
-2.983760450807201;
-0.2991257137999916]
第12幅图像的旋转向量:
[96.01670066309281;
-26.64094179950646;
416.2274288205201]
第12幅图像的旋转矩阵:
[0.7567491679501738, -0.6535493041267192, 0.01428299276065265;
0.646395389787264, 0.7448499136126384, -0.1654436648923622;
0.09748690613867537, 0.1344318164227732, 0.9861158095603086]
第12幅图像的平移向量:
[0.3102020474366194;
-2.539242122567515;
0.04582090317991441]
第13幅图像的旋转向量:
[85.45676125889234;
-55.22279549234693;
521.9249453844111]
第13幅图像的旋转矩阵:
[-0.6381735831497893, 0.6901350832802412, 0.3412448455222926;
-0.7462662082946333, -0.6634686040267135, -0.05381596257984235;
0.1892649574731412, -0.2890034226382421, 0.9384326281497631]
第13幅图像的平移向量:
[-0.07906982782910149;
2.638663251319122;
0.1217742006109206]
第14幅图像的旋转向量:
[85.73782279989832;
-58.60019367622717;
421.4839813658672]
第14幅图像的旋转矩阵:
[0.858415908224653, -0.5112093786363903, -0.04227410201340551;
0.5033527011514616, 0.8553533097385324, -0.1225021377889612;
0.09878353481411511, 0.08387900043237265, 0.9915675098227525]
第14幅图像的平移向量:
[0.1416584286505432;
-3.075559951929528;
-0.7105707913914741]
第15幅图像的旋转向量:
[28.62053384242577;
-48.55148071075439;
474.5826950759649]
第15幅图像的旋转矩阵:
[0.9231213608889992, -0.3829541157956581, -0.03454125454594786;
0.3820153216209252, 0.923640935534295, -0.03084989873878834;
0.04371781235724577, 0.0152829120424776, 0.998927017094942]
第15幅图像的平移向量:
[0.2248030157566079;
-3.187032904324877;
-0.01911963564399981]
标定角点图片:(只贴了一张)
校正前图片与校正后图片:
源码及标定照片下载链接:https://download.csdn.net/download/chujicainiao1989/11313446