相机标定Opencv实现以及标定流程(四)_人生几何_新浪博客

完整的工程代码:

 

[cpp]  view plain  copy  print ?
  1. #include "opencv2/core/core.hpp"  
  2. #include "opencv2/imgproc/imgproc.hpp"  
  3. #include "opencv2/calib3d/calib3d.hpp"  
  4. #include "opencv2/highgui/highgui.hpp"  
  5. #include   
  6. #include   
  7.   
  8. using namespace cv;  
  9. using namespace std;  
  10.   
  11. void main()   
  12. {  
  13.     ifstream fin("calibdata.txt");   
  14.     ofstream fout("caliberation_result.txt");      
  15.     //读取每一幅图像,从中提取出角点,然后对角点进行亚像素精确化   
  16.     cout<<"开始提取角点………………";  
  17.     int image_count=0;    
  18.     Size image_size;    
  19.     Size board_size = Size(4,6);      
  20.     vector image_points_buf;    
  21.     vector> image_points_seq;   
  22.     string filename;  
  23.     int count= -1 ;//用于存储角点个数。  
  24.     while (getline(fin,filename))  
  25.     {  
  26.         image_count++;        
  27.         // 用于观察检验输出  
  28.         cout<<"image_count = "<<image_count<<endl;          
  29.           
  30.         cout<<"-->count = "<<count;        
  31.         Mat imageInput=imread(filename);  
  32.         if (image_count == 1)  //读入第一张图片时获取图像宽高信息  
  33.         {  
  34.             image_size.width = imageInput.cols;  
  35.             image_size.height =imageInput.rows;           
  36.             cout<<"image_size.width = "<<image_size.width<<endl;  
  37.             cout<<"image_size.height = "<<image_size.height<<endl;  
  38.         }  
  39.   
  40.           
  41.         if (0 == findChessboardCorners(imageInput,board_size,image_points_buf))  
  42.         {             
  43.             cout<<"can not find chessboard corners!\n"//找不到角点  
  44.             exit(1);  
  45.         }   
  46.         else   
  47.         {  
  48.             Mat view_gray;  
  49.             cvtColor(imageInput,view_gray,CV_RGB2GRAY);  
  50.               
  51.             find4QuadCornerSubpix(view_gray,image_points_buf,Size(11,11)); //对粗提取的角点进行精确化  
  52.             image_points_seq.push_back(image_points_buf);  //保存亚像素角点  
  53.               
  54.             drawChessboardCorners(view_gray,board_size,image_points_buf,true); //用于在图片中标记角点  
  55.             imshow("Camera Calibration",view_gray);//显示图片  
  56.             waitKey(500);//暂停0.5S         
  57.         }  
  58.     }  
  59.     int total = image_points_seq.size();  
  60.     cout<<"total = "<<total<<endl;  
  61.     int CornerNum=board_size.width*board_size.height;  //每张图片上总的角点数  
  62.     for (int ii=0 ; ii
  63.     {  
  64.         if (0 == ii%CornerNum)// 24 是每幅图片的角点个数。此判断语句是为了输出 图片号,便于控制台观看   
  65.         {     
  66.             int i = -1;  
  67.             i = ii/CornerNum;  
  68.             int j=i+1;  
  69.             cout<<"--> 第 "<<j <<"图片的数据 --> : "<<endl;  
  70.         }  
  71.         if (0 == ii%3)  // 此判断语句,格式化输出,便于控制台查看  
  72.         {  
  73.             cout<<endl;  
  74.         }  
  75.         else  
  76.         {  
  77.             cout.width(10);  
  78.         }  
  79.         //输出所有的角点  
  80.         cout<<" -->"<<image_points_seq[ii][0].x;  
  81.         cout<<" -->"<<image_points_seq[ii][0].y;  
  82.     }     
  83.     cout<<"角点提取完成!\n";  
  84.   
  85.     //以下是摄像机标定  
  86.     cout<<"开始标定………………";  
  87.       
  88.     Size square_size = Size(10,10);    
  89.     vector> object_points;   
  90.       
  91.     Mat cameraMatrix=Mat(3,3,CV_32FC1,Scalar::all(0));   
  92.     vector<<span class="datatypes" style="margin: 0px; padding: 0px; border: none; color: rgb(46, 139, 87); font-weight: bold; background-color: inherit;">int> point_counts;  // 每幅图像中角点的数量  
  93.     Mat distCoeffs=Mat(1,5,CV_32FC1,Scalar::all(0));   
  94.     vector tvecsMat;    
  95.     vector rvecsMat;   
  96.       
  97.     int i,j,t;  
  98.     for (t=0;t
  99.     {  
  100.         vector tempPointSet;  
  101.         for (i=0;i
  102.         {  
  103.             for (j=0;j
  104.             {  
  105.                 Point3f realPoint;  
  106.                   
  107.                 realPoint.x = i*square_size.width;  
  108.                 realPoint.y = j*square_size.height;  
  109.                 realPoint.z = 0;  
  110.                 tempPointSet.push_back(realPoint);  
  111.             }  
  112.         }  
  113.         object_points.push_back(tempPointSet);  
  114.     }  
  115.       
  116.     for (i=0;i
  117.     {  
  118.         point_counts.push_back(board_size.width*board_size.height);  
  119.     }     
  120.       
  121.     calibrateCamera(object_points,image_points_seq,image_size,cameraMatrix,distCoeffs,rvecsMat,tvecsMat,0);  
  122.     cout<<"标定完成!\n";  
  123.     //对标定结果进行评价  
  124.     cout<<"开始评价标定结果………………\n";  
  125.     double total_err = 0.0;   
  126.     double err = 0.0;   
  127.     vector image_points2;   
  128.     cout<<"\t每幅图像的标定误差:\n";  
  129.     fout<<"每幅图像的标定误差:\n";  
  130.     for (i=0;i
  131.     {  
  132.         vector tempPointSet=object_points[i];  
  133.           
  134.         projectPoints(tempPointSet,rvecsMat[i],tvecsMat[i],cameraMatrix,distCoeffs,image_points2);  
  135.           
  136.         vector tempImagePoint = image_points_seq[i];  
  137.         Mat tempImagePointMat = Mat(1,tempImagePoint.size(),CV_32FC2);  
  138.         Mat image_points2Mat = Mat(1,image_points2.size(), CV_32FC2);  
  139.         for (int j = 0 ; j < tempImagePoint.size(); j++)  
  140.         {  
  141.             image_points2Mat.at(0,j) = Vec2f(image_points2[j].x, image_points2[j].y);  
  142.             tempImagePointMat.at(0,j) = Vec2f(tempImagePoint[j].x, tempImagePoint[j].y);  
  143.         }  
  144.         err = norm(image_points2Mat, tempImagePointMat, NORM_L2);  
  145.         total_err += err/=  point_counts[i];     
  146.         std::cout<<"第"<<i+1<<"幅图像的平均误差:"<<err<<"像素"<<endl;     
  147.         fout<<"第"<<i+1<<"幅图像的平均误差:"<<err<<"像素"<<endl;     
  148.     }     
  149.     std::cout<<"总体平均误差:"<<total_err/image_count<<"像素"<<endl;     
  150.     fout<<"总体平均误差:"<<total_err/image_count<<"像素"<<endl<<endl;     
  151.     std::cout<<"评价完成!"<<endl;    
  152.     //保存定标结果      
  153.     std::cout<<"开始保存定标结果………………"<<endl;         
  154.     Mat rotation_matrix = Mat(3,3,CV_32FC1, Scalar::all(0));   
  155.     fout<<"相机内参数矩阵:"<<endl;     
  156.     fout<<cameraMatrix<<endl<<endl;     
  157.     fout<<"畸变系数:\n";     
  158.     fout<<distCoeffs<<endl<<endl<<endl;     
  159.     for (int i=0; i
  160.     {   
  161.         fout<<"第"<<i+1<<"幅图像的旋转向量:"<<endl;     
  162.         fout<<tvecsMat[i]<<endl;     
  163.              
  164.         Rodrigues(tvecsMat[i],rotation_matrix);     
  165.         fout<<"第"<<i+1<<"幅图像的旋转矩阵:"<<endl;     
  166.         fout<<rotation_matrix<<endl;     
  167.         fout<<"第"<<i+1<<"幅图像的平移向量:"<<endl;     
  168.         fout<<rvecsMat[i]<<endl<<endl;     
  169.     }     
  170.     std::cout<<"完成保存"<<endl;   
  171.     fout<<endl;  
  172.     system("pause");      
  173.     return ;  
  174. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值