fork download
  1. #include <opencv2/core/core.hpp>
  2. #include <opencv2/highgui/highgui.hpp>
  3. #include <opencv2/calib3d/calib3d.hpp>
  4. #include <opencv2/legacy/legacy.hpp>
  5. #include <opencv2/calib3d/calib3d.hpp>
  6. #include <iostream>
  7. #include <string>
  8. #include <vector>
  9. #include <time.h>
  10. using namespace std;
  11.  
  12.  
  13. // This function initializes the object points of chessboard corners.
  14. // The coordinates are stored in a vector<vector<cv::Point3f>>.
  15. // The object points can be used for calibrateCamera().
  16.  
  17. void chessboardObjectPoints(const cv::Size patternSize, // # of inner corners per a chessboard row and column
  18. int nViews, // # of views (images) of chessboard
  19. double squareSizeX, // width of square (x distance between corners)
  20. double squareSizeY, // height of square (y distance between corners)
  21. vector<vector<cv::Point3f>> & object_points) // object points of chessboard corners
  22. {
  23. int iView, iY, iX, nY, nX, ic;
  24. nY = patternSize.height;
  25. nX = patternSize.width;
  26.  
  27. // resize the object_points object
  28. object_points.clear();
  29. object_points.resize(nViews);
  30. for (iView = 0; iView < nViews; iView++) {
  31. object_points[iView].resize(nY*nX);
  32. }
  33.  
  34. // fill the object points
  35. for (iView = 0; iView < nViews; iView++) {
  36. ic = 0;
  37. for (iY = nY - 1; iY >= 0; iY--) {
  38. for (iX = 0; iX < nX; iX++) {
  39. object_points[iView][ic].x = (float)(iX * squareSizeX);
  40. object_points[iView][ic].y = (float)(iY * squareSizeY);
  41. object_points[iView][ic].z = (float) 0.0;
  42. ic++;
  43. }
  44. }
  45. }
  46. }
  47.  
  48.  
  49. int main() {
  50. double t0;
  51.  
  52. // read the image file and put the image into my variable.
  53. vector<string> calFilenames;
  54. calFilenames.push_back("sample/20130120_222211.JPG");
  55. calFilenames.push_back("sample/20130120_222233.JPG");
  56. calFilenames.push_back("sample/20130120_222247.JPG");
  57. calFilenames.push_back("sample/20130120_222254.JPG");
  58. calFilenames.push_back("sample/20130120_222313.JPG");
  59. calFilenames.push_back("sample/20130120_222320.JPG");
  60.  
  61. // open an OpenCV window
  62. cv::namedWindow("Chessboard");
  63.  
  64. cv::Size patternSize( 10, 6 ); // patternSize – # of inner corners per a chessboard row and column
  65. cv::Size myCalibImgSize;
  66. vector<cv::Mat> cornersVec;
  67. cornersVec.resize(calFilenames.size());
  68.  
  69. for (int i=0; i< (int) calFilenames.size(); i++) {
  70. cout << "Reading cal image " << i << "...";
  71. t0 = clock();
  72. cv::Mat myCalibImg= cv::imread(calFilenames[i], CV_LOAD_IMAGE_GRAYSCALE );
  73. cout << (clock()-t0)/CLOCKS_PER_SEC << " sec.\n";
  74. myCalibImgSize.width = myCalibImg.cols;
  75. myCalibImgSize.height = myCalibImg.rows;
  76. // cout << "Size: " << myCalibImg.cols << "x" << myCalibImg.rows << endl;
  77.  
  78. // find chessboard corner
  79. bool found;
  80. cv::Mat corners(10, 6, CV_32FC2);
  81. cout << "Finding cal image " << i << "...";
  82. t0 = clock();
  83. found = cv::findChessboardCorners( myCalibImg, patternSize, corners, CV_CALIB_CB_ADAPTIVE_THRESH );
  84. cout << (clock()-t0)/CLOCKS_PER_SEC << " sec.\n";
  85. if ( found == true ) {
  86. cout << "Finding subpixel of image " << i << "...";
  87. t0 = clock();
  88. cv::cornerSubPix( myCalibImg, corners, cv::Size(5,5), cv::Size(-1,-1),
  89. cv::TermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 100, 0.01));
  90. cout << (clock()-t0)/CLOCKS_PER_SEC << " sec.\n";
  91. } else {
  92. cout << "Can not find corners of image " << i << "\n";
  93. }
  94.  
  95. // draw chessboard
  96. drawChessboardCorners(myCalibImg, patternSize, corners, found);
  97. cv::resize(myCalibImg, myCalibImg, cv::Size(800,600), 0, 0, cv::INTER_LINEAR );
  98.  
  99. // show image
  100. cv::imshow("Chessboard", myCalibImg );
  101. cv::waitKey( 100 );
  102.  
  103. // data copy
  104. cornersVec[i] = corners;
  105. }
  106.  
  107.  
  108. // generate object points
  109. vector<vector<cv::Point3f>> object_points;
  110. chessboardObjectPoints( patternSize, calFilenames.size(), 23.8, 23.8,
  111. object_points );
  112.  
  113. // calibration
  114. cv::Mat cameraMatrix(3, 3, CV_64FC1);
  115. cv::Mat distCoeffs (1, 8, CV_64FC1);
  116. vector<vector<double>> rvecs, tvecs;
  117. int flag = CV_CALIB_RATIONAL_MODEL ;
  118.  
  119. double reprojectionError =
  120. cv::calibrateCamera(object_points, cornersVec, myCalibImgSize,
  121. cameraMatrix,
  122. distCoeffs,
  123. rvecs, tvecs,
  124. flag,
  125. cv::TermCriteria( cv::TermCriteria::COUNT+cv::TermCriteria::EPS, 30, DBL_EPSILON) );
  126. cout << "Camera matrix:\n" << cameraMatrix << endl;
  127. cout << "Image size: " << myCalibImgSize.width << " " <<
  128. myCalibImgSize.height << endl;
  129. cout << "Camera matrix:\n" << cameraMatrix << endl;
  130. cout << "Dist. coeffic:\n" << distCoeffs << endl;
  131. cout << "Fx: " << cameraMatrix.at<double>(0,0) << endl;
  132. cout << "Fy: " << cameraMatrix.at<double>(1,1) << endl;
  133. cout << "Cx: " << cameraMatrix.at<double>(0,2) << endl;
  134. cout << "Cy: " << cameraMatrix.at<double>(1,2) << endl;
  135. cout << "k1: " << distCoeffs .at<double>(0) << endl;
  136. cout << "k2: " << distCoeffs .at<double>(1) << endl;
  137. cout << "p1: " << distCoeffs .at<double>(2) << endl;
  138. cout << "p2: " << distCoeffs .at<double>(3) << endl;
  139. cout << "k3: " << distCoeffs .at<double>(4) << endl;
  140. cout << "k4: " << distCoeffs .at<double>(5) << endl;
  141. cout << "k5: " << distCoeffs .at<double>(6) << endl;
  142. cout << "k6: " << distCoeffs .at<double>(7) << endl;
  143. cout << "Reproj. error: " << reprojectionError << endl;
  144. cout << "Finished.\n";
  145.  
  146. cv::waitKey( 0 );
  147.  
  148. return 0; // Or return 1;
  149. }
  150.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:33: fatal error: opencv2/core/core.hpp: No such file or directory
compilation terminated.
stdout
Standard output is empty