看了Learning OpenCV的第11章Camera Models and Calibration,知道每個鏡頭都有鏡面曲率,這會影響到攝影的結果,讓原本是方形的物體成像後變成桶狀,這對影像處理影響深遠,所以有必要做相機矯正 (Camera Calibration)。

這裡使用一個鏡頭和一張棋盤,如圖所示,擷取多張不同角度的棋盤影像。

接著程式會找尋影像中的「角」,判斷是否為我們所指定的棋盤,並且在影像上做標記。

最後使用相機內部參數來矯正影像。

範例程式11-1我修改如下:
/**
Theme: calibrate the camera
Compiler: Dev C++ 4.9.9.2
Date: 101/01/03
Author: HappyMan
Blog: https://cg2010studio.wordpress.com/
*/
// Reading a chessboard's width and height, reading and collecting the
// requested number of views, and calibrating the camera
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
int n_boards = 0; //Will be set by input list
int board_dt = 90; //Wait 90 frames per chessboard view
int board_w;
int board_h;
int main() {
CvCapture* capture;// = cvCreateCameraCapture( 0 );
// assert( capture );
board_w = 6;
board_h = 9;
n_boards = 5;
board_dt = 20;
int board_n = board_w * board_h;// 格子數=寬*高
int board_nx = (board_w-1) * (board_h-1);// 內部角個數=(寬-1)*(高-1)
CvSize board_sz = cvSize( board_w-1, board_h-1 );// 內部row和column的角個數
capture = cvCreateCameraCapture( 0 );
if(!capture) {
printf("\nCouldn't open the camera\n");
system("pause");
return -1;
}
cvNamedWindow( "Calibration" );
cvNamedWindow( "Raw Video");
//ALLOCATE STORAGE
CvMat* image_points = cvCreateMat(n_boards*board_n,2,CV_32FC1);
CvMat* object_points = cvCreateMat(n_boards*board_n,3,CV_32FC1);
CvMat* point_counts = cvCreateMat(n_boards,1,CV_32SC1);
CvMat* intrinsic_matrix = cvCreateMat(3,3,CV_32FC1);
CvMat* distortion_coeffs = cvCreateMat(4,1,CV_32FC1);
CvPoint2D32f* corners = new CvPoint2D32f[ board_n ];
int corner_count;
int successes = 0;
int step, frame = 0;
IplImage *image = cvQueryFrame( capture );
IplImage *gray_image = cvCreateImage(cvGetSize(image),8,1);//subpixel
// CAPTURE CORNER VIEWS LOOP UNTIL WE?E GOT n_boards
// SUCCESSFUL CAPTURES (ALL CORNERS ON THE BOARD ARE FOUND)
int countx=0;// 擷取次數
while(successes < n_boards) {
//Skip every board_dt frames to allow user to move chessboard
if((frame++ % board_dt) == 0) {
//Find chessboard corners:
int found = cvFindChessboardCorners(
image, board_sz, corners, &corner_count,
CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FILTER_QUADS
);
cout<<"found: "<<found<<endl;
cout<<"corner_count: "<<corner_count<<endl;
//Get Subpixel accuracy on those corners
cvCvtColor(image, gray_image, CV_BGR2GRAY);
cvFindCornerSubPix(gray_image, corners, corner_count,
cvSize(10,10),cvSize(-1,-1), cvTermCriteria(
CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));
//Draw it
cvDrawChessboardCorners(image, board_sz, corners,
corner_count, found);
//cvShowImage( "Calibration", image );
// If we got a good board, add it to our data
if( corner_count == board_nx ) {
countx++;
cvShowImage( "Calibration", image ); //show in color if we did collect the image
step = successes*board_n;
for( int i=step, j=0; j<board_n; ++i,++j ) {
CV_MAT_ELEM(*image_points, float,i,0) = corners[j].x;
CV_MAT_ELEM(*image_points, float,i,1) = corners[j].y;
CV_MAT_ELEM(*object_points,float,i,0) = j/board_w;
CV_MAT_ELEM(*object_points,float,i,1) = j%board_w;
CV_MAT_ELEM(*object_points,float,i,2) = 0.0f;
}
CV_MAT_ELEM(*point_counts, int,successes,0) = board_n;
successes++;
printf("Collected our %d of %d needed chessboard images\n",successes,n_boards);
}
else{
countx++;
cvShowImage( "Calibration", gray_image ); //Show Gray if we didn't collect the image
cout<<countx<<endl;
}
} //end skip board_dt between chessboard capture
//Handle pause/unpause and ESC
int c = cvWaitKey(15);
if(c == 'p'){
c = 0;
while(c != 'p' && c != 27){
c = cvWaitKey(250);
}
}
if(c == 27)
return 0;
image = cvQueryFrame( capture ); //Get next image
cvShowImage("Raw Video", image);
} //END COLLECTION WHILE LOOP.
cvDestroyWindow("Calibration");
printf("\n\n*** CALLIBRATING THE CAMERA...");
//ALLOCATE MATRICES ACCORDING TO HOW MANY CHESSBOARDS FOUND
CvMat* object_points2 = cvCreateMat(successes*board_n,3,CV_32FC1);
CvMat* image_points2 = cvCreateMat(successes*board_n,2,CV_32FC1);
CvMat* point_counts2 = cvCreateMat(successes,1,CV_32SC1);
//TRANSFER THE POINTS INTO THE CORRECT SIZE MATRICES
for(int i = 0; i<successes*board_n; ++i){
CV_MAT_ELEM( *image_points2, float, i, 0) =
CV_MAT_ELEM( *image_points, float, i, 0);
CV_MAT_ELEM( *image_points2, float,i,1) =
CV_MAT_ELEM( *image_points, float, i, 1);
CV_MAT_ELEM(*object_points2, float, i, 0) =
CV_MAT_ELEM( *object_points, float, i, 0) ;
CV_MAT_ELEM( *object_points2, float, i, 1) =
CV_MAT_ELEM( *object_points, float, i, 1) ;
CV_MAT_ELEM( *object_points2, float, i, 2) =
CV_MAT_ELEM( *object_points, float, i, 2) ;
}
for(int i=0; i<successes; ++i){ //These are all the same number
CV_MAT_ELEM( *point_counts2, int, i, 0) =
CV_MAT_ELEM( *point_counts, int, i, 0);
}
cvReleaseMat(&object_points);
cvReleaseMat(&image_points);
cvReleaseMat(&point_counts);
// At this point we have all of the chessboard corners we need.
// Initialize the intrinsic matrix such that the two focal
// lengths have a ratio of 1.0
CV_MAT_ELEM( *intrinsic_matrix, float, 0, 0 ) = 1.0f;
CV_MAT_ELEM( *intrinsic_matrix, float, 1, 1 ) = 1.0f;
//CALIBRATE THE CAMERA!
cvCalibrateCamera2(
object_points2, image_points2,
point_counts2, cvGetSize( image ),
intrinsic_matrix, distortion_coeffs,
NULL, NULL,0 //CV_CALIB_FIX_ASPECT_RATIO
);
// SAVE THE INTRINSICS AND DISTORTIONS
printf(" *** DONE!\n\nStoring Intrinsics.xml and Distortions.xml files\n\n");
cvSave("Intrinsics.xml",intrinsic_matrix);
cvSave("Distortion.xml",distortion_coeffs);
// EXAMPLE OF LOADING THESE MATRICES BACK IN:
CvMat *intrinsic = (CvMat*)cvLoad("Intrinsics.xml");
CvMat *distortion = (CvMat*)cvLoad("Distortion.xml");
// Build the undistort map which we will use for all
// subsequent frames.
IplImage* mapx = cvCreateImage( cvGetSize(image), IPL_DEPTH_32F, 1 );
IplImage* mapy = cvCreateImage( cvGetSize(image), IPL_DEPTH_32F, 1 );
cvInitUndistortMap(
intrinsic,
distortion,
mapx,
mapy
);
// Just run the camera to the screen, now showing the raw and
// the undistorted image.
IplImage *r = cvCreateImage(cvGetSize(image),8,1);//subpixel
IplImage *g = cvCreateImage(cvGetSize(image),8,1);//subpixel
IplImage *b = cvCreateImage(cvGetSize(image),8,1);//subpixel
cvNamedWindow( "Undistort" );
while(image) {
cvShowImage( "Raw Video", image ); // Show raw image
//system("pause");
cvSplit(image, r,g,b, NULL);
cvRemap( r, r, mapx, mapy ); // Undistort image
cvRemap( g, g, mapx, mapy ); // Undistort image
cvRemap( b, b, mapx, mapy ); // Undistort image
cvMerge(r,g,b, NULL, image);
//cvRemap( image, t, mapx, mapy ); // Undistort image
cvShowImage("Undistort", image); // Show corrected image
//Handle pause/unpause and ESC
int c = cvWaitKey(15);
if(c == 'p'){
c = 0;
while(c != 'p' && c != 27){
c = cvWaitKey(250);
}
}
if(c == 27)
break;
image = cvQueryFrame( capture );
}
return 0;
}
在此只需要控制四個參數,即可達到我們的需求:
board_w = 6;棋盤的寬
board_h = 9;棋盤的高
n_boards = 5;擷取棋盤個數
board_dt = 20;每frame擷取影像

