Undistort.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. // Trying to undistort the image using the camera parameters obtained from calibration
  71. cv::Mat image;
  72. image = cv::imread(images[0]);
  73. cv::Mat dst, map1, map2,new_camera_matrix;
  74. cv::Size imageSize(cv::Size(image.cols,image.rows));
  75. // Refining the camera matrix using parameters obtained by calibration
  76. new_camera_matrix = cv::getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0);
  77. // Method 1 to undistort the image
  78. cv::undistort( frame, dst, new_camera_matrix, distCoeffs, new_camera_matrix );
  79. // Method 2 to undistort the image
  80. cv::initUndistortRectifyMap(cameraMatrix, distCoeffs, cv::Mat(),cv::getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, imageSize, 0),imageSize, CV_16SC2, map1, map2);
  81. cv::remap(frame, dst, map1, map2, cv::INTER_LINEAR);
  82. //Displaying the undistorted image
  83. cv::imshow("undistorted image",dst);
  84. cv::waitKey(0);
  85. return 0;
  86. }