blob: 2c35a0bf6e9207c680a7e81607385c7cae8fc845 [file] [log] [blame]
Austin Schuh25837f22021-06-27 15:49:14 -07001#ifndef Y2020_VISION_CHARUCO_LIB_H_
2#define Y2020_VISION_CHARUCO_LIB_H_
3
4#include <functional>
Austin Schuh25837f22021-06-27 15:49:14 -07005#include <opencv2/aruco/charuco.hpp>
6#include <opencv2/calib3d.hpp>
Milind Upadhyayc6e42ee2022-12-27 00:02:11 -08007#include <string_view>
8
Austin Schuh25837f22021-06-27 15:49:14 -07009#include "Eigen/Dense"
10#include "Eigen/Geometry"
Austin Schuh25837f22021-06-27 15:49:14 -070011#include "absl/types/span.h"
12#include "aos/events/event_loop.h"
Austin Schuhea7b0142021-10-08 22:04:53 -070013#include "aos/network/message_bridge_server_generated.h"
James Kuszmaul969e4ab2023-01-28 16:09:19 -080014#include "external/com_github_foxglove_schemas/ImageAnnotations_generated.h"
Jim Ostrowski814d2812022-12-11 23:17:14 -080015#include "frc971/vision/calibration_generated.h"
Austin Schuh25837f22021-06-27 15:49:14 -070016
Jim Ostrowskib3cab972022-12-03 15:47:00 -080017DECLARE_bool(visualize);
Jim Ostrowskib3cab972022-12-03 15:47:00 -080018
Austin Schuh25837f22021-06-27 15:49:14 -070019namespace frc971 {
20namespace vision {
21
22// Class to find extrinsics for a specified pi's camera using the provided
23// training data.
24class CameraCalibration {
25 public:
James Kuszmaul7e958812023-02-11 15:34:31 -080026 CameraCalibration(const calibration::CameraCalibration *calibration);
Austin Schuh25837f22021-06-27 15:49:14 -070027
28 // Intrinsics for the located camera.
James Kuszmaul7e958812023-02-11 15:34:31 -080029 cv::Mat CameraIntrinsics() const { return intrinsics_; }
30 Eigen::Matrix3d CameraIntrinsicsEigen() const { return intrinsics_eigen_; }
Austin Schuh25837f22021-06-27 15:49:14 -070031
32 // Distortion coefficients for the located camera.
James Kuszmaul7e958812023-02-11 15:34:31 -080033 cv::Mat CameraDistCoeffs() const { return dist_coeffs_; }
Austin Schuh25837f22021-06-27 15:49:14 -070034
35 private:
James Kuszmaul7e958812023-02-11 15:34:31 -080036 const cv::Mat intrinsics_;
37 const Eigen::Matrix3d intrinsics_eigen_;
38 const cv::Mat dist_coeffs_;
Austin Schuh25837f22021-06-27 15:49:14 -070039};
40
Jim Ostrowskib3cab972022-12-03 15:47:00 -080041// 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 Schuh25837f22021-06-27 15:49:14 -070046class ImageCallback {
47 public:
Austin Schuhac402e92023-01-08 13:56:20 -080048 enum class Format {
49 YUYV2 = 0,
50 BGR = 1,
51 GRAYSCALE = 2,
52 };
milind-u0cb53112023-02-03 20:32:55 -080053
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 Schuh25837f22021-06-27 15:49:14 -070060
milind-u09fb1252023-01-28 19:21:41 -080061 void set_format(Format format) { format_ = format; }
Austin Schuhac402e92023-01-08 13:56:20 -080062
Austin Schuh25837f22021-06-27 15:49:14 -070063 private:
Austin Schuhc3419862023-01-08 13:54:36 -080064 void DisableTracing();
65
Austin Schuh25837f22021-06-27 15:49:14 -070066 aos::EventLoop *event_loop_;
Austin Schuhea7b0142021-10-08 22:04:53 -070067 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 Schuhc3419862023-01-08 13:54:36 -080070 aos::TimerHandler *timer_fn_;
71
72 bool disabling_ = false;
73
74 aos::Ftrace ftrace_;
Austin Schuhac402e92023-01-08 13:56:20 -080075
76 Format format_ = Format::BGR;
milind-u0cb53112023-02-03 20:32:55 -080077
78 aos::monotonic_clock::duration max_age_;
Austin Schuh25837f22021-06-27 15:49:14 -070079};
80
Milind Upadhyayc6e42ee2022-12-27 00:02:11 -080081// Types of targets that a CharucoExtractor can detect in images
82enum class TargetType : uint8_t {
milind-u09fb1252023-01-28 19:21:41 -080083 kAruco = 0,
84 kCharuco = 1,
85 kCharucoDiamond = 2
Milind Upadhyayc6e42ee2022-12-27 00:02:11 -080086};
87
James Kuszmauld6199be2023-02-11 19:56:28 -080088TargetType TargetTypeFromString(std::string_view str);
89std::ostream &operator<<(std::ostream &os, TargetType target_type);
90
Austin Schuh25837f22021-06-27 15:49:14 -070091// Class which calls a callback each time an image arrives with the information
92// extracted from it.
93class CharucoExtractor {
94 public:
95 // The callback takes the following arguments:
96 // cv::Mat -> image with overlays drawn on it.
Austin Schuhea7b0142021-10-08 22:04:53 -070097 // monotonic_clock::time_point -> Time on this node when this image was
98 // captured.
Jim Ostrowskib3cab972022-12-03 15:47:00 -080099 // std::vector<Vec4i> -> target ids (aruco/april in first slot of Vec4i)
100 // NOTE: We use Vec4i since that stores the ids for the charuco diamond target
101 // std::vector<std::vector<cv::Point2f>> -> charuco_corners
Austin Schuh25837f22021-06-27 15:49:14 -0700102 // bool -> true if rvec/tvec is valid.
Jim Ostrowskib3cab972022-12-03 15:47:00 -0800103 // std::vector<Eigen::Vector3d> -> rvec
104 // std::vector<Eigen::Vector3d> -> tvec
105 // NOTE: we return as a vector since all but charuco boards could have
106 // multiple targets in an image; for charuco boards, there should be just one
107 // element
Austin Schuhea7b0142021-10-08 22:04:53 -0700108 CharucoExtractor(
James Kuszmaul7e958812023-02-11 15:34:31 -0800109 aos::EventLoop *event_loop,
110 const calibration::CameraCalibration *calibration, TargetType target_type,
Milind Upadhyayc6e42ee2022-12-27 00:02:11 -0800111 std::string_view image_channel,
Austin Schuhea7b0142021-10-08 22:04:53 -0700112 std::function<void(cv::Mat, aos::monotonic_clock::time_point,
Jim Ostrowskib3cab972022-12-03 15:47:00 -0800113 std::vector<cv::Vec4i>,
114 std::vector<std::vector<cv::Point2f>>, bool,
115 std::vector<Eigen::Vector3d>,
116 std::vector<Eigen::Vector3d>)> &&handle_charuco_fn);
117
118 // Handles the image by detecting the charuco board in it.
119 void HandleImage(cv::Mat rgb_image, aos::monotonic_clock::time_point eof);
Austin Schuh25837f22021-06-27 15:49:14 -0700120
121 // Returns the aruco dictionary in use.
122 cv::Ptr<cv::aruco::Dictionary> dictionary() const { return dictionary_; }
123 // Returns the aruco board in use.
124 cv::Ptr<cv::aruco::CharucoBoard> board() const { return board_; }
125
126 // Returns the camera matrix for this camera.
James Kuszmaul7e958812023-02-11 15:34:31 -0800127 const cv::Mat camera_matrix() const {
128 return calibration_.CameraIntrinsics();
129 }
Austin Schuh25837f22021-06-27 15:49:14 -0700130 // Returns the distortion coefficients for this camera.
James Kuszmaul7e958812023-02-11 15:34:31 -0800131 const cv::Mat dist_coeffs() const { return calibration_.CameraDistCoeffs(); }
Austin Schuh25837f22021-06-27 15:49:14 -0700132
133 private:
Jim Ostrowskib3cab972022-12-03 15:47:00 -0800134 // Creates the dictionary, board, and other parameters for the appropriate
135 // (ch)aruco target
136 void SetupTargetData();
137
138 // Draw the axes from the pose(s) on the image
139 void DrawTargetPoses(cv::Mat rgb_image, std::vector<cv::Vec3d> rvecs,
140 std::vector<cv::Vec3d> tvecs);
141
142 // Helper function to convert rotation (rvecs) and translation (tvecs) vectors
143 // into Eigen vectors and store in corresponding vectors
144 void PackPoseResults(std::vector<cv::Vec3d> &rvecs,
145 std::vector<cv::Vec3d> &tvecs,
146 std::vector<Eigen::Vector3d> *rvecs_eigen,
147 std::vector<Eigen::Vector3d> *tvecs_eigen);
Austin Schuh25837f22021-06-27 15:49:14 -0700148
Austin Schuhea7b0142021-10-08 22:04:53 -0700149 aos::EventLoop *event_loop_;
Austin Schuh25837f22021-06-27 15:49:14 -0700150
151 cv::Ptr<cv::aruco::Dictionary> dictionary_;
152 cv::Ptr<cv::aruco::CharucoBoard> board_;
153
Milind Upadhyayc6e42ee2022-12-27 00:02:11 -0800154 // Type of targets to detect
155 TargetType target_type_;
156 // Channel to listen on for images
157 std::string_view image_channel_;
158
159 // Length of a side of the target marker
Jim Ostrowskib3cab972022-12-03 15:47:00 -0800160 double marker_length_;
161 // Length of a side of the checkerboard squares (around the marker)
162 double square_length_;
163
James Kuszmaul7e958812023-02-11 15:34:31 -0800164 CameraCalibration calibration_;
Austin Schuh25837f22021-06-27 15:49:14 -0700165
Austin Schuh25837f22021-06-27 15:49:14 -0700166 // Function to call.
Jim Ostrowskib3cab972022-12-03 15:47:00 -0800167 std::function<void(
168 cv::Mat, aos::monotonic_clock::time_point, std::vector<cv::Vec4i>,
169 std::vector<std::vector<cv::Point2f>>, bool, std::vector<Eigen::Vector3d>,
170 std::vector<Eigen::Vector3d>)>
Austin Schuh25837f22021-06-27 15:49:14 -0700171 handle_charuco_;
172};
173
James Kuszmaul969e4ab2023-01-28 16:09:19 -0800174// Puts the provided charuco corners into a foxglove ImageAnnotation type for
175// visualization purposes.
176flatbuffers::Offset<foxglove::ImageAnnotations> BuildAnnotations(
Jim Ostrowski5e2c5e62023-02-26 12:52:56 -0800177 flatbuffers::FlatBufferBuilder *fbb,
James Kuszmaul969e4ab2023-01-28 16:09:19 -0800178 const aos::monotonic_clock::time_point monotonic_now,
Jim Ostrowski5e2c5e62023-02-26 12:52:56 -0800179 const std::vector<std::vector<cv::Point2f>> &corners,
180 const std::vector<double> rgba_color = std::vector<double>{0.0, 1.0, 0.0,
181 1.0},
182 const double thickness = 5,
183 const foxglove::PointsAnnotationType line_type =
184 foxglove::PointsAnnotationType::POINTS);
185
186// Creates a PointsAnnotation to build up ImageAnnotations with different types
187flatbuffers::Offset<foxglove::PointsAnnotation> BuildPointsAnnotation(
188 flatbuffers::FlatBufferBuilder *fbb,
189 const aos::monotonic_clock::time_point monotonic_now,
190 const std::vector<cv::Point2f> &corners,
191 const std::vector<double> rgba_color = std::vector<double>{0.0, 1.0, 0.0,
192 1.0},
193 const double thickness = 5,
194 const foxglove::PointsAnnotationType line_type =
195 foxglove::PointsAnnotationType::POINTS);
James Kuszmaul969e4ab2023-01-28 16:09:19 -0800196
Austin Schuh25837f22021-06-27 15:49:14 -0700197} // namespace vision
198} // namespace frc971
199
200#endif // Y2020_VISION_CHARUCO_LIB_H_