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