blob: a54bfca11a88c24e9be0e261ae5a5588c697d4f2 [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>
5#include <string_view>
6
7#include <opencv2/aruco/charuco.hpp>
8#include <opencv2/calib3d.hpp>
9#include "Eigen/Dense"
10#include "Eigen/Geometry"
11
12#include "absl/types/span.h"
13#include "aos/events/event_loop.h"
Austin Schuhea7b0142021-10-08 22:04:53 -070014#include "aos/network/message_bridge_server_generated.h"
Austin Schuh25837f22021-06-27 15:49:14 -070015#include "y2020/vision/sift/sift_generated.h"
16#include "y2020/vision/sift/sift_training_generated.h"
17
18namespace frc971 {
19namespace vision {
20
21// Class to find extrinsics for a specified pi's camera using the provided
22// training data.
23class CameraCalibration {
24 public:
25 CameraCalibration(const absl::Span<const uint8_t> training_data_bfbs,
26 std::string_view pi);
27
28 // Intrinsics for the located camera.
29 cv::Mat CameraIntrinsics() const;
30 Eigen::Matrix3d CameraIntrinsicsEigen() const;
31
32 // Distortion coefficients for the located camera.
33 cv::Mat CameraDistCoeffs() const;
34
35 private:
36 // Finds the camera specific calibration flatbuffer.
37 const sift::CameraCalibration *FindCameraCalibration(
38 const sift::TrainingData *const training_data, std::string_view pi) const;
39
40 // Pointer to this camera's calibration parameters.
41 const sift::CameraCalibration *camera_calibration_;
42};
43
44// Class to call a function with a cv::Mat and age when an image shows up on the
45// provided channel. This hides all the conversions and wrangling needed to
46// view the image.
47class ImageCallback {
48 public:
Austin Schuhea7b0142021-10-08 22:04:53 -070049 ImageCallback(
50 aos::EventLoop *event_loop, std::string_view channel,
51 std::function<void(cv::Mat, aos::monotonic_clock::time_point)> &&fn);
Austin Schuh25837f22021-06-27 15:49:14 -070052
53 private:
54 aos::EventLoop *event_loop_;
Austin Schuhea7b0142021-10-08 22:04:53 -070055 aos::Fetcher<aos::message_bridge::ServerStatistics> server_fetcher_;
56 const aos::Node *source_node_;
57 std::function<void(cv::Mat, aos::monotonic_clock::time_point)> handle_image_;
Austin Schuh25837f22021-06-27 15:49:14 -070058};
59
60// Class which calls a callback each time an image arrives with the information
61// extracted from it.
62class CharucoExtractor {
63 public:
64 // The callback takes the following arguments:
65 // cv::Mat -> image with overlays drawn on it.
Austin Schuhea7b0142021-10-08 22:04:53 -070066 // monotonic_clock::time_point -> Time on this node when this image was
67 // captured.
Austin Schuh25837f22021-06-27 15:49:14 -070068 // std::vector<int> -> charuco_ids
69 // std::vector<cv::Point2f> -> charuco_corners
70 // bool -> true if rvec/tvec is valid.
71 // Eigen::Vector3d -> rvec
72 // Eigen::Vector3d -> tvec
Austin Schuhea7b0142021-10-08 22:04:53 -070073 CharucoExtractor(
74 aos::EventLoop *event_loop, std::string_view pi,
75 std::function<void(cv::Mat, aos::monotonic_clock::time_point,
76 std::vector<int>, std::vector<cv::Point2f>, bool,
77 Eigen::Vector3d, Eigen::Vector3d)> &&fn);
Austin Schuh25837f22021-06-27 15:49:14 -070078
79 // Returns the aruco dictionary in use.
80 cv::Ptr<cv::aruco::Dictionary> dictionary() const { return dictionary_; }
81 // Returns the aruco board in use.
82 cv::Ptr<cv::aruco::CharucoBoard> board() const { return board_; }
83
84 // Returns the camera matrix for this camera.
85 const cv::Mat camera_matrix() const { return camera_matrix_; }
86 // Returns the distortion coefficients for this camera.
87 const cv::Mat dist_coeffs() const { return dist_coeffs_; }
88
89 private:
Austin Schuhea7b0142021-10-08 22:04:53 -070090 // Handles the image by detecting the charuco board in it.
91 void HandleImage(cv::Mat rgb_image, aos::monotonic_clock::time_point eof);
Austin Schuh25837f22021-06-27 15:49:14 -070092
Austin Schuhea7b0142021-10-08 22:04:53 -070093 aos::EventLoop *event_loop_;
Austin Schuh25837f22021-06-27 15:49:14 -070094 CameraCalibration calibration_;
95
96 cv::Ptr<cv::aruco::Dictionary> dictionary_;
97 cv::Ptr<cv::aruco::CharucoBoard> board_;
98
99 const cv::Mat camera_matrix_;
100 const Eigen::Matrix3d eigen_camera_matrix_;
101 const cv::Mat dist_coeffs_;
102
103 const std::optional<uint16_t> pi_number_;
104
105 ImageCallback image_callback_;
106
107 // Function to call.
Austin Schuhea7b0142021-10-08 22:04:53 -0700108 std::function<void(cv::Mat, aos::monotonic_clock::time_point,
109 std::vector<int>, std::vector<cv::Point2f>, bool,
110 Eigen::Vector3d, Eigen::Vector3d)>
Austin Schuh25837f22021-06-27 15:49:14 -0700111 handle_charuco_;
112};
113
114} // namespace vision
115} // namespace frc971
116
117#endif // Y2020_VISION_CHARUCO_LIB_H_