opencv4.0棋盘格标定c++
生活随笔
收集整理的這篇文章主要介紹了
opencv4.0棋盘格标定c++
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考?https://blog.csdn.net/u011574296/article/details/73823569?修改
#include <opencv2/opencv.hpp>#include <iostream>
#include <fstream>
#include <string>
#include <vector>using namespace cv;
using namespace std;void m_calibration(vector<string> &FilesName, Size board_size, Size square_size, Mat &cameraMatrix, Mat &distCoeffs, vector<Mat> &rvecsMat, vector<Mat> &tvecsMat)
{ofstream fout("caliberation_result.txt"); // 保存標定結果的文件cout << "開始提取角點………………" << endl;int image_count = 0; // 圖像數量Size image_size; // 圖像的尺寸vector<Point2f> image_points; // 緩存每幅圖像上檢測到的角點vector<vector<Point2f>> image_points_seq; // 保存檢測到的所有角點for (int i = 0; i < FilesName.size(); i++){image_count++;// 用于觀察檢驗輸出cout << "image_count = " << image_count << endl;Mat imageInput = imread(FilesName[i]);if (image_count == 1) //讀入第一張圖片時獲取圖像寬高信息{image_size.width = imageInput.cols;image_size.height = imageInput.rows;cout << "image_size.width = " << image_size.width << endl;cout << "image_size.height = " << image_size.height << endl;}/* 提取角點 */bool bRes = findChessboardCorners(imageInput, board_size, image_points, 0);if (bRes){Mat view_gray;cout << "imageInput.channels()=" << imageInput.channels() << endl;cvtColor(imageInput, view_gray, cv::COLOR_RGB2GRAY);/* 亞像素精確化 */cv::cornerSubPix(view_gray, image_points, cv::Size(11, 11), cv::Size(-1, -1), cv::TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 30, 0.01));image_points_seq.push_back(image_points); //保存亞像素角點/* 在圖像上顯示角點位置 */drawChessboardCorners(view_gray, board_size, image_points, true);//imshow("Camera Calibration", view_gray);//顯示圖片//waitKey(100);//暫停0.1S}else{cout << "第" << image_count << "張照片提取角點失敗,請刪除后,重新標定!" << endl; //找不到角點imshow("失敗照片", imageInput);waitKey(0);}}cout << "角點提取完成!!!" << endl;/*棋盤三維信息*/vector<vector<Point3f>> object_points_seq; // 保存標定板上角點的三維坐標for (int t = 0; t < image_count; t++){vector<Point3f> object_points;for (int i = 0; i < board_size.height; i++){for (int j = 0; j < board_size.width; j++){Point3f realPoint;/* 假設標定板放在世界坐標系中z=0的平面上 */realPoint.x = i * square_size.width;realPoint.y = j * square_size.height;realPoint.z = 0;object_points.push_back(realPoint);}}object_points_seq.push_back(object_points);}/* 運行標定函數 */double rms = calibrateCamera(object_points_seq, image_points_seq, image_size, cameraMatrix, distCoeffs, rvecsMat, tvecsMat, cv::CALIB_FIX_K3 + cv::CALIB_ZERO_TANGENT_DIST);cout << "RMS:" << rms << "像素" << endl << endl;cout << "標定完成!!!" << endl;cout << "開始評價標定結果………………";double total_err = 0.0; // 所有圖像的平均誤差的總和double err = 0.0; // 每幅圖像的平均誤差double totalErr = 0.0;double totalPoints = 0.0;vector<Point2f> image_points_pro; // 保存重新計算得到的投影點for (int i = 0; i < image_count; i++){projectPoints(object_points_seq[i], rvecsMat[i], tvecsMat[i], cameraMatrix, distCoeffs, image_points_pro); //通過得到的攝像機內外參數,對角點的空間三維坐標進行重新投影計算err = norm(Mat(image_points_seq[i]), Mat(image_points_pro), NORM_L2);totalErr += err * err;totalPoints += object_points_seq[i].size();err /= object_points_seq[i].size();//fout << "第" << i + 1 << "幅圖像的平均誤差:" << err << "像素" << endl;total_err += err;}fout << "重投影誤差2:" << sqrt(totalErr / totalPoints) << "像素" << endl << endl;fout << "重投影誤差3:" << total_err / image_count << "像素" << endl << endl;cout << "x = " << cameraMatrix.at<double>(0, 2) << endl;cout << "y = " << cameraMatrix.at<double>(1, 2) << endl;//保存定標結果// cout << "開始保存定標結果………………" << endl;// Mat rotation_matrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); /* 保存每幅圖像的旋轉矩陣 */// fout << "相機內參數矩陣:" << endl;fout << cameraMatrix << endl << endl;// fout << "畸變系數:\n";// fout << distCoeffs << endl << endl << endl;// for (int i = 0; i < image_count; i++)// {// fout << "第" << i + 1 << "幅圖像的旋轉向量:" << endl;// fout << rvecsMat[i] << endl;// /* 將旋轉向量轉換為相對應的旋轉矩陣 */// Rodrigues(rvecsMat[i], rotation_matrix);// fout << "第" << i + 1 << "幅圖像的旋轉矩陣:" << endl;// fout << rotation_matrix << endl;// fout << "第" << i + 1 << "幅圖像的平移向量:" << endl;// fout << tvecsMat[i] << endl << endl;// }cout << "定標結果完成保存!!!" << endl;fout << endl;
}int main()
{string File_Directory1 = "./*.jpg"; //文件夾目錄1std::vector<cv::String> filenames;// getFilesName(File_Directory1, FileType, FilesName1); // 標定所用圖像文件的路徑cv::glob(File_Directory1, filenames);Size board_size = Size(11, 8); // 標定板上每行、列的角點數Size square_size = Size(30, 30); // 實際測量得到的標定板上每個棋盤格的物理尺寸,單位mmMat cameraMatrix = Mat(3, 3, CV_32FC1, Scalar::all(0)); // 攝像機內參數矩陣Mat distCoeffs = Mat(1, 5, CV_32FC1, Scalar::all(0)); // 攝像機的5個畸變系數:k1,k2,p1,p2,k3vector<Mat> rvecsMat; // 存放所有圖像的旋轉向量,每一副圖像的旋轉向量為一個matvector<Mat> tvecsMat; // 存放所有圖像的平移向量,每一副圖像的平移向量為一個matm_calibration(filenames, board_size, square_size, cameraMatrix, distCoeffs, rvecsMat, tvecsMat);//m_undistort(FilesName1, image_size, cameraMatrix, distCoeffs);return 0;
}
?
總結
以上是生活随笔為你收集整理的opencv4.0棋盘格标定c++的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: opencv拟合高维曲线
- 下一篇: pyqt5 视频播放器