cameraCalibration.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #include <opencv2/opencv.hpp>
  2. #include <opencv2/calib3d/calib3d.hpp>
  3. #include <opencv2/highgui/highgui.hpp>
  4. #include <opencv2/imgproc/imgproc.hpp>
  5. #include <stdio.h>
  6. #include <iostream>
  7. // Defining the dimensions of checkerboard
  8. int CHECKERBOARD[2]{6,9};
  9. int main()
  10. {
  11. // Creating vector to store vectors of 3D points for each checkerboard image
  12. std::vector<std::vector<cv::Point3f> > objpoints;
  13. // Creating vector to store vectors of 2D points for each checkerboard image
  14. std::vector<std::vector<cv::Point2f> > imgpoints;
  15. // Defining the world coordinates for 3D points
  16. std::vector<cv::Point3f> objp;
  17. for(int i{0}; i<CHECKERBOARD[1]; i++)
  18. {
  19. for(int j{0}; j<CHECKERBOARD[0]; j++)
  20. objp.push_back(cv::Point3f(j,i,0));
  21. }
  22. // Extracting path of individual image stored in a given directory
  23. std::vector<cv::String> images;
  24. // Path of the folder containing checkerboard images
  25. std::string path = "./images/*.jpg";
  26. cv::glob(path, images);
  27. cv::Mat frame, gray;
  28. // vector to store the pixel coordinates of detected checker board corners
  29. std::vector<cv::Point2f> corner_pts;
  30. bool success;
  31. // Looping over all the images in the directory
  32. for(int i{0}; i<images.size(); i++)
  33. {
  34. frame = cv::imread(images[i]);
  35. cv::cvtColor(frame,gray,cv::COLOR_BGR2GRAY);
  36. // Finding checker board corners
  37. // If desired number of corners are found in the image then success = true
  38. success = cv::findChessboardCorners(gray,cv::Size(CHECKERBOARD[0],CHECKERBOARD[1]), corner_pts, cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_FAST_CHECK | cv::CALIB_CB_NORMALIZE_IMAGE);
  39. /*
  40. * If desired number of corner are detected,
  41. * we refine the pixel coordinates and display
  42. * them on the images of checker board
  43. */
  44. if(success)
  45. {
  46. cv::TermCriteria criteria(cv::TermCriteria::EPS | cv::TermCriteria::MAX_ITER, 30, 0.001);
  47. // refining pixel coordinates for given 2d points.
  48. cv::cornerSubPix(gray,corner_pts,cv::Size(11,11), cv::Size(-1,-1),criteria);
  49. // Displaying the detected corner points on the checker board
  50. cv::drawChessboardCorners(frame, cv::Size(CHECKERBOARD[0],CHECKERBOARD[1]), corner_pts,success);
  51. objpoints.push_back(objp);
  52. imgpoints.push_back(corner_pts);
  53. }
  54. cv::imshow("Image",frame);
  55. cv::waitKey(0);
  56. }
  57. cv::destroyAllWindows();
  58. cv::Mat cameraMatrix,distCoeffs,R,T;
  59. /*
  60. * Performing camera calibration by
  61. * passing the value of known 3D points (objpoints)
  62. * and corresponding pixel coordinates of the
  63. * detected corners (imgpoints)
  64. */
  65. cv::calibrateCamera(objpoints, imgpoints,cv::Size(gray.rows,gray.cols),cameraMatrix,distCoeffs,R,T);
  66. std::cout << "cameraMatrix : " << cameraMatrix << std::endl;
  67. std::cout << "distCoeffs : " << distCoeffs << std::endl;
  68. std::cout << "Rotation vector : " << R << std::endl;
  69. std::cout << "Translation vector : " << T << std::endl;
  70. return 0;
  71. }