Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 1 | #ifndef Y2020_VISION_CHARUCO_LIB_H_ |
| 2 | #define Y2020_VISION_CHARUCO_LIB_H_ |
| 3 | |
| 4 | #include <functional> |
Milind Upadhyay | c6e42ee | 2022-12-27 00:02:11 -0800 | [diff] [blame] | 5 | #include <string_view> |
| 6 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 7 | #include "Eigen/Dense" |
| 8 | #include "Eigen/Geometry" |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 9 | #include "absl/types/span.h" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 10 | #include "external/com_github_foxglove_schemas/ImageAnnotations_generated.h" |
| 11 | #include <opencv2/aruco/charuco.hpp> |
| 12 | #include <opencv2/calib3d.hpp> |
| 13 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 14 | #include "aos/events/event_loop.h" |
Austin Schuh | ea7b014 | 2021-10-08 22:04:53 -0700 | [diff] [blame] | 15 | #include "aos/network/message_bridge_server_generated.h" |
Jim Ostrowski | 814d281 | 2022-12-11 23:17:14 -0800 | [diff] [blame] | 16 | #include "frc971/vision/calibration_generated.h" |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 17 | |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame] | 18 | DECLARE_bool(visualize); |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame] | 19 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 20 | namespace frc971::vision { |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 21 | |
| 22 | // Class to find extrinsics for a specified pi's camera using the provided |
| 23 | // training data. |
| 24 | class CameraCalibration { |
| 25 | public: |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 26 | CameraCalibration(const calibration::CameraCalibration *calibration); |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 27 | |
| 28 | // Intrinsics for the located camera. |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 29 | cv::Mat CameraIntrinsics() const { return intrinsics_; } |
| 30 | Eigen::Matrix3d CameraIntrinsicsEigen() const { return intrinsics_eigen_; } |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 31 | |
| 32 | // Distortion coefficients for the located camera. |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 33 | cv::Mat CameraDistCoeffs() const { return dist_coeffs_; } |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 34 | |
| 35 | private: |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 36 | const cv::Mat intrinsics_; |
| 37 | const Eigen::Matrix3d intrinsics_eigen_; |
| 38 | const cv::Mat dist_coeffs_; |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 39 | }; |
| 40 | |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame] | 41 | // Helper class to call a function with a cv::Mat and age when an image shows up |
| 42 | // on the provided channel. This hides all the conversions and wrangling needed |
| 43 | // to view the image. |
| 44 | // Can connect this with HandleImage function from CharucoExtrator for |
| 45 | // full-service callback functionality |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 46 | class ImageCallback { |
| 47 | public: |
Austin Schuh | ac402e9 | 2023-01-08 13:56:20 -0800 | [diff] [blame] | 48 | enum class Format { |
| 49 | YUYV2 = 0, |
| 50 | BGR = 1, |
| 51 | GRAYSCALE = 2, |
| 52 | }; |
milind-u | 0cb5311 | 2023-02-03 20:32:55 -0800 | [diff] [blame] | 53 | |
| 54 | // `max_age` is the age to start dropping frames at |
| 55 | ImageCallback( |
| 56 | aos::EventLoop *event_loop, std::string_view channel, |
| 57 | std::function<void(cv::Mat, aos::monotonic_clock::time_point)> |
| 58 | &&handle_image_fn, |
| 59 | aos::monotonic_clock::duration max_age = std::chrono::milliseconds(100)); |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 60 | |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 61 | void set_format(Format format) { format_ = format; } |
Austin Schuh | ac402e9 | 2023-01-08 13:56:20 -0800 | [diff] [blame] | 62 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 63 | private: |
Austin Schuh | c341986 | 2023-01-08 13:54:36 -0800 | [diff] [blame] | 64 | void DisableTracing(); |
| 65 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 66 | aos::EventLoop *event_loop_; |
Austin Schuh | ea7b014 | 2021-10-08 22:04:53 -0700 | [diff] [blame] | 67 | aos::Fetcher<aos::message_bridge::ServerStatistics> server_fetcher_; |
| 68 | const aos::Node *source_node_; |
| 69 | std::function<void(cv::Mat, aos::monotonic_clock::time_point)> handle_image_; |
Austin Schuh | c341986 | 2023-01-08 13:54:36 -0800 | [diff] [blame] | 70 | aos::TimerHandler *timer_fn_; |
| 71 | |
| 72 | bool disabling_ = false; |
| 73 | |
| 74 | aos::Ftrace ftrace_; |
Austin Schuh | ac402e9 | 2023-01-08 13:56:20 -0800 | [diff] [blame] | 75 | |
| 76 | Format format_ = Format::BGR; |
milind-u | 0cb5311 | 2023-02-03 20:32:55 -0800 | [diff] [blame] | 77 | |
| 78 | aos::monotonic_clock::duration max_age_; |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
Milind Upadhyay | c6e42ee | 2022-12-27 00:02:11 -0800 | [diff] [blame] | 81 | // Types of targets that a CharucoExtractor can detect in images |
| 82 | enum class TargetType : uint8_t { |
milind-u | 09fb125 | 2023-01-28 19:21:41 -0800 | [diff] [blame] | 83 | kAruco = 0, |
| 84 | kCharuco = 1, |
Jim Ostrowski | 2be78e3 | 2023-03-25 11:57:54 -0700 | [diff] [blame] | 85 | kCharucoDiamond = 2, |
| 86 | kAprilTag = 3 |
Milind Upadhyay | c6e42ee | 2022-12-27 00:02:11 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
James Kuszmaul | d6199be | 2023-02-11 19:56:28 -0800 | [diff] [blame] | 89 | TargetType TargetTypeFromString(std::string_view str); |
| 90 | std::ostream &operator<<(std::ostream &os, TargetType target_type); |
| 91 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 92 | // Class which calls a callback each time an image arrives with the information |
| 93 | // extracted from it. |
| 94 | class CharucoExtractor { |
| 95 | public: |
Jim Ostrowski | 2f2685f | 2023-03-25 11:57:54 -0700 | [diff] [blame] | 96 | // Setting up a constructor that doesn't require an event_loop, so we can call |
| 97 | // and get results back from ProcessImage directly |
| 98 | CharucoExtractor(const calibration::CameraCalibration *calibration, |
| 99 | const TargetType target_type); |
| 100 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 101 | // The callback takes the following arguments: |
| 102 | // cv::Mat -> image with overlays drawn on it. |
Austin Schuh | ea7b014 | 2021-10-08 22:04:53 -0700 | [diff] [blame] | 103 | // monotonic_clock::time_point -> Time on this node when this image was |
| 104 | // captured. |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame] | 105 | // std::vector<Vec4i> -> target ids (aruco/april in first slot of Vec4i) |
| 106 | // NOTE: We use Vec4i since that stores the ids for the charuco diamond target |
| 107 | // std::vector<std::vector<cv::Point2f>> -> charuco_corners |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 108 | // bool -> true if rvec/tvec is valid. |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame] | 109 | // std::vector<Eigen::Vector3d> -> rvec |
| 110 | // std::vector<Eigen::Vector3d> -> tvec |
| 111 | // NOTE: we return as a vector since all but charuco boards could have |
| 112 | // multiple targets in an image; for charuco boards, there should be just one |
| 113 | // element |
Austin Schuh | ea7b014 | 2021-10-08 22:04:53 -0700 | [diff] [blame] | 114 | CharucoExtractor( |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 115 | aos::EventLoop *event_loop, |
| 116 | const calibration::CameraCalibration *calibration, TargetType target_type, |
Milind Upadhyay | c6e42ee | 2022-12-27 00:02:11 -0800 | [diff] [blame] | 117 | std::string_view image_channel, |
Austin Schuh | ea7b014 | 2021-10-08 22:04:53 -0700 | [diff] [blame] | 118 | std::function<void(cv::Mat, aos::monotonic_clock::time_point, |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame] | 119 | std::vector<cv::Vec4i>, |
| 120 | std::vector<std::vector<cv::Point2f>>, bool, |
| 121 | std::vector<Eigen::Vector3d>, |
| 122 | std::vector<Eigen::Vector3d>)> &&handle_charuco_fn); |
| 123 | |
| 124 | // Handles the image by detecting the charuco board in it. |
Jim Ostrowski | 2f2685f | 2023-03-25 11:57:54 -0700 | [diff] [blame] | 125 | void HandleImage(cv::Mat rgb_image, |
| 126 | const aos::monotonic_clock::time_point eof); |
| 127 | |
| 128 | void ProcessImage(cv::Mat rgb_image, |
| 129 | const aos::monotonic_clock::time_point eof, |
| 130 | const aos::monotonic_clock::time_point current_time, |
| 131 | std::vector<cv::Vec4i> &result_ids, |
| 132 | std::vector<std::vector<cv::Point2f>> &result_corners, |
| 133 | bool &valid, std::vector<Eigen::Vector3d> &rvecs_eigen, |
| 134 | std::vector<Eigen::Vector3d> &tvecs_eigen); |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 135 | |
| 136 | // Returns the aruco dictionary in use. |
| 137 | cv::Ptr<cv::aruco::Dictionary> dictionary() const { return dictionary_; } |
| 138 | // Returns the aruco board in use. |
| 139 | cv::Ptr<cv::aruco::CharucoBoard> board() const { return board_; } |
| 140 | |
| 141 | // Returns the camera matrix for this camera. |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 142 | const cv::Mat camera_matrix() const { |
| 143 | return calibration_.CameraIntrinsics(); |
| 144 | } |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 145 | // Returns the distortion coefficients for this camera. |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 146 | const cv::Mat dist_coeffs() const { return calibration_.CameraDistCoeffs(); } |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 147 | |
| 148 | private: |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame] | 149 | // Creates the dictionary, board, and other parameters for the appropriate |
| 150 | // (ch)aruco target |
| 151 | void SetupTargetData(); |
| 152 | |
| 153 | // Draw the axes from the pose(s) on the image |
| 154 | void DrawTargetPoses(cv::Mat rgb_image, std::vector<cv::Vec3d> rvecs, |
| 155 | std::vector<cv::Vec3d> tvecs); |
| 156 | |
Jim Ostrowski | 2f2685f | 2023-03-25 11:57:54 -0700 | [diff] [blame] | 157 | // Helper function to convert rotation (rvecs) and translation (tvecs) |
| 158 | // vectors into Eigen vectors and store in corresponding vectors |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame] | 159 | void PackPoseResults(std::vector<cv::Vec3d> &rvecs, |
| 160 | std::vector<cv::Vec3d> &tvecs, |
| 161 | std::vector<Eigen::Vector3d> *rvecs_eigen, |
| 162 | std::vector<Eigen::Vector3d> *tvecs_eigen); |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 163 | |
Austin Schuh | ea7b014 | 2021-10-08 22:04:53 -0700 | [diff] [blame] | 164 | aos::EventLoop *event_loop_; |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 165 | |
| 166 | cv::Ptr<cv::aruco::Dictionary> dictionary_; |
| 167 | cv::Ptr<cv::aruco::CharucoBoard> board_; |
| 168 | |
Milind Upadhyay | c6e42ee | 2022-12-27 00:02:11 -0800 | [diff] [blame] | 169 | // Type of targets to detect |
| 170 | TargetType target_type_; |
| 171 | // Channel to listen on for images |
| 172 | std::string_view image_channel_; |
| 173 | |
| 174 | // Length of a side of the target marker |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame] | 175 | double marker_length_; |
| 176 | // Length of a side of the checkerboard squares (around the marker) |
| 177 | double square_length_; |
| 178 | |
James Kuszmaul | 7e95881 | 2023-02-11 15:34:31 -0800 | [diff] [blame] | 179 | CameraCalibration calibration_; |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 180 | |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 181 | // Function to call. |
Jim Ostrowski | b3cab97 | 2022-12-03 15:47:00 -0800 | [diff] [blame] | 182 | std::function<void( |
| 183 | cv::Mat, aos::monotonic_clock::time_point, std::vector<cv::Vec4i>, |
| 184 | std::vector<std::vector<cv::Point2f>>, bool, std::vector<Eigen::Vector3d>, |
| 185 | std::vector<Eigen::Vector3d>)> |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 186 | handle_charuco_; |
| 187 | }; |
| 188 | |
James Kuszmaul | 969e4ab | 2023-01-28 16:09:19 -0800 | [diff] [blame] | 189 | // Puts the provided charuco corners into a foxglove ImageAnnotation type for |
| 190 | // visualization purposes. |
| 191 | flatbuffers::Offset<foxglove::ImageAnnotations> BuildAnnotations( |
Jim Ostrowski | 5e2c5e6 | 2023-02-26 12:52:56 -0800 | [diff] [blame] | 192 | flatbuffers::FlatBufferBuilder *fbb, |
James Kuszmaul | 969e4ab | 2023-01-28 16:09:19 -0800 | [diff] [blame] | 193 | const aos::monotonic_clock::time_point monotonic_now, |
Jim Ostrowski | 5e2c5e6 | 2023-02-26 12:52:56 -0800 | [diff] [blame] | 194 | const std::vector<std::vector<cv::Point2f>> &corners, |
| 195 | const std::vector<double> rgba_color = std::vector<double>{0.0, 1.0, 0.0, |
| 196 | 1.0}, |
| 197 | const double thickness = 5, |
| 198 | const foxglove::PointsAnnotationType line_type = |
| 199 | foxglove::PointsAnnotationType::POINTS); |
| 200 | |
Jim Ostrowski | 2f2685f | 2023-03-25 11:57:54 -0700 | [diff] [blame] | 201 | // Creates a PointsAnnotation to build up ImageAnnotations with different |
| 202 | // types |
Jim Ostrowski | 5e2c5e6 | 2023-02-26 12:52:56 -0800 | [diff] [blame] | 203 | flatbuffers::Offset<foxglove::PointsAnnotation> BuildPointsAnnotation( |
| 204 | flatbuffers::FlatBufferBuilder *fbb, |
| 205 | const aos::monotonic_clock::time_point monotonic_now, |
| 206 | const std::vector<cv::Point2f> &corners, |
| 207 | const std::vector<double> rgba_color = std::vector<double>{0.0, 1.0, 0.0, |
| 208 | 1.0}, |
| 209 | const double thickness = 5, |
| 210 | const foxglove::PointsAnnotationType line_type = |
| 211 | foxglove::PointsAnnotationType::POINTS); |
James Kuszmaul | 969e4ab | 2023-01-28 16:09:19 -0800 | [diff] [blame] | 212 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 213 | } // namespace frc971::vision |
Austin Schuh | 25837f2 | 2021-06-27 15:49:14 -0700 | [diff] [blame] | 214 | |
| 215 | #endif // Y2020_VISION_CHARUCO_LIB_H_ |