开始标定
double fisheye::calibrate(InputArrayOfArrays objectPoints, InputArrayOfArrays imagePoints,
const Size& image_size, InputOutputArray K, InputOutputArray D,
OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs, int flags=0,
TermCriteria criteria=TermCriteria(TermCriteria::COUNT + TermCriteria::
EPS, 100, DBL_EPSILON))
注意:K,D 分别表示内参矩阵和畸变系数向量,在定义时要定义为double型,这里推荐使用Matx33d和Vec4d数据类型,更为方便简单。objectPoints,imagePoints可以是float型,也可以是double型,但是再stereorectify中需要时double型。flags的可选项有很多,其中需要注意的是必须要指定CALIB_FIX_SKEW,代表求解时假设内参中fx=fy.
4.评定误差(可选项)
for (int i = 0; i != image_count; i++)
{
cout << "Frame #" << i + 1 << "..." << endl;
string image_Name;
stringstream stream;
stream << (i + startNum);
stream >> image_Name;
image_Name = path_ChessboardImage + image_Name + ".jpg";
cv::Mat image = imread(image_Name);
Mat image_gray;
cvtColor(image, image_gray, CV_RGB2GRAY);
vector<Point2f> corners;
bool patternFound = findChessboardCorners(image_gray, board_size, corners,
CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE + CALIB_CB_FAST_CHECK);
if (!patternFound || fullcornersNum != corners.size())
{
cout << "can not find chessboard corners!\n";
continue;
}
else
{
cornerSubPix(image_gray, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
count = count + corners.size();
corners_Seq.push_back(corners);
successImageNum = successImageNum + 1;
image_Seq.push_back(image);
}
}
/************************************************************************
摄像机定标
*************************************************************************/
vector<vector<Point3f>> object_Points; /**** 保存定标板上角点的三维坐标 ****/
Mat image_points = Mat(1, count, CV_32FC2, Scalar::all(0)); /***** 保存提取的所有角点 *****/
vector<int> point_counts;
/* 初始化定标板上角点的三维坐标 */
for (int t = 0; t<successImageNum; t++)
{
vector<Point3f> tempPointSet;
for (int i = 0; i<board_size.height; i++)
{
for (int j = 0; j<board_size.width; j++)
{
/* 假设定标板放在世界坐标系中z=0的平面上 */
Point3f tempPoint;
tempPoint.x = i*square_size.width;
tempPoint.y = j*square_size.height;
tempPoint.z = 0;
tempPointSet.push_back(tempPoint);
}
}
object_Points.push_back(tempPointSet);
}
for (int i = 0; i< successImageNum; i++)
{
point_counts.push_back(board_size.width*board_size.height);
}
/* 开始定标 */
Size image_size = image_Seq[0].size();
cv::Matx33d intrinsic_matrix; /***** 摄像机内参数矩阵 ****/
cv::Vec4d distortion_coeffs; /* 摄像机的4个畸变系数:k1,k2,k3,k4*/
std::vector<cv::Vec3d> rotation_vectors; /* 每幅图像的旋转向量 */
std::vector<cv::Vec3d> translation_vectors; /* 每幅图像的平移向量 */
int flags = 0;
flags |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
flags |= cv::fisheye::CALIB_CHECK_COND;
flags |= cv::fisheye::CALIB_FIX_SKEW;
fisheye::calibrate(object_Points, corners_Seq, image_size, intrinsic_matrix, distortion_coeffs, rotation_vectors, translation_vectors, flags, cv::TermCriteria(3, 20, 1e-6));
标定结果: