Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 1 | #include <cmath> |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 2 | #include <opencv2/calib3d.hpp> |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 3 | #include <opencv2/highgui/highgui.hpp> |
| 4 | #include <opencv2/imgproc.hpp> |
| 5 | |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 6 | #include "Eigen/Dense" |
| 7 | #include "Eigen/Geometry" |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 8 | #include "absl/strings/str_format.h" |
| 9 | #include "aos/events/shm_event_loop.h" |
| 10 | #include "aos/init.h" |
| 11 | #include "aos/network/team_number.h" |
| 12 | #include "aos/time/time.h" |
| 13 | #include "aos/util/file.h" |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 14 | #include "y2020/vision/charuco_lib.h" |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 15 | |
| 16 | DEFINE_string(config, "config.json", "Path to the config file to use."); |
| 17 | DEFINE_string(pi, "pi-7971-1", "Pi name to calibrate."); |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 18 | DEFINE_string(calibration_folder, ".", "Folder to place calibration files."); |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 19 | DEFINE_bool(display_undistorted, false, |
| 20 | "If true, display the undistorted image."); |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 21 | |
| 22 | namespace frc971 { |
| 23 | namespace vision { |
| 24 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 25 | class Calibration { |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 26 | public: |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 27 | Calibration(aos::ShmEventLoop *event_loop, std::string_view pi) |
| 28 | : event_loop_(event_loop), |
| 29 | pi_(pi), |
| 30 | pi_number_(aos::network::ParsePiNumber(pi)), |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 31 | prev_rvec_(Eigen::Vector3d::Zero()), |
| 32 | prev_tvec_(Eigen::Vector3d::Zero()), |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 33 | charuco_extractor_( |
| 34 | event_loop, pi, |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 35 | [this](cv::Mat rgb_image, |
| 36 | const aos::monotonic_clock::time_point eof, |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 37 | std::vector<int> charuco_ids, |
| 38 | std::vector<cv::Point2f> charuco_corners, bool valid, |
| 39 | Eigen::Vector3d rvec_eigen, Eigen::Vector3d tvec_eigen) { |
Austin Schuh | ea7b014 | 2021-10-08 22:04:53 -0700 | [diff] [blame] | 40 | HandleCharuco(rgb_image, eof, charuco_ids, charuco_corners, |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 41 | valid, rvec_eigen, tvec_eigen); |
| 42 | }) { |
| 43 | CHECK(pi_number_) << ": Invalid pi number " << pi |
| 44 | << ", failed to parse pi number"; |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Austin Schuh | ea7b014 | 2021-10-08 22:04:53 -0700 | [diff] [blame] | 47 | void HandleCharuco(cv::Mat rgb_image, |
| 48 | const aos::monotonic_clock::time_point /*eof*/, |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 49 | std::vector<int> charuco_ids, |
| 50 | std::vector<cv::Point2f> charuco_corners, bool valid, |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 51 | Eigen::Vector3d rvec_eigen, Eigen::Vector3d tvec_eigen) { |
| 52 | // Reduce resolution displayed on remote viewer to prevent lag |
| 53 | cv::resize(rgb_image, rgb_image, |
| 54 | cv::Size(rgb_image.cols / 2, rgb_image.rows / 2)); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 55 | cv::imshow("Display", rgb_image); |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 56 | |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 57 | if (FLAGS_display_undistorted) { |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 58 | const cv::Size image_size(rgb_image.cols, rgb_image.rows); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 59 | cv::Mat undistorted_rgb_image(image_size, CV_8UC3); |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 60 | cv::undistort(rgb_image, undistorted_rgb_image, |
| 61 | charuco_extractor_.camera_matrix(), |
| 62 | charuco_extractor_.dist_coeffs()); |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 63 | |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 64 | cv::imshow("Display undist", undistorted_rgb_image); |
| 65 | } |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 66 | |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 67 | // Calibration calculates rotation and translation delta from last image |
| 68 | // captured to automatically capture next image |
| 69 | |
| 70 | Eigen::Matrix3d r_aff = |
| 71 | Eigen::AngleAxisd(rvec_eigen.norm(), rvec_eigen / rvec_eigen.norm()) |
| 72 | .toRotationMatrix(); |
| 73 | Eigen::Matrix3d prev_r_aff = |
| 74 | Eigen::AngleAxisd(prev_rvec_.norm(), prev_rvec_ / prev_rvec_.norm()) |
| 75 | .toRotationMatrix(); |
| 76 | |
| 77 | Eigen::AngleAxisd delta_r = Eigen::AngleAxisd(r_aff * prev_r_aff.inverse()); |
| 78 | |
| 79 | Eigen::Vector3d delta_t = tvec_eigen - prev_tvec_; |
| 80 | |
| 81 | double r_norm = std::abs(delta_r.angle()); |
| 82 | double t_norm = delta_t.norm(); |
| 83 | |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 84 | int keystroke = cv::waitKey(1); |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 85 | if (r_norm > kDeltaRThreshold || t_norm > kDeltaTThreshold) { |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 86 | if (valid) { |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 87 | prev_rvec_ = rvec_eigen; |
| 88 | prev_tvec_ = tvec_eigen; |
| 89 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 90 | all_charuco_ids_.emplace_back(std::move(charuco_ids)); |
| 91 | all_charuco_corners_.emplace_back(std::move(charuco_corners)); |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 92 | |
| 93 | if (r_norm > kDeltaRThreshold) { |
| 94 | LOG(INFO) << "Triggered by rotation delta = " << r_norm << " > " |
| 95 | << kDeltaRThreshold; |
| 96 | } |
| 97 | if (t_norm > kDeltaTThreshold) { |
| 98 | LOG(INFO) << "Trigerred by translation delta = " << t_norm << " > " |
| 99 | << kDeltaTThreshold; |
| 100 | } |
| 101 | |
| 102 | LOG(INFO) << "Image count " << all_charuco_corners_.size(); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 103 | } |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 104 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 105 | if (all_charuco_ids_.size() >= 50) { |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 106 | LOG(INFO) << "Got enough images to calibrate"; |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 107 | event_loop_->Exit(); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 108 | } |
| 109 | } else if ((keystroke & 0xFF) == static_cast<int>('q')) { |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 110 | event_loop_->Exit(); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 111 | } |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void MaybeCalibrate() { |
| 115 | if (all_charuco_ids_.size() >= 50) { |
| 116 | cv::Mat cameraMatrix, distCoeffs; |
| 117 | std::vector<cv::Mat> rvecs, tvecs; |
| 118 | cv::Mat stdDeviationsIntrinsics, stdDeviationsExtrinsics, perViewErrors; |
| 119 | |
| 120 | // Set calibration flags (same as in calibrateCamera() function) |
| 121 | int calibration_flags = 0; |
| 122 | cv::Size img_size(640, 480); |
| 123 | const double reprojection_error = cv::aruco::calibrateCameraCharuco( |
| 124 | all_charuco_corners_, all_charuco_ids_, charuco_extractor_.board(), |
| 125 | img_size, cameraMatrix, distCoeffs, rvecs, tvecs, |
| 126 | stdDeviationsIntrinsics, stdDeviationsExtrinsics, perViewErrors, |
| 127 | calibration_flags); |
| 128 | CHECK_LE(reprojection_error, 1.0) << ": Reproduction error is bad."; |
| 129 | LOG(INFO) << "Reprojection Error is " << reprojection_error; |
| 130 | |
| 131 | flatbuffers::FlatBufferBuilder fbb; |
| 132 | flatbuffers::Offset<flatbuffers::String> name_offset = |
| 133 | fbb.CreateString(absl::StrFormat("pi%d", pi_number_.value())); |
| 134 | flatbuffers::Offset<flatbuffers::Vector<float>> intrinsics_offset = |
| 135 | fbb.CreateVector<float>(9u, [&cameraMatrix](size_t i) { |
| 136 | return static_cast<float>( |
| 137 | reinterpret_cast<double *>(cameraMatrix.data)[i]); |
| 138 | }); |
| 139 | |
| 140 | flatbuffers::Offset<flatbuffers::Vector<float>> |
| 141 | distortion_coefficients_offset = |
| 142 | fbb.CreateVector<float>(5u, [&distCoeffs](size_t i) { |
| 143 | return static_cast<float>( |
| 144 | reinterpret_cast<double *>(distCoeffs.data)[i]); |
| 145 | }); |
| 146 | |
| 147 | sift::CameraCalibration::Builder camera_calibration_builder(fbb); |
| 148 | std::optional<uint16_t> team_number = |
| 149 | aos::network::team_number_internal::ParsePiTeamNumber(pi_); |
| 150 | CHECK(team_number) << ": Invalid pi hostname " << pi_ |
| 151 | << ", failed to parse team number"; |
| 152 | |
| 153 | const aos::realtime_clock::time_point realtime_now = |
| 154 | aos::realtime_clock::now(); |
| 155 | camera_calibration_builder.add_node_name(name_offset); |
| 156 | camera_calibration_builder.add_team_number(team_number.value()); |
| 157 | camera_calibration_builder.add_calibration_timestamp( |
| 158 | realtime_now.time_since_epoch().count()); |
| 159 | camera_calibration_builder.add_intrinsics(intrinsics_offset); |
| 160 | camera_calibration_builder.add_dist_coeffs( |
| 161 | distortion_coefficients_offset); |
| 162 | fbb.Finish(camera_calibration_builder.Finish()); |
| 163 | |
| 164 | aos::FlatbufferDetachedBuffer<sift::CameraCalibration> camera_calibration( |
| 165 | fbb.Release()); |
| 166 | std::stringstream time_ss; |
| 167 | time_ss << realtime_now; |
| 168 | |
| 169 | const std::string calibration_filename = |
| 170 | FLAGS_calibration_folder + |
| 171 | absl::StrFormat("/calibration_pi-%d-%d_%s.json", team_number.value(), |
| 172 | pi_number_.value(), time_ss.str()); |
| 173 | |
| 174 | LOG(INFO) << calibration_filename << " -> " |
| 175 | << aos::FlatbufferToJson(camera_calibration, |
| 176 | {.multi_line = true}); |
| 177 | |
| 178 | aos::util::WriteStringToFileOrDie( |
| 179 | calibration_filename, |
| 180 | aos::FlatbufferToJson(camera_calibration, {.multi_line = true})); |
| 181 | } else { |
| 182 | LOG(INFO) << "Skipping calibration due to not enough images."; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private: |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 187 | static constexpr double kDeltaRThreshold = M_PI / 6.0; |
| 188 | static constexpr double kDeltaTThreshold = 0.3; |
| 189 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 190 | aos::ShmEventLoop *event_loop_; |
| 191 | std::string pi_; |
| 192 | const std::optional<uint16_t> pi_number_; |
| 193 | |
| 194 | std::vector<std::vector<int>> all_charuco_ids_; |
| 195 | std::vector<std::vector<cv::Point2f>> all_charuco_corners_; |
| 196 | |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 197 | Eigen::Vector3d prev_rvec_; |
| 198 | Eigen::Vector3d prev_tvec_; |
| 199 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 200 | CharucoExtractor charuco_extractor_; |
| 201 | }; |
| 202 | |
| 203 | namespace { |
| 204 | |
| 205 | void Main() { |
| 206 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 207 | aos::configuration::ReadConfig(FLAGS_config); |
| 208 | |
| 209 | aos::ShmEventLoop event_loop(&config.message()); |
| 210 | |
| 211 | Calibration extractor(&event_loop, FLAGS_pi); |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 212 | |
| 213 | event_loop.Run(); |
| 214 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 215 | extractor.MaybeCalibrate(); |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | } // namespace |
| 219 | } // namespace vision |
| 220 | } // namespace frc971 |
| 221 | |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 222 | int main(int argc, char **argv) { |
| 223 | aos::InitGoogle(&argc, &argv); |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 224 | frc971::vision::Main(); |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 225 | } |