標(biāo)題: 張正友相機(jī)標(biāo)定源碼 利用opencv實(shí)現(xiàn) [打印本頁(yè)]

作者: mvp科比    時(shí)間: 2018-8-6 14:32
標(biāo)題: 張正友相機(jī)標(biāo)定源碼 利用opencv實(shí)現(xiàn)
張正友相機(jī)標(biāo)定。利用opencv實(shí)現(xiàn)


單片機(jī)源程序如下:
  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 <iostream>
  6. #include <fstream>
  7. #include <iomanip>
  8. using namespace cv;
  9. using namespace std;

  10. void main()
  11. {
  12.         //ifstream fin("calibdata.txt"); /* 標(biāo)定所用圖像文件的路徑 */
  13.         //ofstream fout("caliberation_result.txt");  /* 保存標(biāo)定結(jié)果的文件 */
  14.         //                                                                                   //讀取每一幅圖像,從中提取出角點(diǎn),然后對(duì)角點(diǎn)進(jìn)行亞像素精確化       
  15.         //cout << "開(kāi)始提取角點(diǎn)………………";
  16.         //int image_count = 0;  /* 圖像數(shù)量 */
  17.         //Size image_size;  /* 圖像的尺寸 */
  18.         //Size board_size = Size(4, 6);    /* 標(biāo)定板上每行、列的角點(diǎn)數(shù) */
  19.         //vector<Point2f> image_points_buf;  /* 緩存每幅圖像上檢測(cè)到的角點(diǎn) */
  20.         //vector<vector<Point2f>> image_points_seq; /* 保存檢測(cè)到的所有角點(diǎn) */
  21.         //string filename;
  22.         //int count = -1;//用于存儲(chǔ)角點(diǎn)個(gè)數(shù)。
  23.         //while (getline(fin, filename))
  24.         //{
  25.         //        image_count++;
  26.         //        // 用于觀察檢驗(yàn)輸出
  27.         //        cout << "image_count = " << image_count << endl;
  28.         //        /* 輸出檢驗(yàn)*/
  29.         //        cout << "-->count = " << count;
  30.         //        Mat imageInput = imread(filename);

  31.         ofstream fout("caliberation_result.txt");  /* 保存標(biāo)定結(jié)果的文件 */
  32.         cv::namedWindow("Image");
  33.         cv::Mat imageInput;
  34.         std::vector<std::string> filelist;//存放標(biāo)定圖片路徑
  35.         int image_count = 0;  /* 圖像數(shù)量 */
  36.         Size image_size;  /* 圖像的尺寸 */
  37.         Size board_size = Size(4, 6);    /* 標(biāo)定板上每行、列的角點(diǎn)數(shù) */
  38.         vector<Point2f> image_points_buf;  /* 緩存每幅圖像上檢測(cè)到的角點(diǎn) */
  39.         vector<vector<Point2f>> image_points_seq; /* 保存檢測(cè)到的所有角點(diǎn) */
  40.         int count = -1;//用于存儲(chǔ)角點(diǎn)個(gè)數(shù)。
  41.                                                                           //生成路徑,此處表示圖片放在工程根目錄下的chessboards文件夾
  42.         for (int i = 1; i <= 21; i++)
  43.         {
  44.                 std::stringstream str;//setw(int n)用來(lái)控制輸出間隔
  45.                 str << "chessboards/chessboard" << std::setw(2) << std::setfill('0') << i << ".jpg";//圖片的相對(duì)路徑
  46.                 std::cout << str.str() << std::endl;

  47.                 filelist.push_back(str.str());
  48.                 imageInput = cv::imread(str.str());
  49.                 cv::imshow("Image", imageInput);


  50.                 //if (image_count == 1)  //讀入第一張圖片時(shí)獲取圖像寬高信息
  51.                 //{
  52.                 //        image_size.width = imageInput.cols;
  53.                 //        image_size.height = imageInput.rows;
  54.                 //        cout << "image_size.width = " << image_size.width << endl;
  55.                 //        cout << "image_size.height = " << image_size.height << endl;
  56.                 //}

  57.                 /* 提取角點(diǎn) */
  58.                 //if (0 == findChessboardCorners(imageInput, board_size, image_points_buf))
  59.                 //{
  60.                 //        cout << "can not find chessboard corners!\n"; //找不到角點(diǎn)
  61.                 //        exit(1);
  62.                 //}
  63.                 //else
  64.                 //{
  65.                     findChessboardCorners(imageInput, board_size, image_points_buf);
  66.                         Mat view_gray;
  67.                         cvtColor(imageInput, view_gray, CV_RGB2GRAY);
  68.                         findChessboardCorners(imageInput, board_size, image_points_buf);
  69.                         /* 亞像素精確化 */
  70.                         find4QuadCornerSubpix(view_gray, image_points_buf, Size(5, 5)); //對(duì)粗提取的角點(diǎn)進(jìn)行精確化
  71.                                                                                                                                                         //cornerSubPix(view_gray,image_points_buf,Size(5,5),Size(-1,-1),TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER,30,0.1));
  72.                         image_points_seq.push_back(image_points_buf);  //保存亞像素角點(diǎn)
  73.                                                                                                                    /* 在圖像上顯示角點(diǎn)位置 */
  74.                         drawChessboardCorners(view_gray, board_size, image_points_buf, false); //用于在圖片中標(biāo)記角點(diǎn)
  75.                         imshow("Camera Calibration", view_gray);//顯示圖片
  76.                         waitKey(200);//暫停0.5S               
  77.                


  78.         }
  79.        
  80.         int total = image_points_seq.size();
  81.         cout << "total = " << total << endl;
  82.         int CornerNum = board_size.width*board_size.height;  //每張圖片上總的角點(diǎn)數(shù)
  83.         for (int ii = 0; ii<total; ii++)
  84.         {
  85.                 if (0 == ii%CornerNum)// 24 是每幅圖片的角點(diǎn)個(gè)數(shù)。此判斷語(yǔ)句是為了輸出 圖片號(hào),便于控制臺(tái)觀看
  86.                 {
  87.                         int i = -1;
  88.                         i = ii / CornerNum;
  89.                         int j = i + 1;
  90.                         cout << "--> 第 " << j << "圖片的數(shù)據(jù) --> : " << endl;
  91.                 }
  92.                 if (0 == ii % 3)        // 此判斷語(yǔ)句,格式化輸出,便于控制臺(tái)查看
  93.                 {
  94.                         cout << endl;
  95.                 }
  96.                 else
  97.                 {
  98.                         cout.width(10);
  99.                 }
  100.                 //輸出所有的角點(diǎn)
  101.                 cout << " -->" << image_points_seq[ii][0].x;
  102.                 cout << " -->" << image_points_seq[ii][0].y;
  103.         }
  104.         cout << "角點(diǎn)提取完成!\n";

  105.         //以下是攝像機(jī)標(biāo)定
  106.         cout << "開(kāi)始標(biāo)定………………";
  107.         /*棋盤(pán)三維信息*/
  108.         Size square_size = Size(10, 10);  /* 實(shí)際測(cè)量得到的標(biāo)定板上每個(gè)棋盤(pán)格的大小 */
  109.         vector<vector<Point3f>> object_points; /* 保存標(biāo)定板上角點(diǎn)的三維坐標(biāo) */
  110.                                                                                    /*內(nèi)外參數(shù)*/
  111.         Mat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); /* 攝像機(jī)內(nèi)參數(shù)矩陣 */
  112.         vector<int> point_counts;  // 每幅圖像中角點(diǎn)的數(shù)量
  113.         Mat distCoeffs = Mat(1, 5, CV_32FC1, Scalar::all(0)); /* 攝像機(jī)的5個(gè)畸變系數(shù):k1,k2,p1,p2,k3 */
  114.         vector<Mat> tvecsMat;  /* 每幅圖像的旋轉(zhuǎn)向量 */
  115.         vector<Mat> rvecsMat; /* 每幅圖像的平移向量 */
  116.                                                   /* 初始化標(biāo)定板上角點(diǎn)的三維坐標(biāo) */
  117.         int i, j, t;
  118.         for (t = 0; t<image_count; t++)
  119.         {
  120.                 vector<Point3f> tempPointSet;
  121.                 for (i = 0; i<board_size.height; i++)
  122.                 {
  123.                         for (j = 0; j<board_size.width; j++)
  124.                         {
  125.                                 Point3f realPoint;
  126.                                 /* 假設(shè)標(biāo)定板放在世界坐標(biāo)系中z=0的平面上 */
  127.                                 realPoint.x = i*square_size.width;
  128.                                 realPoint.y = j*square_size.height;
  129.                                 realPoint.z = 0;
  130.                                 tempPointSet.push_back(realPoint);
  131.                         }
  132.                 }
  133.                 object_points.push_back(tempPointSet);
  134.         }
  135.         /* 初始化每幅圖像中的角點(diǎn)數(shù)量,假定每幅圖像中都可以看到完整的標(biāo)定板 */
  136.         for (i = 0; i<image_count; i++)
  137.         {
  138.                 point_counts.push_back(board_size.width*board_size.height);
  139.         }
  140.         /* 開(kāi)始標(biāo)定 */
  141.         calibrateCamera(object_points, image_points_seq, image_size, cameraMatrix, distCoeffs, rvecsMat, tvecsMat, 0);
  142.         cout << "標(biāo)定完成!\n";
  143.         //對(duì)標(biāo)定結(jié)果進(jìn)行評(píng)價(jià)
  144.         cout << "開(kāi)始評(píng)價(jià)標(biāo)定結(jié)果………………\n";
  145.         double total_err = 0.0; /* 所有圖像的平均誤差的總和 */
  146.         double err = 0.0; /* 每幅圖像的平均誤差 */
  147.         vector<Point2f> image_points2; /* 保存重新計(jì)算得到的投影點(diǎn) */
  148.         cout << "\t每幅圖像的標(biāo)定誤差:\n";
  149.         fout << "每幅圖像的標(biāo)定誤差:\n";
  150.         for (i = 0; i<image_count; i++)
  151.         {
  152.                 vector<Point3f> tempPointSet = object_points[i];
  153.                 /* 通過(guò)得到的攝像機(jī)內(nèi)外參數(shù),對(duì)空間的三維點(diǎn)進(jìn)行重新投影計(jì)算,得到新的投影點(diǎn) */
  154.                 projectPoints(tempPointSet, rvecsMat[i], tvecsMat[i], cameraMatrix, distCoeffs, image_points2);
  155.                 /* 計(jì)算新的投影點(diǎn)和舊的投影點(diǎn)之間的誤差*/
  156.                 vector<Point2f> tempImagePoint = image_points_seq[i];
  157.                 Mat tempImagePointMat = Mat(1, tempImagePoint.size(), CV_32FC2);
  158.                 Mat image_points2Mat = Mat(1, image_points2.size(), CV_32FC2);
  159.                 for (int j = 0; j < tempImagePoint.size(); j++)
  160.                 {
  161.                         image_points2Mat.at<Vec2f>(0, j) = Vec2f(image_points2[j].x, image_points2[j].y);
  162.                         tempImagePointMat.at<Vec2f>(0, j) = Vec2f(tempImagePoint[j].x, tempImagePoint[j].y);
  163.                 }
  164.                 err = norm(image_points2Mat, tempImagePointMat, NORM_L2);
  165.                 total_err += err /= point_counts[i];
  166.                 std::cout << "第" << i + 1 << "幅圖像的平均誤差:" << err << "像素" << endl;
  167.                 fout << "第" << i + 1 << "幅圖像的平均誤差:" << err << "像素" << endl;
  168.         }
  169.         std::cout << "總體平均誤差:" << total_err / image_count << "像素" << endl;
  170.         fout << "總體平均誤差:" << total_err / image_count << "像素" << endl << endl;
  171.         std::cout << "評(píng)價(jià)完成!" << endl;
  172.         //保存定標(biāo)結(jié)果         
  173.         std::cout << "開(kāi)始保存定標(biāo)結(jié)果………………" << endl;
  174.         Mat rotation_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); /* 保存每幅圖像的旋轉(zhuǎn)矩陣 */
  175.         fout << "相機(jī)內(nèi)參數(shù)矩陣:" << endl;
  176.         fout << cameraMatrix << endl << endl;
  177.         fout << "畸變系數(shù):\n";
  178.         fout << distCoeffs << endl << endl << endl;
  179.         for (int i = 0; i<image_count; i++)
  180.         {
  181.                 fout << "第" << i + 1 << "幅圖像的旋轉(zhuǎn)向量:" << endl;
  182.                 fout << tvecsMat[i] << endl;
  183.                 /* 將旋轉(zhuǎn)向量轉(zhuǎn)換為相對(duì)應(yīng)的旋轉(zhuǎn)矩陣 */
  184.                 Rodrigues(tvecsMat[i], rotation_matrix);
  185.                 fout << "第" << i + 1 << "幅圖像的旋轉(zhuǎn)矩陣:" << endl;
  186.                 fout << rotation_matrix << endl;
  187.                 fout << "第" << i + 1 << "幅圖像的平移向量:" << endl;
  188.                 fout << rvecsMat[i] << endl << endl;
  189.         }
  190.         std::cout << "完成保存" << endl;
  191.         fout << endl;
  192.         /************************************************************************
  193.         顯示定標(biāo)結(jié)果
  194.         *************************************************************************/
  195.         Mat mapx = Mat(image_size, CV_32FC1);
  196.         Mat mapy = Mat(image_size, CV_32FC1);
  197.         Mat R = Mat::eye(3, 3, CV_32F);
  198.         std::cout << "保存矯正圖像" << endl;
  199.         string imageFileName;
  200.         std::stringstream StrStm;
  201.         for (int i = 0; i != image_count; i++)
  202.         {
  203.                 std::cout << "Frame #" << i + 1 << "..." << endl;
  204.                 initUndistortRectifyMap(cameraMatrix, distCoeffs, R, cameraMatrix, image_size, CV_32FC1, mapx, mapy);
  205.                 StrStm.clear();
  206.                 imageFileName.clear();
  207.                 string filePath = "chess";
  208.                 StrStm << i + 1;
  209.                 StrStm >> imageFileName;
  210.                 filePath += imageFileName;
  211.                 filePath += ".bmp";
  212.                 Mat imageSource = imread(filePath);
  213.                 Mat newimage = imageSource.clone();
  214.                 //另一種不需要轉(zhuǎn)換矩陣的方式
  215.                 //undistort(imageSource,newimage,cameraMatrix,distCoeffs);
  216.                 remap(imageSource, newimage, mapx, mapy, INTER_LINEAR);
  217.                 StrStm.clear();
  218.                 filePath.clear();
  219.                 StrStm << i + 1;
  220.                 StrStm >> imageFileName;
  221.                 imageFileName += "_d.jpg";
  222.                 imwrite(imageFileName, newimage);
  223.         }
  224.         std::cout << "保存結(jié)束" << endl;
  225.         return;
  226. }
復(fù)制代碼

所有資料51hei提供下載:
張正友相機(jī)標(biāo)定.zip (6.57 MB, 下載次數(shù): 28)







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1