#include \#include
using namespace cvut; using namespace std;
#pragma comment(lib,\#pragma comment(lib,\#pragma comment(lib,\
void main() {
ifstream fin(\定标所用图像文件的路径 */
ofstream fout(\保存定标结果的文件 */
读取每一幅图像,从中提取出角点,然后对角点进行亚像素精确化
*************************************************************************/ cout<<\开始提取角点………………\int image_count=0; /* 图像数量 */ CvSize image_size; /* 图像的尺寸 */
CvSize board_size = cvSize(5,7); /* 定标板上每行、列的角点数 */ CvPoint2D32f*
image_points_buf
=
new
/************************************************************************
CvPoint2D32f[board_size.width*board_size.height]; /* 缓存每幅图像上检测到的角点 */
Seq
string filename;
while (getline(fin,filename)) {
image_count++; int count;
Image
/* 提取角点 */
if (0 == cvFindChessboardCorners( view.cvimage, board_size, {
cout<<\exit(1);
image_size.width = view.size().width; image_size.height = view.size().height;
image_points_buf, &count, CV_CALIB_CB_ADAPTIVE_THRESH ))
} else {
}
}
Image
view_gray.cvimage,
image_points_buf,
count,
cvSize(11,11),
cvSize(-1,-1), cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30,
0.1 ));
image_points_seq.push_back(image_points_buf,count); /* 在图像上显示角点位置 */
cvDrawChessboardCorners( view.cvimage, board_size, image_points_buf, view.show(\cvWaitKey(); view.close();
count, 1);
delete []image_points_buf; cout<<\角点提取完成!\\n\
/************************************************************************ 摄像机定标
*************************************************************************/ cout<<\开始定标………………\
CvSize square_size = cvSize(10,10); /* 实际测量得到的定标板上每个棋盘格的大小 */ Matrix
Matrix
/* 保存定标板上角点的三维坐标 */ 角点 */
/* 初始化定标板上角点的三维坐标 */ int i,j,t;
for (t=0;t for (i=0;i for (j=0;j /* 假设定标板放在世界坐标系中z=0的平面上 */ object_points(0,t*board_size.height*board_size.width+i*board_size.width+j,0) = i*square_size.width; 0; */ object_points(0,t*board_size.height*board_size.width+i*board_size.width+j,1) = j*square_size.height; object_points(0,t*board_size.height*board_size.width+i*board_size.width+j,2) = } /* 将角点的存储结构转换成矩阵形式 */ for (i=0;i /* 初始化每幅图像中的角点数量,这里我们假设每幅图像中都可以看到完整的定标板 */ for (i=0;i /* 开始定标 */ cvCalibrateCamera2(object_points.cvmat, /************************************************************************ 对定标结果进行评价 *************************************************************************/ cout<<\开始评价定标结果………………\\n\ double total_err = 0.0; /* 所有图像的平均误差的总和 */ double err = 0.0; /* 每幅图像的平均误差 */ Matrix image_points.cvmat, image_size, distortion_coeffs.cvmat, translation_vectors.cvmat, 0); point_counts(0,i) = board_size.width*board_size.height; image_points(0,i,0) = image_points_seq[i].x; image_points(0,i,1) = image_points_seq[i].y; } } point_counts.cvmat, intrinsic_matrix.cvmat, rotation_vectors.cvmat, cout<<\定标完成!\\n\ cout<<\每幅图像的定标误差:\\n\fout<<\每幅图像的定标误差:\\n\for (i=0;i at, /* 通过得到的摄像机内外参数,对空间的三维点进行重新投影计算,得到新的投影点 */ cvProjectPoints2(object_points.get_cols(i*point_counts(0,0,0),(i+1)*point_count err rotation_vectors.get_col(i).cvmat, translation_vectors.get_col(i).cvmat, intrinsic_matrix.cvmat, distortion_coeffs.cvmat, image_points2.cvmat, 0,0,0,0); = s(0,0,0)-1).cvmat, /* 计算新的投影点和旧的投影点之间的误差*/ cvNorm(image_points.get_cols(i*point_counts(0,0,0),(i+1)*point_counts(0,0,0)-1).cvm } cout<<\总体平均误差:\像素\fout<<\总体平均误差:\像素\cout<<\评价完成!\\n\ /************************************************************************ 保存定标结果 *************************************************************************/ cout<<\开始保存定标结果………………\ Matrix fout<<\相机内参数矩阵:\\n\fout< fout< fout<<\第\幅图像的旋转向量:\\n\fout< /* 将旋转向量转换为相对应的旋转矩阵 */ cvRodrigues2(rotation_vector.cvmat,rotation_matrix.cvmat); rotation_vector(j,0,0) = rotation_vectors(0,i,j); image_points2.cvmat, CV_L1); total_err += err/=point_counts(0,0,0); cout<<\第\幅图像的平均误差:\像素\fout<<\第\幅图像的平均误差:\像素\ } } fout<<\第\幅图像的旋转矩阵:\\n\fout< fout<<\第\幅图像的平移向量:\\n\fout< cout<<\完成保存\\n\