相机标定获得内参矩阵及其他参数

csdn博客推荐:http://blog.csdn.net/chenyusiyuan/article/details/5963256


理论方面和单目标定opencv代码网址:

http://pan.baidu.com/s/1cFRWg2



下面为转载的一部分

我自己写了一个摄像机标定程序,核心算法参照learning opencv,但是那个程序要从命令行预先输入参数,且标定图片要预先准备好,我觉得不太好,我就自己写了一个,跟大家分享下。
若有纰漏,希望大家指正!

[cpp]  view plain  copy
  1. #include   
  2. #include   
  3. #include   
  4. #include   
  5.   
  6. #pragma comment(lib, "ml.lib")  
  7. #pragma comment(lib, "cv.lib")  
  8. #pragma comment(lib, "cvaux.lib")  
  9. #pragma comment(lib, "cvcam.lib")  
  10. #pragma comment(lib, "cxcore.lib")  
  11. #pragma comment(lib, "cxts.lib")  
  12. #pragma comment(lib, "highgui.lib")  
  13. #pragma comment(lib, "cvhaartraining.lib")  
  14.   
  15. using namespace std;  
  16.   
  17. int main()  
  18. {  
  19.     int cube_length=7;  
  20.   
  21.     CvCapture* capture;  
  22.   
  23.     capture=cvCreateCameraCapture(0);  
  24.   
  25.     if(capture==0)  
  26.     {  
  27.         printf("无法捕获摄像头设备!\n\n");  
  28.         return 0;  
  29.     }  
  30.     else  
  31.     {  
  32.         printf("捕获摄像头设备成功!!\n\n");  
  33.     }  
  34.   
  35.     IplImage* frame = NULL;  
  36.   
  37.     cvNamedWindow("摄像机帧截取窗口",1);  
  38.   
  39.     printf("按“C”键截取当前帧并保存为标定图片...\n按“Q”键退出截取帧过程...\n\n");  
  40.   
  41.     int number_image=1;  
  42.     char *str1;  
  43.     str1=".jpg";  
  44.     char filename[20]="";  
  45.   
  46.     while(true)  
  47.     {  
  48.         frame=cvQueryFrame(capture);  
  49.         if(!frame)  
  50.             break;  
  51.         cvShowImage("摄像机帧截取窗口",frame);  
  52.   
  53.         if(cvWaitKey(10)=='c')  
  54.         {  
  55.             sprintf_s (filename,"%d.jpg",number_image);  
  56.             cvSaveImage(filename,frame);  
  57.             cout<<"成功获取当前帧,并以文件名"<"保存...\n\n";  
  58.             printf("按“C”键截取当前帧并保存为标定图片...\n按“Q”键退出截取帧过程...\n\n");  
  59.             number_image++;  
  60.         }  
  61.         else if(cvWaitKey(10)=='q')  
  62.         {  
  63.             printf("截取图像帧过程完成...\n\n");  
  64.             cout<<"共成功截取"<<--number_image<<"帧图像!!\n\n";  
  65.             break;  
  66.         }  
  67.     }  
  68.   
  69.     cvReleaseImage(&frame);  
  70.     cvDestroyWindow("摄像机帧截取窗口");  
  71.   
  72.     IplImage * show;  
  73.     cvNamedWindow("RePlay",1);  
  74.   
  75.     int a=1;  
  76.     int number_image_copy = number_image;  
  77.   
  78.     CvSize board_size=cvSize(7,7);  
  79.     int board_width=board_size.width;  
  80.     int board_height=board_size.height;  
  81.     int total_per_image=board_width*board_height;  
  82.     CvPoint2D32f * image_points_buf = new CvPoint2D32f[total_per_image];  
  83.     CvMat * image_points=cvCreateMat(number_image*total_per_image,2,CV_32FC1);  
  84.     CvMat * object_points=cvCreateMat(number_image*total_per_image,3,CV_32FC1);  
  85.     CvMat * point_counts=cvCreateMat(number_image,1,CV_32SC1);  
  86.     CvMat * intrinsic_matrix=cvCreateMat(3,3,CV_32FC1);  
  87.     CvMat * distortion_coeffs=cvCreateMat(5,1,CV_32FC1);  
  88.   
  89.     int count;  
  90.     int found;  
  91.     int step;  
  92.     int successes=0;  
  93.   
  94.     while(a<=number_image_copy)  
  95.     {  
  96.         sprintf_s (filename,"%d.jpg",a);  
  97.         show=cvLoadImage(filename,-1);  
  98.   
  99.         found=cvFindChessboardCorners(show,board_size,image_points_buf,&count,  
  100.             CV_CALIB_CB_ADAPTIVE_THRESH|CV_CALIB_CB_FILTER_QUADS);  
  101.         if(found==0)  
  102.         {         
  103.             cout<<"第"<"帧图片无法找到棋盘格所有角点!\n\n";  
  104.             cvNamedWindow("RePlay",1);  
  105.             cvShowImage("RePlay",show);  
  106.             cvWaitKey(0);  
  107.   
  108.         }  
  109.         else  
  110.         {  
  111.             cout<<"第"<"帧图像成功获得"<"个角点...\n";  
  112.   
  113.             cvNamedWindow("RePlay",1);  
  114.   
  115.             IplImage * gray_image= cvCreateImage(cvGetSize(show),8,1);  
  116.             cvCvtColor(show,gray_image,CV_BGR2GRAY);  
  117.             cout<<"获取源图像灰度图过程完成...\n";  
  118.             cvFindCornerSubPix(gray_image,image_points_buf,count,cvSize(11,11),cvSize(-1,-1),  
  119.                 cvTermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,0.1));  
  120.             cout<<"灰度图亚像素化过程完成...\n";  
  121.             cvDrawChessboardCorners(show,board_size,image_points_buf,count,found);  
  122.             cout<<"在源图像上绘制角点过程完成...\n\n";  
  123.             cvShowImage("RePlay",show);  
  124.   
  125.             cvWaitKey(0);  
  126.         }  
  127.   
  128.         if(total_per_image==count)  
  129.         {  
  130.             step=successes*total_per_image;  
  131.             for(int i=step,j=0;j
  132.             {  
  133.                 CV_MAT_ELEM(*image_points,float,i,0)=image_points_buf[j].x;  
  134.                 CV_MAT_ELEM(*image_points,float,i,1)=image_points_buf[j].y;  
  135.                 CV_MAT_ELEM(*object_points,float,i,0)=(float)(j/cube_length);  
  136.                 CV_MAT_ELEM(*object_points,float,i,1)=(float)(j%cube_length);  
  137.                 CV_MAT_ELEM(*object_points,float,i,2)=0.0f;  
  138.             }  
  139.             CV_MAT_ELEM(*point_counts,int,successes,0)=total_per_image;  
  140.             successes++;  
  141.         }  
  142.         a++;  
  143.     }  
  144.   
  145.     cvReleaseImage(&show);  
  146.     cvDestroyWindow("RePlay");  
  147.   
  148.   
  149.     cout<<"*********************************************\n";  
  150.     cout<"帧图片中,标定成功的图片为"<"帧...\n";  
  151.     cout<"帧图片中,标定失败的图片为"<"帧...\n\n";  
  152.     cout<<"*********************************************\n\n";  
  153.   
  154.     cout<<"按任意键开始计算摄像机内参数...\n\n";  
  155.   
  156.     CvCapture* capture1;  
  157.     capture1=cvCreateCameraCapture(0);  
  158.     IplImage * show_colie;  
  159.     show_colie=cvQueryFrame(capture1);  
  160.   
  161.     CvMat * object_points2=cvCreateMat(successes*total_per_image,3,CV_32FC1);  
  162.     CvMat * image_points2=cvCreateMat(successes*total_per_image,2,CV_32FC1);  
  163.     CvMat * point_counts2=cvCreateMat(successes,1,CV_32SC1);  
  164.   
  165.     for(int i=0;i
  166.     {  
  167.         CV_MAT_ELEM(*image_points2,float,i,0)=CV_MAT_ELEM(*image_points,float,i,0);  
  168.         CV_MAT_ELEM(*image_points2,float,i,1)=CV_MAT_ELEM(*image_points,float,i,1);  
  169.         CV_MAT_ELEM(*object_points2,float,i,0)=CV_MAT_ELEM(*object_points,float,i,0);  
  170.         CV_MAT_ELEM(*object_points2,float,i,1)=CV_MAT_ELEM(*object_points,float,i,1);  
  171.         CV_MAT_ELEM(*object_points2,float,i,2)=CV_MAT_ELEM(*object_points,float,i,2);  
  172.     }  
  173.   
  174.     for(int i=0;i
  175.     {  
  176.         CV_MAT_ELEM(*point_counts2,int,i,0)=CV_MAT_ELEM(*point_counts,int,i,0);  
  177.     }  
  178.   
  179.     cvReleaseMat(&object_points);  
  180.     cvReleaseMat(&image_points);  
  181.     cvReleaseMat(&point_counts);  
  182.   
  183.     CV_MAT_ELEM(*intrinsic_matrix,float,0,0)=1.0f;  
  184.     CV_MAT_ELEM(*intrinsic_matrix,float,1,1)=1.0f;  
  185.   
  186.     cvCalibrateCamera2(object_points2,image_points2,point_counts2,cvGetSize(show_colie),  
  187.         intrinsic_matrix,distortion_coeffs,NULL,NULL,0);  
  188.   
  189.     cout<<"摄像机内参数矩阵为:\n";  
  190.     cout<float,0,0)<<"    "<float,0,1)  
  191.         <<"    "<float,0,2)  
  192.         <<"\n\n";  
  193.     cout<float,1,0)<<"    "<float,1,1)  
  194.         <<"    "<float,1,2)  
  195.         <<"\n\n";  
  196.     cout<float,2,0)<<"    "<float,2,1)  
  197.         <<"          "<float,2,2)  
  198.         <<"\n\n";  
  199.   
  200.     cout<<"畸变系数矩阵为:\n";  
  201.     cout<float,0,0)<<"    "<float,1,0)  
  202.         <<"    "<float,2,0)  
  203.         <<"    "<float,3,0)  
  204.         <<"    "<float,4,0)  
  205.         <<"\n\n";  
  206.   
  207.     cvSave("Intrinsics.xml",intrinsic_matrix);  
  208.     cvSave("Distortion.xml",distortion_coeffs);  
  209.   
  210.     cout<<"摄像机矩阵、畸变系数向量已经分别存储在名为Intrinsics.xml、Distortion.xml文档中\n\n";  
  211.   
  212.     CvMat * intrinsic=(CvMat *)cvLoad("Intrinsics.xml");  
  213.     CvMat * distortion=(CvMat *)cvLoad("Distortion.xml");  
  214.   
  215.     IplImage * mapx=cvCreateImage(cvGetSize(show_colie),IPL_DEPTH_32F,1);  
  216.     IplImage * mapy=cvCreateImage(cvGetSize(show_colie),IPL_DEPTH_32F,1);  
  217.   
  218.     cvInitUndistortMap(intrinsic,distortion,mapx,mapy);  
  219.   
  220.     cvNamedWindow("原始图像",1);  
  221.     cvNamedWindow("非畸变图像",1);  
  222.   
  223.     cout<<"按‘E’键退出显示...\n\n";  
  224.   
  225.     while(show_colie)  
  226.     {  
  227.         IplImage * clone=cvCloneImage(show_colie);  
  228.         cvShowImage("原始图像",show_colie);  
  229.         cvRemap(clone,show_colie,mapx,mapy);  
  230.         cvReleaseImage(&clone);  
  231.         cvShowImage("非畸变图像",show_colie);  
  232.   
  233.         if(cvWaitKey(10)=='e')  
  234.         {  
  235.             break;  
  236.         }  
  237.   
  238.         show_colie=cvQueryFrame(capture1);  
  239.     }  
  240.   
  241.     return 0;  
  242. }  

你可能感兴趣的:(opencv)