首先我們打開攝像頭并按下‘g’鍵開始標定:
?。踓pp] view plain copy print?
VideoCapture cap(1);
cap.set(CV_CAP_PROP_FRAME_WIDTH,640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT,480);
if(!cap.isOpened()){
std::cout《《“打開攝像頭失敗,退出”;
exit(-1);
}
namedWindow(“Calibration”);
std::cout《《“Press ‘g’ to start capturing images!”《《endl;
VideoCapture cap(1);
cap.set(CV_CAP_PROP_FRAME_WIDTH,640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT,480);
if(!cap.isOpened()){
std::cout《《“打開攝像頭失敗,退出”;
exit(-1);
}
namedWindow(“Calibration”);
std::cout《《“Press ‘g’ to start capturing images!”《《endl;
?。踓pp] view plain copy print?
if( cap.isOpened() && key == ‘g’ )
{
《span style=“white-space:pre”》 《/span》mode = CAPTURING;
}
if( cap.isOpened() && key == ‘g’ )
{
《span style=“white-space:pre”》 《/span》mode = CAPTURING;
}
按下空格鍵(SPACE)后使用findChessboardCorners函數(shù)在當前幀尋找是否存在可用于標定的角點,如果存在將其提取出來后亞像素化并壓入角點集合,保存當前圖像:
[cpp] view plain copy print?
if( (key & 255) == 32 )
{
image_size = frame.size();
/* 提取角點 */
Mat imageGray;
cvtColor(frame, imageGray , CV_RGB2GRAY);
bool patternfound = findChessboardCorners(frame, board_size, corners,CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE + CALIB_CB_FAST_CHECK );
if (patternfound)
{
n++;
tempname《《n;
tempname》》filename;
filename+=“.jpg”;
/* 亞像素精確化 */
cornerSubPix(imageGray, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
count += corners.size();
corners_Seq.push_back(corners);
imwrite(filename,frame);
tempname.clear();
filename.clear();
}
else
{
std::cout《《“Detect Failed.\n”;
}
}
if( (key & 255) == 32 )
{
image_size = frame.size();
/* 提取角點 */
Mat imageGray;
cvtColor(frame, imageGray , CV_RGB2GRAY);
bool patternfound = findChessboardCorners(frame, board_size, corners,CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE + CALIB_CB_FAST_CHECK );
if (patternfound)
{
n++;
tempname《《n;
tempname》》filename;
filename+=“.jpg”;
/* 亞像素精確化 */
cornerSubPix(imageGray, corners, Size(11, 11), Size(-1, -1), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 30, 0.1));
count += corners.size();
corners_Seq.push_back(corners);
imwrite(filename,frame);
tempname.clear();
filename.clear();
}
else
{
std::cout《《“Detect Failed.\n”;
}
}
角點提取完成后開始標定,首先初始化定標板上角點的三維坐標:
?。踓pp] view plain copy print?
for (int t=0;t《image_count;t++)
{
《span style=“white-space:pre”》 《/span》vector《Point3f》 tempPointSet;
for (int i=0;i《board_size.height;i++)
{
《span style=“white-space:pre”》 《/span》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);
《span style=“white-space:pre”》 《/span》}
}
object_Points.push_back(tempPointSet);
}
for (int t=0;t《image_count;t++)
{
《span style=“white-space:pre”》 《/span》vector《Point3f》 tempPointSet;
for (int i=0;i《board_size.height;i++)
{
《span style=“white-space:pre”》 《/span》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);
《span style=“white-space:pre”》 《/span》}
}
object_Points.push_back(tempPointSet);
}
使用calibrateCamera函數(shù)開始標定:
?。踓pp] view plain copy print?
calibrateCamera(object_Points, corners_Seq, image_size, intrinsic_matrix ,distortion_coeffs, rotation_vectors, translation_vectors);
calibrateCamera(object_Points, corners_Seq, image_size, intrinsic_matrix ,distortion_coeffs, rotation_vectors, translation_vectors);
完成定標后對標定進行評價,計算標定誤差并寫入文件:
?。踓pp] view plain copy print?
std::cout《《“每幅圖像的定標誤差:”《《endl;
fout《《“每幅圖像的定標誤差:”《《endl《《endl;
for (int i=0; i《image_count; i++)
{
vector《Point3f》 tempPointSet = object_Points[i];
/**** 通過得到的攝像機內(nèi)外參數(shù),對空間的三維點進行重新投影計算,得到新的投影點 ****/
projectPoints(tempPointSet, rotation_vectors[i], translation_vectors[i], intrinsic_matrix, distortion_coeffs, image_points2);
/* 計算新的投影點和舊的投影點之間的誤差*/
vector《Point2f》 tempImagePoint = corners_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《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;
fout《《“每幅圖像的定標誤差:”《《endl《《endl;
for (int i=0; i《image_count; i++)
{
vector《Point3f》 tempPointSet = object_Points[i];
/**** 通過得到的攝像機內(nèi)外參數(shù),對空間的三維點進行重新投影計算,得到新的投影點 ****/
projectPoints(tempPointSet, rotation_vectors[i], translation_vectors[i], intrinsic_matrix, distortion_coeffs, image_points2);
/* 計算新的投影點和舊的投影點之間的誤差*/
vector《Point2f》 tempImagePoint = corners_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《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;
顯示標定結(jié)果并寫入文件:
[cpp] view plain copy print?
std::cout《《“開始保存定標結(jié)果………………”《《endl;
Mat rotation_matrix = Mat(3,3,CV_32FC1, Scalar::all(0)); /* 保存每幅圖像的旋轉(zhuǎn)矩陣 */
fout《《“相機內(nèi)參數(shù)矩陣:”《《endl;
fout《《intrinsic_matrix《《endl《《endl;
fout《《“畸變系數(shù):\n”;
fout《《distortion_coeffs《《endl《《endl《《endl;
for (int i=0; i《image_count; i++)
{
fout《《“第”《《i+1《《“幅圖像的旋轉(zhuǎn)向量:”《《endl;
fout《《rotation_vectors[i]《《endl;
/* 將旋轉(zhuǎn)向量轉(zhuǎn)換為相對應的旋轉(zhuǎn)矩陣 */
Rodrigues(rotation_vectors[i],rotation_matrix);
fout《《“第”《《i+1《《“幅圖像的旋轉(zhuǎn)矩陣:”《《endl;
fout《《rotation_matrix《《endl;
fout《《“第”《《i+1《《“幅圖像的平移向量:”《《endl;
fout《《translation_vectors[i]《《endl《《endl;
}
std::cout《《“完成保存”《《endl;
fout《《endl;
std::cout《《“開始保存定標結(jié)果………………”《《endl;
Mat rotation_matrix = Mat(3,3,CV_32FC1, Scalar::all(0)); /* 保存每幅圖像的旋轉(zhuǎn)矩陣 */
fout《《“相機內(nèi)參數(shù)矩陣:”《《endl;
fout《《intrinsic_matrix《《endl《《endl;
fout《《“畸變系數(shù):\n”;
fout《《distortion_coeffs《《endl《《endl《《endl;
for (int i=0; i《image_count; i++)
{
fout《《“第”《《i+1《《“幅圖像的旋轉(zhuǎn)向量:”《《endl;
fout《《rotation_vectors[i]《《endl;
/* 將旋轉(zhuǎn)向量轉(zhuǎn)換為相對應的旋轉(zhuǎn)矩陣 */
Rodrigues(rotation_vectors[i],rotation_matrix);
fout《《“第”《《i+1《《“幅圖像的旋轉(zhuǎn)矩陣:”《《endl;
fout《《rotation_matrix《《endl;
fout《《“第”《《i+1《《“幅圖像的平移向量:”《《endl;
fout《《translation_vectors[i]《《endl《《endl;
}
std::cout《《“完成保存”《《endl;
fout《《endl;
具體的代碼實現(xiàn)和工程詳見:Calibration
運行截圖:
下一節(jié)我們將使用RPP相機姿態(tài)算法得到相機的外部參數(shù):旋轉(zhuǎn)和平移。
2015/11/14補充:
所有分辨率下的畸變(k1,k2,p1,p2)相同,但內(nèi)參不同(fx,fy,u0,v0),不同分辨率下需要重新標定相機內(nèi)參。以下是羅技C920在1920*1080下的內(nèi)參:
2016/08/20補充:
findChessboardCorners函數(shù)的第二個參數(shù)是定義棋盤格的橫縱內(nèi)角點個數(shù),要設置正確,不然函數(shù)找不到合適的角點,返回false。如下圖中的橫內(nèi)角點是12,縱內(nèi)角點是7,則Size board_size = Size(12, 7);
評論