aruco_samples_utility.hpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #include <opencv2/highgui.hpp>
  2. #include <opencv2/aruco.hpp>
  3. #include <opencv2/calib3d.hpp>
  4. #include <ctime>
  5. namespace {
  6. inline static bool readCameraParameters(std::string filename, cv::Mat &camMatrix, cv::Mat &distCoeffs) {
  7. cv::FileStorage fs(filename, cv::FileStorage::READ);
  8. if (!fs.isOpened())
  9. return false;
  10. fs["camera_matrix"] >> camMatrix;
  11. fs["distortion_coefficients"] >> distCoeffs;
  12. return true;
  13. }
  14. inline static bool saveCameraParams(const std::string &filename, cv::Size imageSize, float aspectRatio, int flags,
  15. const cv::Mat &cameraMatrix, const cv::Mat &distCoeffs, double totalAvgErr) {
  16. cv::FileStorage fs(filename, cv::FileStorage::WRITE);
  17. if (!fs.isOpened())
  18. return false;
  19. time_t tt;
  20. time(&tt);
  21. struct tm *t2 = localtime(&tt);
  22. char buf[1024];
  23. strftime(buf, sizeof(buf) - 1, "%c", t2);
  24. fs << "calibration_time" << buf;
  25. fs << "image_width" << imageSize.width;
  26. fs << "image_height" << imageSize.height;
  27. if (flags & cv::CALIB_FIX_ASPECT_RATIO) fs << "aspectRatio" << aspectRatio;
  28. if (flags != 0) {
  29. sprintf(buf, "flags: %s%s%s%s",
  30. flags & cv::CALIB_USE_INTRINSIC_GUESS ? "+use_intrinsic_guess" : "",
  31. flags & cv::CALIB_FIX_ASPECT_RATIO ? "+fix_aspectRatio" : "",
  32. flags & cv::CALIB_FIX_PRINCIPAL_POINT ? "+fix_principal_point" : "",
  33. flags & cv::CALIB_ZERO_TANGENT_DIST ? "+zero_tangent_dist" : "");
  34. }
  35. fs << "flags" << flags;
  36. fs << "camera_matrix" << cameraMatrix;
  37. fs << "distortion_coefficients" << distCoeffs;
  38. fs << "avg_reprojection_error" << totalAvgErr;
  39. return true;
  40. }
  41. }