#include <cv.h>
#include <highgui.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h> using namespace std; int n_boards=;//影象數目
const int board_dt=;//等20幀每棋盤檢視 //int sn_board=0;//成功找到角點的影象數目
int board_w;//影象的角點行列數 canshu
int board_h; //canshu
int main(int argc ,char * argv[])
{
/*讀入影象*/
board_w=; //canshu
board_h=; //canshu
n_boards=; //canshu int board_n=board_h*board_w;//每幅影象的角點數
CvSize board_size=cvSize(board_w,board_h);//每幅影象的角點數
CvCapture*capture;
capture=cvCreateCameraCapture();
//assert(capture); if(!capture) { printf("\nCouldn't open the camera\n"); return -;}
cvNamedWindow("MyCalib");
cvNamedWindow("Raw Video"); //allocate storage
CvMat*object_points = cvCreateMat(board_n*n_boards,,CV_32FC1);
CvMat*image_points =cvCreateMat(board_n*n_boards,,CV_32FC1);
CvMat*point_counts =cvCreateMat(n_boards,,CV_32SC1);
CvMat * camera_matrix =cvCreateMat(,,CV_32FC1);//攝像機矩陣(內參數矩陣)
CvMat * distortion_coeffs=cvCreateMat(,,CV_32FC1);//畸變係數
CvPoint2D32f*corners=new CvPoint2D32f[board_n];//一幅影象的角點陣列
int corner_count;
int successes=; // 本文 successes就是 sn_board
int step,frame=; IplImage*image=cvQueryFrame(capture);
IplImage *gray_image=cvCreateImage(cvGetSize(image),,);//建立頭並分配資料 //亞畫素subpixel
//capture corner views loop until we've got n_boards
//成功捕獲(找到所有角點on the board) while (successes<n_boards)
{ //skip every board_dt frames to allow user to move chessboard
if(frame++%board_dt==)
{
int found=cvFindChessboardCorners(image,board_size,corners,&corner_count,CV_CALIB_CB_ADAPTIVE_THRESH|CV_CALIB_CB_FILTER_QUADS); //精確到亞畫素,繪製棋盤角點
cvCvtColor(image,gray_image,CV_BGR2GRAY);
cvFindCornerSubPix(gray_image,corners,corner_count,cvSize(,),cvSize(-,-),cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,,0.1));
cvDrawChessboardCorners(image,board_size,corners,corner_count,found);
//cvShowImage("MyCalib",image); //如果得到一個好板,則把把它加入到我們的資料中
if(corner_count==board_n){ //xin
cvShowImage( "Calibration", image ); //show in color if we did collect the image
step=successes*board_n; //xin
for(int i=step,j=;j<board_n;++i,++j){
CV_MAT_ELEM(*image_points,float,i,) =corners[j].x;//CV_MAT_ELEM用來訪問矩陣每個元素的巨集,這個巨集只對單通道矩陣有效,多通道會報錯
CV_MAT_ELEM(*image_points,float,i,) =corners[j].y;//(要訪問的矩陣,矩陣元素的型別,所要訪問元素的行數,列數)
//上兩句是將檢測到的點以座標形式儲存
CV_MAT_ELEM(*object_points,float,i,) =float(j/board_w);
CV_MAT_ELEM(*object_points,float,i,) =float(j%board_w);
//上兩句是將檢測到的點的數目,以行列形式儲存
CV_MAT_ELEM(*object_points,float,i,) =0.0f;
}
CV_MAT_ELEM(*point_counts,int,successes,)=board_n;
successes++;
printf("Collected our %d of %d needed chessboard images\n",successes,n_boards);
}
else
cvShowImage( "Calibration", gray_image );//Show Gray if we didn't collect the image
}//end skip board_dt between chessboard capture //暫停模組
int c = cvWaitKey();
if(c == 'p'){
c = ;
while(c != 'p' && c != ){
c = cvWaitKey();
}
}
if(c == )
return ;
image = cvQueryFrame( capture ); //Get next image
cvShowImage("Raw Video", image);
} //END COLLECTION WHILE LOOP.
cvDestroyWindow("Calibration");
printf("\n\n*** CALLIBRATING THE CAMERA..."); //重新賦值,又重新分配了矩陣空間
CvMat * object_points0= cvCreateMat(board_n*successes,,CV_32FC1);
CvMat * image_points0=cvCreateMat(board_n*successes,,CV_32FC1);
CvMat * point_counts0=cvCreateMat(successes,,CV_32SC1);
//transfer THE points INTO THE correct size matrices
for (int i=;i<successes*board_n;i++)//board_n為每幅影象的角點數*sn_board為成功找到角點的影象數目
{
CV_MAT_ELEM(*image_points0,float,i,)=CV_MAT_ELEM(*image_points,float,i,);
CV_MAT_ELEM(*image_points0,float,i,)=CV_MAT_ELEM(*image_points,float,i,);
CV_MAT_ELEM(*object_points0,float,i,)=CV_MAT_ELEM(*object_points,float,i,);
CV_MAT_ELEM(*object_points0,float,i,)=CV_MAT_ELEM(*object_points,float,i,);
CV_MAT_ELEM(*object_points0,float,i,)=0.0f;
}
for (int i=;i<successes;i++)
{
CV_MAT_ELEM(*point_counts0,int,i,)=CV_MAT_ELEM(*point_counts,int,i,);
}
cvReleaseMat(&object_points);//世界座標點
cvReleaseMat(&point_counts); //每張影象中的角點數
cvReleaseMat(&image_points); //影象座標點
//使用cvReleaseMat函式之後,系統將釋放剛才載入矩陣的記憶體空間(也即這個矩陣已經不在記憶體中了) // 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( *camera_matrix, float, , ) = 1.0f;
CV_MAT_ELEM( *camera_matrix, float, , ) = 1.0f;
//CALIBRATE THE CAMERA!
cvCalibrateCamera2(
object_points0, image_points0,
point_counts0, cvGetSize( image ),
camera_matrix, distortion_coeffs,
NULL, NULL, //CV_CALIB_FIX_ASPECT_RATIO
); // SAVE THE INTRINSICS AND DISTORTIONS xin
printf("......儲存內部引數與畸變引數\n");
cvSave("camera_matrix1111.xml",camera_matrix);
cvSave("distortion_coeffs.xml",distortion_coeffs); // EXAMPLE OF LOADING THESE MATRICES BACK IN: xin
CvMat *intrinsic = (CvMat*)cvLoad("Intrinsics.xml");
CvMat *distortion = (CvMat*)cvLoad("Distortion.xml"); // Build the undistort map which we will use for all
// subsequent frames.建立不失真的圖map
//
IplImage* mapx = cvCreateImage( cvGetSize(image), IPL_DEPTH_32F, );
IplImage* mapy = cvCreateImage( cvGetSize(image), IPL_DEPTH_32F, );
cvInitUndistortMap(intrinsic,distortion,mapx, mapy); // Just run the camera to the screen, now showing the raw and
// the undistorted image.
//
cvNamedWindow( "Undistort" );
while(image) {
IplImage *t = cvCloneImage(image);
cvShowImage( "Raw Video", image ); // Show raw image
cvRemap( t, image, mapx, mapy ); // Undistort image
cvReleaseImage(&t);
cvShowImage("Undistort", image); // Show corrected image //Handle pause/unpause and ESC
int c = cvWaitKey();
if(c == 'p'){
c = ;
while(c != 'p' && c != ){
c = cvWaitKey();
}
}
if(c == )
break;
image = cvQueryFrame( capture );
} return ;
}