blob: b509a2ec3a2e3127c9b364c8347056428d33df87 [file] [log] [blame]
Milind Upadhyayf61e1482022-02-11 20:42:55 -08001#ifndef Y2022_VISION_TARGET_ESTIMATOR_H_
2#define Y2022_VISION_TARGET_ESTIMATOR_H_
milind-u92195982022-01-22 20:29:31 -08003
Milind Upadhyayf61e1482022-02-11 20:42:55 -08004#include <optional>
5
6#include "Eigen/Dense"
7#include "Eigen/Geometry"
8#include "opencv2/core/types.hpp"
milind-u92195982022-01-22 20:29:31 -08009#include "opencv2/imgproc.hpp"
Milind Upadhyay8f38ad82022-03-03 10:06:18 -080010#include "y2022/vision/blob_detector.h"
milind-u92195982022-01-22 20:29:31 -080011#include "y2022/vision/target_estimate_generated.h"
12
13namespace y2022::vision {
Milind Upadhyay14279de2022-02-26 16:07:53 -080014
Milind Upadhyay8f38ad82022-03-03 10:06:18 -080015// Class to estimate the distance and rotation of the camera from the
Milind Upadhyayf61e1482022-02-11 20:42:55 -080016// target.
milind-u92195982022-01-22 20:29:31 -080017class TargetEstimator {
18 public:
Milind Upadhyayf61e1482022-02-11 20:42:55 -080019 TargetEstimator(cv::Mat intrinsics, cv::Mat extrinsics);
20
21 // Runs the solver to estimate the target
Milind Upadhyayf61e1482022-02-11 20:42:55 -080022 // If image != std::nullopt, the solver's progress will be displayed
23 // graphically.
Milind Upadhyay8f38ad82022-03-03 10:06:18 -080024 void Solve(const std::vector<BlobDetector::BlobStats> &blob_stats,
Milind Upadhyayf61e1482022-02-11 20:42:55 -080025 std::optional<cv::Mat> image);
26
27 // Cost function for the solver.
28 // Takes in the rotation of the camera in the hub's frame, the horizontal
29 // polar coordinates of the camera in the hub's frame, and the height of the
30 // camera (can change if the robot is shaking).
31 // Hub frame is relative to the center of the bottom of the hub.
32 // Compares the projected pieces of tape with these values to the detected
33 // blobs for calculating the cost.
34 template <typename S>
35 bool operator()(const S *const roll, const S *const pitch, const S *const yaw,
36 const S *const distance, const S *const theta,
37 const S *const camera_height, S *residual) const;
38
39 inline double roll() const { return roll_; }
40 inline double pitch() const { return pitch_; }
41 inline double yaw() const { return yaw_; }
42
43 inline double distance() const { return distance_; }
44 inline double angle_to_camera() const { return angle_to_camera_; }
45 inline double angle_to_target() const { return M_PI - yaw_; }
46 inline double camera_height() const { return camera_height_; }
47
Milind Upadhyay14279de2022-02-26 16:07:53 -080048 inline double confidence() const { return confidence_; }
49
Milind Upadhyayf61e1482022-02-11 20:42:55 -080050 // Draws the distance, angle, and rotation on the given image
51 static void DrawEstimate(const TargetEstimate &target_estimate,
52 cv::Mat view_image);
53 void DrawEstimate(cv::Mat view_image) const;
milind-u92195982022-01-22 20:29:31 -080054
55 private:
Milind Upadhyayf61e1482022-02-11 20:42:55 -080056 // 3d points of the visible pieces of tape in the hub's frame
57 static const std::vector<cv::Point3d> kTapePoints;
Milind Upadhyay8f38ad82022-03-03 10:06:18 -080058 // 3d outer points of the middle piece of tape in the hub's frame,
59 // clockwise around the rectangle
60 static const std::array<cv::Point3d, 4> kMiddleTapePiecePoints;
61
62 template <typename S>
63 cv::Point_<S> ProjectToImage(
64 cv::Point3d tape_point_hub,
65 Eigen::Transform<S, 3, Eigen::Affine> &H_hub_camera) const;
Milind Upadhyayf61e1482022-02-11 20:42:55 -080066
67 template <typename S>
68 cv::Point_<S> DistanceFromTape(
69 size_t centroid_index,
70 const std::vector<cv::Point_<S>> &tape_points) const;
71
Milind Upadhyay8f38ad82022-03-03 10:06:18 -080072 std::vector<BlobDetector::BlobStats> blob_stats_;
73 size_t middle_blob_index_;
Milind Upadhyayf61e1482022-02-11 20:42:55 -080074 std::optional<cv::Mat> image_;
75
76 Eigen::Matrix3d intrinsics_;
77 Eigen::Matrix4d extrinsics_;
78
79 double roll_;
80 double pitch_;
81 double yaw_;
82
83 double distance_;
84 double angle_to_camera_;
85 double camera_height_;
Milind Upadhyay14279de2022-02-26 16:07:53 -080086 double confidence_;
milind-u92195982022-01-22 20:29:31 -080087};
88
89} // namespace y2022::vision
90
Milind Upadhyayf61e1482022-02-11 20:42:55 -080091#endif // Y2022_VISION_TARGET_ESTIMATOR_H_