1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
| #include <iostream> #include <sstream> #include <string> #include <ctime> #include <cstdio> #include <vector> #include <opencv2/core.hpp> #include <opencv2/core/utility.hpp> #include <opencv2/imgproc.hpp> #include <opencv2/calib3d.hpp> #include <opencv2/imgcodecs.hpp> #include <opencv2/videoio.hpp> #include <opencv2/highgui.hpp>
using namespace cv; using namespace std; string imageList[]={string("./n1.JPG"),string("./n2.JPG"),string("./n3.JPG"),string("./n4.JPG"),\ string("./n5.JPG"),string("./n6.JPG"),string("./n7.JPG"),string("./n8.JPG"),\ string("./n9.JPG"),string("./n10.JPG"),string("./n11.JPG"),string("./n12.JPG") }; CvSize boardSize(5,7); int photo_size=12; float squareSize=50; int main(){ vector< Point3f > CornerPoints; vector<vector< Point3f >> ObjectPoints; vector<vector< Point2f >> imagePoints; vector<Mat> save_view; bool have_set=false; CvSize ImageSize; for(int i=0;i<photo_size;i++){ Mat view = imread(imageList[i], 1); cout<<view.size()<<endl; imshow("input", view); ImageSize=view.size(); std::vector<cv::Point2f> ptvec; int corner_count=9; bool found = findChessboardCorners( view, boardSize, ptvec ,CALIB_CB_ADAPTIVE_THRESH );
drawChessboardCorners(view,boardSize,ptvec,found); imshow("input", view); save_view.push_back(view); imagePoints.push_back(ptvec); if(have_set==false){ have_set=true; for(int j=0;j<boardSize.height;j++){ for(int k=0;k<boardSize.width;k++){ CornerPoints.push_back(Point3f(j*squareSize,k*squareSize,0)); } } }
} ObjectPoints.resize(imagePoints.size(),CornerPoints); Mat cameraMatrix; cameraMatrix= Mat::eye(3, 3, CV_64F); cameraMatrix.at<double>(0,0) = 1; Mat distCoeffs; distCoeffs=Mat::zeros(8, 1, CV_64F); int iFixedPoint = -1; vector<Mat> rvecs;vector<Mat> tvecs; double rms = calibrateCamera(ObjectPoints, imagePoints, ImageSize, cameraMatrix, distCoeffs, rvecs, tvecs, CALIB_FIX_ASPECT_RATIO | CALIB_USE_LU); cout<<cameraMatrix<<endl; double totalErr=0,totalPoints=0,err; for(int i=0;i<photo_size;i++){ vector< Point2f > projectedPoints; cv::projectPoints(ObjectPoints[i], rvecs[i], tvecs[i],cameraMatrix,\ distCoeffs, projectedPoints); err = norm(imagePoints[i], projectedPoints, NORM_L2); cout<<i<<":"<<endl; drawChessboardCorners(save_view[i],boardSize,projectedPoints,true); imshow("input2", save_view[i]);cv::waitKey(1000); cout<<err<<endl; totalErr += err*err; totalPoints += ObjectPoints[i].size(); } cout<<std::sqrt(totalErr/totalPoints)<<endl; }
|