久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5764|回復: 0
打印 上一主題 下一主題
收起左側

張正友相機標定源碼 利用opencv實現(xiàn)

[復制鏈接]
跳轉到指定樓層
樓主
ID:383430 發(fā)表于 2018-8-6 14:32 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
張正友相機標定。利用opencv實現(xiàn)


單片機源程序如下:
  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"); /* 標定所用圖像文件的路徑 */
  13.         //ofstream fout("caliberation_result.txt");  /* 保存標定結果的文件 */
  14.         //                                                                                   //讀取每一幅圖像,從中提取出角點,然后對角點進行亞像素精確化       
  15.         //cout << "開始提取角點………………";
  16.         //int image_count = 0;  /* 圖像數(shù)量 */
  17.         //Size image_size;  /* 圖像的尺寸 */
  18.         //Size board_size = Size(4, 6);    /* 標定板上每行、列的角點數(shù) */
  19.         //vector<Point2f> image_points_buf;  /* 緩存每幅圖像上檢測到的角點 */
  20.         //vector<vector<Point2f>> image_points_seq; /* 保存檢測到的所有角點 */
  21.         //string filename;
  22.         //int count = -1;//用于存儲角點個數(shù)。
  23.         //while (getline(fin, filename))
  24.         //{
  25.         //        image_count++;
  26.         //        // 用于觀察檢驗輸出
  27.         //        cout << "image_count = " << image_count << endl;
  28.         //        /* 輸出檢驗*/
  29.         //        cout << "-->count = " << count;
  30.         //        Mat imageInput = imread(filename);

  31.         ofstream fout("caliberation_result.txt");  /* 保存標定結果的文件 */
  32.         cv::namedWindow("Image");
  33.         cv::Mat imageInput;
  34.         std::vector<std::string> filelist;//存放標定圖片路徑
  35.         int image_count = 0;  /* 圖像數(shù)量 */
  36.         Size image_size;  /* 圖像的尺寸 */
  37.         Size board_size = Size(4, 6);    /* 標定板上每行、列的角點數(shù) */
  38.         vector<Point2f> image_points_buf;  /* 緩存每幅圖像上檢測到的角點 */
  39.         vector<vector<Point2f>> image_points_seq; /* 保存檢測到的所有角點 */
  40.         int count = -1;//用于存儲角點個數(shù)。
  41.                                                                           //生成路徑,此處表示圖片放在工程根目錄下的chessboards文件夾
  42.         for (int i = 1; i <= 21; i++)
  43.         {
  44.                 std::stringstream str;//setw(int n)用來控制輸出間隔
  45.                 str << "chessboards/chessboard" << std::setw(2) << std::setfill('0') << i << ".jpg";//圖片的相對路徑
  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)  //讀入第一張圖片時獲取圖像寬高信息
  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.                 /* 提取角點 */
  58.                 //if (0 == findChessboardCorners(imageInput, board_size, image_points_buf))
  59.                 //{
  60.                 //        cout << "can not find chessboard corners!\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)); //對粗提取的角點進行精確化
  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);  //保存亞像素角點
  73.                                                                                                                    /* 在圖像上顯示角點位置 */
  74.                         drawChessboardCorners(view_gray, board_size, image_points_buf, false); //用于在圖片中標記角點
  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;  //每張圖片上總的角點數(shù)
  83.         for (int ii = 0; ii<total; ii++)
  84.         {
  85.                 if (0 == ii%CornerNum)// 24 是每幅圖片的角點個數(shù)。此判斷語句是為了輸出 圖片號,便于控制臺觀看
  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)        // 此判斷語句,格式化輸出,便于控制臺查看
  93.                 {
  94.                         cout << endl;
  95.                 }
  96.                 else
  97.                 {
  98.                         cout.width(10);
  99.                 }
  100.                 //輸出所有的角點
  101.                 cout << " -->" << image_points_seq[ii][0].x;
  102.                 cout << " -->" << image_points_seq[ii][0].y;
  103.         }
  104.         cout << "角點提取完成!\n";

  105.         //以下是攝像機標定
  106.         cout << "開始標定………………";
  107.         /*棋盤三維信息*/
  108.         Size square_size = Size(10, 10);  /* 實際測量得到的標定板上每個棋盤格的大小 */
  109.         vector<vector<Point3f>> object_points; /* 保存標定板上角點的三維坐標 */
  110.                                                                                    /*內外參數(shù)*/
  111.         Mat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); /* 攝像機內參數(shù)矩陣 */
  112.         vector<int> point_counts;  // 每幅圖像中角點的數(shù)量
  113.         Mat distCoeffs = Mat(1, 5, CV_32FC1, Scalar::all(0)); /* 攝像機的5個畸變系數(shù):k1,k2,p1,p2,k3 */
  114.         vector<Mat> tvecsMat;  /* 每幅圖像的旋轉向量 */
  115.         vector<Mat> rvecsMat; /* 每幅圖像的平移向量 */
  116.                                                   /* 初始化標定板上角點的三維坐標 */
  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.                                 /* 假設標定板放在世界坐標系中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.         /* 初始化每幅圖像中的角點數(shù)量,假定每幅圖像中都可以看到完整的標定板 */
  136.         for (i = 0; i<image_count; i++)
  137.         {
  138.                 point_counts.push_back(board_size.width*board_size.height);
  139.         }
  140.         /* 開始標定 */
  141.         calibrateCamera(object_points, image_points_seq, image_size, cameraMatrix, distCoeffs, rvecsMat, tvecsMat, 0);
  142.         cout << "標定完成!\n";
  143.         //對標定結果進行評價
  144.         cout << "開始評價標定結果………………\n";
  145.         double total_err = 0.0; /* 所有圖像的平均誤差的總和 */
  146.         double err = 0.0; /* 每幅圖像的平均誤差 */
  147.         vector<Point2f> image_points2; /* 保存重新計算得到的投影點 */
  148.         cout << "\t每幅圖像的標定誤差:\n";
  149.         fout << "每幅圖像的標定誤差:\n";
  150.         for (i = 0; i<image_count; i++)
  151.         {
  152.                 vector<Point3f> tempPointSet = object_points[i];
  153.                 /* 通過得到的攝像機內外參數(shù),對空間的三維點進行重新投影計算,得到新的投影點 */
  154.                 projectPoints(tempPointSet, rvecsMat[i], tvecsMat[i], cameraMatrix, distCoeffs, image_points2);
  155.                 /* 計算新的投影點和舊的投影點之間的誤差*/
  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 << "評價完成!" << endl;
  172.         //保存定標結果         
  173.         std::cout << "開始保存定標結果………………" << endl;
  174.         Mat rotation_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); /* 保存每幅圖像的旋轉矩陣 */
  175.         fout << "相機內參數(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 << "幅圖像的旋轉向量:" << endl;
  182.                 fout << tvecsMat[i] << endl;
  183.                 /* 將旋轉向量轉換為相對應的旋轉矩陣 */
  184.                 Rodrigues(tvecsMat[i], rotation_matrix);
  185.                 fout << "第" << i + 1 << "幅圖像的旋轉矩陣:" << 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.         顯示定標結果
  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.                 //另一種不需要轉換矩陣的方式
  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 << "保存結束" << endl;
  225.         return;
  226. }
復制代碼

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


評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 九色www | 日韩高清中文字幕 | 中文字幕视频三区 | 亚洲v日韩v综合v精品v | 免费一级欧美在线观看视频 | 国产精品.xx视频.xxtv | www.久久精品视频 | 91国内精品久久 | 久久久久久久久久久久久91 | 欧美区在线| 久久精品国产免费高清 | 麻豆hd| 久久中文一区二区 | 欧美性乱 | 午夜视频在线观看视频 | 久久国产精品一区 | 久久精品亚洲 | 一区二区在线不卡 | 罗宾被扒开腿做同人网站 | 欧美成人第一页 | 激情国产在线 | 青青久草 | 中文字幕成人免费视频 | 久在线 | 亚洲国产精久久久久久久 | 精品免费国产一区二区三区四区介绍 | 国产欧美精品一区二区色综合朱莉 | 中文字幕a√ | 久草在线视频中文 | 欧美三级电影在线播放 | 亚洲国产一区二区三区 | 天天操人人干 | 日本在线综合 | 高清av电影 | 亚洲在线一区 | 中文字幕在线观看www | 精品免费av | 一级视频在线免费观看 | 日本偷偷操 | 久久性av | 亚洲综合色视频在线观看 |