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 | 460c9fb | 2022-02-12 18:14:50 -0800 | [diff] [blame^] | 31 | H_camera_board_(Eigen::Affine3d()), |
| 32 | prev_H_camera_board_(Eigen::Affine3d()), |
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 | |
Yash Chainani | 460c9fb | 2022-02-12 18:14:50 -0800 | [diff] [blame^] | 70 | Eigen::Affine3d H_board_camera = |
| 71 | Eigen::Translation3d(tvec_eigen) * |
| 72 | Eigen::AngleAxisd(rvec_eigen.norm(), rvec_eigen / rvec_eigen.norm()); |
| 73 | H_camera_board_ = H_board_camera.inverse(); |
| 74 | Eigen::Affine3d H_delta = H_board_camera * prev_H_camera_board_; |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 75 | |
Yash Chainani | 460c9fb | 2022-02-12 18:14:50 -0800 | [diff] [blame^] | 76 | Eigen::AngleAxisd delta_r = Eigen::AngleAxisd(H_delta.rotation()); |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 77 | |
Yash Chainani | 460c9fb | 2022-02-12 18:14:50 -0800 | [diff] [blame^] | 78 | Eigen::Vector3d delta_t = H_delta.translation(); |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 79 | |
| 80 | double r_norm = std::abs(delta_r.angle()); |
| 81 | double t_norm = delta_t.norm(); |
| 82 | |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 83 | int keystroke = cv::waitKey(1); |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 84 | if (r_norm > kDeltaRThreshold || t_norm > kDeltaTThreshold) { |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 85 | if (valid) { |
Yash Chainani | 460c9fb | 2022-02-12 18:14:50 -0800 | [diff] [blame^] | 86 | prev_H_camera_board_ = H_camera_board_; |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 87 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 88 | all_charuco_ids_.emplace_back(std::move(charuco_ids)); |
| 89 | all_charuco_corners_.emplace_back(std::move(charuco_corners)); |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 90 | |
| 91 | if (r_norm > kDeltaRThreshold) { |
| 92 | LOG(INFO) << "Triggered by rotation delta = " << r_norm << " > " |
| 93 | << kDeltaRThreshold; |
| 94 | } |
| 95 | if (t_norm > kDeltaTThreshold) { |
| 96 | LOG(INFO) << "Trigerred by translation delta = " << t_norm << " > " |
| 97 | << kDeltaTThreshold; |
| 98 | } |
| 99 | |
| 100 | LOG(INFO) << "Image count " << all_charuco_corners_.size(); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 101 | } |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 102 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 103 | if (all_charuco_ids_.size() >= 50) { |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 104 | LOG(INFO) << "Got enough images to calibrate"; |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 105 | event_loop_->Exit(); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 106 | } |
| 107 | } else if ((keystroke & 0xFF) == static_cast<int>('q')) { |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 108 | event_loop_->Exit(); |
Ravago Jones | cf453ab | 2020-05-06 21:14:53 -0700 | [diff] [blame] | 109 | } |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | void MaybeCalibrate() { |
| 113 | if (all_charuco_ids_.size() >= 50) { |
| 114 | cv::Mat cameraMatrix, distCoeffs; |
| 115 | std::vector<cv::Mat> rvecs, tvecs; |
| 116 | cv::Mat stdDeviationsIntrinsics, stdDeviationsExtrinsics, perViewErrors; |
| 117 | |
| 118 | // Set calibration flags (same as in calibrateCamera() function) |
| 119 | int calibration_flags = 0; |
| 120 | cv::Size img_size(640, 480); |
| 121 | const double reprojection_error = cv::aruco::calibrateCameraCharuco( |
| 122 | all_charuco_corners_, all_charuco_ids_, charuco_extractor_.board(), |
| 123 | img_size, cameraMatrix, distCoeffs, rvecs, tvecs, |
| 124 | stdDeviationsIntrinsics, stdDeviationsExtrinsics, perViewErrors, |
| 125 | calibration_flags); |
| 126 | CHECK_LE(reprojection_error, 1.0) << ": Reproduction error is bad."; |
| 127 | LOG(INFO) << "Reprojection Error is " << reprojection_error; |
| 128 | |
| 129 | flatbuffers::FlatBufferBuilder fbb; |
| 130 | flatbuffers::Offset<flatbuffers::String> name_offset = |
| 131 | fbb.CreateString(absl::StrFormat("pi%d", pi_number_.value())); |
| 132 | flatbuffers::Offset<flatbuffers::Vector<float>> intrinsics_offset = |
| 133 | fbb.CreateVector<float>(9u, [&cameraMatrix](size_t i) { |
| 134 | return static_cast<float>( |
| 135 | reinterpret_cast<double *>(cameraMatrix.data)[i]); |
| 136 | }); |
| 137 | |
| 138 | flatbuffers::Offset<flatbuffers::Vector<float>> |
| 139 | distortion_coefficients_offset = |
| 140 | fbb.CreateVector<float>(5u, [&distCoeffs](size_t i) { |
| 141 | return static_cast<float>( |
| 142 | reinterpret_cast<double *>(distCoeffs.data)[i]); |
| 143 | }); |
| 144 | |
| 145 | sift::CameraCalibration::Builder camera_calibration_builder(fbb); |
| 146 | std::optional<uint16_t> team_number = |
| 147 | aos::network::team_number_internal::ParsePiTeamNumber(pi_); |
| 148 | CHECK(team_number) << ": Invalid pi hostname " << pi_ |
| 149 | << ", failed to parse team number"; |
| 150 | |
| 151 | const aos::realtime_clock::time_point realtime_now = |
| 152 | aos::realtime_clock::now(); |
| 153 | camera_calibration_builder.add_node_name(name_offset); |
| 154 | camera_calibration_builder.add_team_number(team_number.value()); |
| 155 | camera_calibration_builder.add_calibration_timestamp( |
| 156 | realtime_now.time_since_epoch().count()); |
| 157 | camera_calibration_builder.add_intrinsics(intrinsics_offset); |
| 158 | camera_calibration_builder.add_dist_coeffs( |
| 159 | distortion_coefficients_offset); |
| 160 | fbb.Finish(camera_calibration_builder.Finish()); |
| 161 | |
| 162 | aos::FlatbufferDetachedBuffer<sift::CameraCalibration> camera_calibration( |
| 163 | fbb.Release()); |
| 164 | std::stringstream time_ss; |
| 165 | time_ss << realtime_now; |
| 166 | |
| 167 | const std::string calibration_filename = |
| 168 | FLAGS_calibration_folder + |
| 169 | absl::StrFormat("/calibration_pi-%d-%d_%s.json", team_number.value(), |
| 170 | pi_number_.value(), time_ss.str()); |
| 171 | |
| 172 | LOG(INFO) << calibration_filename << " -> " |
| 173 | << aos::FlatbufferToJson(camera_calibration, |
| 174 | {.multi_line = true}); |
| 175 | |
| 176 | aos::util::WriteStringToFileOrDie( |
| 177 | calibration_filename, |
| 178 | aos::FlatbufferToJson(camera_calibration, {.multi_line = true})); |
| 179 | } else { |
| 180 | LOG(INFO) << "Skipping calibration due to not enough images."; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | private: |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 185 | static constexpr double kDeltaRThreshold = M_PI / 6.0; |
| 186 | static constexpr double kDeltaTThreshold = 0.3; |
| 187 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 188 | aos::ShmEventLoop *event_loop_; |
| 189 | std::string pi_; |
| 190 | const std::optional<uint16_t> pi_number_; |
| 191 | |
| 192 | std::vector<std::vector<int>> all_charuco_ids_; |
| 193 | std::vector<std::vector<cv::Point2f>> all_charuco_corners_; |
| 194 | |
Yash Chainani | 460c9fb | 2022-02-12 18:14:50 -0800 | [diff] [blame^] | 195 | Eigen::Affine3d H_camera_board_; |
| 196 | Eigen::Affine3d prev_H_camera_board_; |
Yash Chainani | 5c005da | 2022-01-22 16:51:11 -0800 | [diff] [blame] | 197 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 198 | CharucoExtractor charuco_extractor_; |
| 199 | }; |
| 200 | |
| 201 | namespace { |
| 202 | |
| 203 | void Main() { |
| 204 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 205 | aos::configuration::ReadConfig(FLAGS_config); |
| 206 | |
| 207 | aos::ShmEventLoop event_loop(&config.message()); |
| 208 | |
| 209 | Calibration extractor(&event_loop, FLAGS_pi); |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 210 | |
| 211 | event_loop.Run(); |
| 212 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 213 | extractor.MaybeCalibrate(); |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | } // namespace |
| 217 | } // namespace vision |
| 218 | } // namespace frc971 |
| 219 | |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 220 | int main(int argc, char **argv) { |
| 221 | aos::InitGoogle(&argc, &argv); |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 222 | frc971::vision::Main(); |
Austin Schuh | c1f118e | 2020-04-11 15:50:08 -0700 | [diff] [blame] | 223 | } |