若影像中棋盤不符合所要求,將不會有任何結果。
若有找到,則會標記每個角,顏色順序為紅、橙、黃、綠、藍、靛、紫。








若沒有完全找到符合的角個數,則會在找到的角標記紅色,可看到影像中棋盤遠近、搖晃、角度等狀況下的找尋狀況。
接下來使用已經尋得的參數來矯正webcam影像。


以上為失敗的矯正結果,可以看到下方影像mapping完全錯誤! 接著使用他人尋得的參數。


看樣子是類似魚眼鏡頭的參數,其所儲存的.xml擋資料如下:
Intrinsics.xml中內容為:
<?xml version="1.0"?>
<opencv_storage>
<Intrinsics type_id="opencv-matrix">
<rows>3</rows>
<cols>3</cols>
<dt>f</dt>
<data>
649.64843750 0. 288.47882080 0. 647.89129639 271.92953491 0. 0. 1.</data></Intrinsics>
</opencv_storage>
Distortion.xml中內容為:
<?xml version="1.0"?>
<opencv_storage>
<Distortion type_id="opencv-matrix">
<rows>5</rows>
<cols>1</cols>
<dt>f</dt>
<data>
-0.37764871 22.05950546 0.06449836 -0.03288389 -209.10910034</data></Distortion>
</opencv_storage>
參考:Fisheye to Rectilinear Conversion、Calibrating a camera: Theory、Calibrating & Undistorting with OpenCV in C++ (Oh yeah)、cvRemap () crash – Stack Overflow。
Comments on: "[OpenCV] 相機矯正 (Camera Calibration)" (4)
您好!您對於相機校正的說明非常棒!
在執行您提供的範例中,我在經過校正程序後有出現 Undistort 視窗,但發現還是與
Row Video 視窗影像中一樣。請問這是要修改哪裡呢?
讚Liked by 1 person
謝謝你的青睞呀!
真不好意思,五年前的研究我忘了差不多了XD~
就請你自行研究囉! 😉
讚讚
謝謝您的回覆!
讚Liked by 1 person
老實說,用一顆鏡頭來矯正的效果不好,所以我採用兩顆鏡頭來實做。
讚讚