版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/u013498583/article/details/71703175
<link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-e2445db1a8.css" target="_blank" rel="external nofollow" >
<div class="htmledit_views">
海爾630冰箱的标定和畸變校正。上篇文章中直接使用OpenCV的例程進行畸變校正的效果不太理想。使用以下方法(張正友)效果更好。
京東連結:
http://item.jd.com/4027713.html#crumb-wrap
标定代碼:
- /************************************************************************
- 運作環境:VS2013+OpenCV 2.4.13
- 運作結果:檢測拍攝的棋盤畸變圖像,擷取攝像頭的畸變資訊
- *************************************************************************/
- #include <opencv2\opencv.hpp>
- #include <fstream>
- #include <iostream>
- using namespace std;
- using namespace cv;
- char dir[ ];
- char fileNames[ ];
- char chess_boardImage_path[] = "E:\\hanxiaoxuan\\distort\\";
- char chess_boardDetect_path[] = "E:\\hanxiaoxuan\\distort_detect\\";
- char chess_boardCorner_path[] = "E:\\hanxiaoxuan\\distort_corner\\";
- char calibrationResult[] = "E:\\hanxiaoxuan\\calibration_result.txt";
- char datFileName[] = "E:\\hanxiaoxuan\\camParam.dat";
- int main()
- {
- string *imageList = new string[ ];
- string *chess_boardList = new string[ ];
- ofstream fout(calibrationResult); //儲存标定結果的檔案
- // 利用dir指令将目前目錄下的.jpg檔案名寫入names.txt
- sprintf(dir, "%s%s%s%s%s%s", "dir ", chess_boardImage_path, "*.jpg", " /a /b >", chess_boardImage_path, "names.txt");
- system(dir);
- char name[ ] = "";
- // 打開檔案讀取其中的檔案名
- sprintf(fileNames, "%s%s", chess_boardImage_path, "names.txt");
- FILE* fp = fopen(fileNames, "r");
- if ( NULL == fp)
- printf( "error,cannot open the name list");
- // 獲得檔案數量
- int line = ;
- while (fgets(name, , fp) != NULL)
- {
- char subname[ ];
- sscanf(name, "%[^\n]%s", subname);
- string image_name;
- stringstream stream;
- stream << subname;
- image_name = stream.str();
- imageList[line] = image_name.substr( , image_name.length() - );
- line++;
- }
- //讀取每一幅圖像,從中提取出角點,然後對角點進行亞像素精确化
- cout << "開始提取角點………………" << endl;
- int image_count = line; //圖像數量
- Size board_size = Size( , ); //棋盤上每行、列的内角點數
- vector<Point2f> corners; //緩存每幅圖像上檢測到的角點
- vector< vector<Point2f>> corners_Seq; //儲存檢測到的所有角點
- vector<Mat> image_Seq;
- int successImageNum = ; //成功提取角點的棋盤圖數量
- int count = ;
- for ( int i = ; i != image_count; i++)
- {
- cout << "Frame #" << i + << "..." << endl;
- string imageFileName;
- imageFileName = imageList[i]; //圖像的檔案名
- imageFileName += ".jpg"; //圖像的檔案名.jpg
- cv::Mat image = imread(chess_boardImage_path + imageFileName);
- //提取角點
- cv::Mat imageGray;
- cvtColor(image, imageGray, CV_RGB2GRAY);
- bool patternfound = findChessboardCorners(image, board_size, corners, CALIB_CB_ADAPTIVE_THRESH + CALIB_CB_NORMALIZE_IMAGE +
- CALIB_CB_FAST_CHECK);
- if (!patternfound)
- {
- cout << "can not find chessboard corners!\n";
- continue;
- exit( );
- }
- else
- {
- //亞像素精确化
- cornerSubPix(imageGray, corners, Size( , ), Size( , ), TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, , ));
- //繪制檢測到的角點并儲存
- Mat imageTemp = image.clone();
- for ( int j = ; j < corners.size(); j++)
- {
- circle(imageTemp, corners[j], , Scalar( , , ), , , );
- }
- string imageFileName;
- imageFileName = imageList[i];
- imageFileName += "_corner.jpg";
- imwrite(chess_boardCorner_path + imageFileName, imageTemp);
- cout << "Frame corner#" << i + << "...end" << endl;
- imwrite(chess_boardDetect_path + imageFileName, image);
- chess_boardList[successImageNum] = imageList[i];
- count = count + corners.size();
- successImageNum = successImageNum + ;
- corners_Seq.push_back(corners);
- }
- image_Seq.push_back(image);
- }
- cout << "角點提取完成!\n";
- //錄影機标定
- cout << "開始标定………………" << endl;
- Size square_size = Size( , );
- vector< vector<Point3f>> object_Points; //儲存标定闆上角點的三維坐标
- Mat image_points = Mat( , count, CV_32FC2, Scalar::all( )); //儲存提取的所有角點
- vector< int> point_counts;
- //初始化标定闆上角點的三維坐标
- for ( int t = ; t < successImageNum; t++)
- {
- vector<Point3f> tempPointSet;
- for ( int i = ; i < board_size.height; i++)
- {
- for ( int j = ; j < board_size.width; j++)
- {
- //假設标定闆放在世界坐标系中z=0的平面上
- Point3f tempPoint;
- tempPoint.x = i*square_size.width;
- tempPoint.y = j*square_size.height;
- tempPoint.z = ;
- tempPointSet.push_back(tempPoint);
- }
- }
- object_Points.push_back(tempPointSet);
- }
- for ( int i = ; i < successImageNum; i++)
- {
- point_counts.push_back(board_size.width*board_size.height);
- }
- //開始标定
- Size image_size = image_Seq[ ].size();
- cv::Matx33d intrinsic_matrix; //錄影機内參數矩陣
- cv::Vec4d distortion_coeffs; //錄影機的4個畸變系數:k1,k2,k3,k4
- std:: vector<cv::Vec3d> rotation_vectors; //每幅圖像的旋轉向量
- std:: vector<cv::Vec3d> translation_vectors; //每幅圖像的平移向量
- int flags = ;
- flags |= cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC;
- flags |= cv::fisheye::CALIB_CHECK_COND;
- flags |= cv::fisheye::CALIB_FIX_SKEW;
- fisheye::calibrate(object_Points, corners_Seq, image_size, intrinsic_matrix, distortion_coeffs, rotation_vectors, translation_vectors, flags, cv::TermCriteria( , , ));
- cout << "标定完成!\n";
- FILE *camParam = fopen(datFileName, "wb");
- if (camParam == NULL) {
- std:: cout << "can not create data file: " << datFileName << " !!!" << std:: endl;
- return false;
- }
- fwrite(&intrinsic_matrix, sizeof(cv::Matx33d), , camParam);
- fwrite(&distortion_coeffs, sizeof(cv::Vec4d), , camParam);
- fwrite(&image_size, sizeof(Size), , camParam);
- fclose(camParam);
- //對标定結果進行評價
- cout << "開始評價标定結果………………" << endl;
- double total_err = ; //所有圖像的平均誤差的總和
- double err = ; //每幅圖像的平均誤差
- vector<Point2f> image_points2; //儲存重新計算得到的投影點
- cout << "每幅圖像的标定誤差:" << endl;
- cout << "每幅圖像的标定誤差:" << endl << endl;
- for ( int i = ; i < successImageNum; i++)
- {
- vector<Point3f> tempPointSet = object_Points[i];
- //通過得到的錄影機内外參數,對空間的三維點進行重新投影計算,得到新的投影點
- fisheye::projectPoints(tempPointSet, image_points2, rotation_vectors[i], translation_vectors[i], intrinsic_matrix, distortion_coeffs);
- //計算新的投影點和舊的投影點之間的誤差
- vector<Point2f> tempImagePoint = corners_Seq[i];
- Mat tempImagePointMat = Mat( , tempImagePoint.size(), CV_32FC2);
- Mat image_points2Mat = Mat( , image_points2.size(), CV_32FC2);
- for ( size_t i = ; i != tempImagePoint.size(); i++)
- {
- image_points2Mat.at<Vec2f>( , i) = Vec2f(image_points2[i].x, image_points2[i].y);
- tempImagePointMat.at<Vec2f>( , i) = Vec2f(tempImagePoint[i].x, tempImagePoint[i].y);
- }
- err = norm(image_points2Mat, tempImagePointMat, NORM_L2);
- total_err += err /= point_counts[i];
- cout << "第" << i + << "幅圖像的平均誤差:" << err << "像素" << endl;
- fout << "第" << i + << "幅圖像的平均誤差:" << err << "像素" << endl;
- }
- cout << "總體平均誤差:" << total_err / image_count << "像素" << endl;
- fout << "總體平均誤差:" << total_err / image_count << "像素" << endl << endl;
- cout << "評價完成!" << endl;
- //儲存标定結果
- cout << "開始儲存标定結果………………" << endl;
- Mat rotation_matrix = Mat( , , CV_32FC1, Scalar::all( )); //儲存每幅圖像的旋轉矩陣
- fout << "相機内參數矩陣:" << endl;
- fout << intrinsic_matrix << endl;
- fout << "畸變系數:\n";
- fout << distortion_coeffs << endl;
- for ( int i = ; i < successImageNum; i++)
- {
- fout << "第" << i + << "幅圖像的旋轉向量:" << endl;
- fout << rotation_vectors[i] << endl;
- //将旋轉向量轉換為相對應的旋轉矩陣
- Rodrigues(rotation_vectors[i], rotation_matrix);
- fout << "第" << i + << "幅圖像的旋轉矩陣:" << endl;
- fout << rotation_matrix << endl;
- fout << "第" << i + << "幅圖像的平移向量:" << endl;
- fout << translation_vectors[i] << endl;
- }
- cout << "完成儲存" << endl;
- fout << endl;
- //顯示标定結果
- Mat mapx = Mat(image_size, CV_32FC1);
- Mat mapy = Mat(image_size, CV_32FC1);
- Mat R = Mat::eye( , , CV_32F);
- cout << "儲存矯正圖像" << endl;
- for ( int i = ; i != successImageNum; i++)
- {
- cout << "Frame #" << i + << "..." << endl;
- Mat newCameraMatrix = Mat( , , CV_32FC1, Scalar::all( ));
- fisheye::initUndistortRectifyMap(intrinsic_matrix, distortion_coeffs, R, intrinsic_matrix, image_size, CV_32FC1, mapx, mapy);
- Mat t = image_Seq[i].clone();
- cv::remap(image_Seq[i], t, mapx, mapy, INTER_LINEAR);
- string imageFileName;
- imageFileName = chess_boardList[i];
- imageFileName += "_d.jpg";
- imwrite(chess_boardCorner_path + imageFileName, t);
- }
- cout << "儲存結束" << endl;
- delete [] imageList;
- delete [] chess_boardList;
- return ;
- }
得到兩組參數,分别是相機的内參矩陣和畸變系數。儲存在camPara.dat供calibration代碼校正使用。
校正代碼:
- /************************************************************************
- 運作環境:VS2013+OpenCV 2.4.13
- 運作結果:根據攝像頭的畸變資訊,進行相機标定
- *************************************************************************/
- #include <opencv2\opencv.hpp>
- #include <fstream>
- #include <iostream>
- using namespace std;
- using namespace cv;
- char dir[ ];
- char test_fileNames[ ];
- char testFile_path[] = "E:\\hanxiaoxuan\\distort\\";
- char outputFile_path[] = "E:\\hanxiaoxuan\\undistort\\";
- char test_datFileName[] = "E:\\hanxiaoxuan\\camParam.dat";
- //string test_imageList[100];
- int main()
- {
- string *test_imageList = new string[ ];
- // 利用dir指令将目前目錄下的.jpg檔案名寫入test_names.txt
- sprintf(dir, "%s%s%s%s%s%s", "dir ", testFile_path, "*.jpg", " /a /b >", testFile_path, "test_names.txt");
- system(dir);
- char test_name[ ] = "";
- // 打開檔案讀取其中的檔案名
- sprintf(test_fileNames, "%s%s", testFile_path, "test_names.txt");
- FILE* fp_test = fopen(test_fileNames, "r");
- if ( NULL == fp_test)
- printf( "error,cannot open the name list");
- // 獲得檔案數量
- int line = ;
- while (fgets(test_name, , fp_test) != NULL)
- {
- char subname[ ];
- sscanf(test_name, "%[^\n]%s", subname);
- string image_name;
- stringstream stream;
- stream << subname;
- image_name = stream.str();
- test_imageList[line] = image_name.substr( , image_name.length() - );
- line++;
- }
- string testName;
- //利用錄影機畸變參數對圖檔進行矯正
- cout << "儲存矯正圖像" << endl;
- for ( int i = ; i < line; i++)
- {
- cout << "Frame #" << i + << "..." << endl;
- testName = test_imageList[i] + ".jpg";
- Mat testImage = imread(testFile_path + testName);
- cv::Matx33d test_intrinsic_matrix;
- cv::Vec4d test_distortion_coeffs;
- Size test_image_size = testImage.size();
- FILE *test_camParam = fopen(test_datFileName, "rb");
- if (test_camParam == NULL) {
- std:: cout << "can not create data file: " << test_datFileName << " !!!" << std:: endl;
- return false;
- }
- fread(&test_intrinsic_matrix, sizeof(cv::Matx33d), , test_camParam);
- fread(&test_distortion_coeffs, sizeof(cv::Vec4d), , test_camParam);
- fread(&test_image_size, sizeof(Size), , test_camParam);
- fclose(test_camParam);
- Mat test_mapx = Mat(test_image_size, CV_32FC1);
- Mat test_mapy = Mat(test_image_size, CV_32FC1);
- Mat test_R = Mat::eye( , , CV_32F);
- fisheye::initUndistortRectifyMap(test_intrinsic_matrix, test_distortion_coeffs, test_R, test_intrinsic_matrix, test_image_size, CV_32FC1, test_mapx, test_mapy);
- Mat t = testImage.clone();
- cv::remap(testImage, t, test_mapx, test_mapy, INTER_LINEAR);
- imwrite(outputFile_path + testName, t);
- }
- cout << "标定結束" << endl;
- delete [] test_imageList;
- return ;
- }
校正效果如下:
原圖1
原圖校正後
原圖2
校正後
校正效果基本令人滿意。
</div>
</div>