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