blob: 174f7741a56eb4820ed3822eed0450e565f4f026 [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 Upadhyay3336f3a2022-04-01 21:45:57 -070050 // Draws the distance, angle, rotation, and projected tape on the given image
Milind Upadhyayf61e1482022-02-11 20:42:55 -080051 void DrawEstimate(cv::Mat view_image) const;
milind-u92195982022-01-22 20:29:31 -080052
53 private:
Milind Upadhyayf61e1482022-02-11 20:42:55 -080054 // 3d points of the visible pieces of tape in the hub's frame
55 static const std::vector<cv::Point3d> kTapePoints;
Milind Upadhyay8f38ad82022-03-03 10:06:18 -080056 // 3d outer points of the middle piece of tape in the hub's frame,
57 // clockwise around the rectangle
58 static const std::array<cv::Point3d, 4> kMiddleTapePiecePoints;
59
Milind Upadhyay3336f3a2022-04-01 21:45:57 -070060 // Computes matrix of hub in camera's frame
61 template <typename S>
62 Eigen::Transform<S, 3, Eigen::Affine> ComputeHubCameraTransform(
63 S roll, S pitch, S yaw, S distance, S theta, S camera_height) const;
64
Milind Upadhyay8f38ad82022-03-03 10:06:18 -080065 template <typename S>
66 cv::Point_<S> ProjectToImage(
67 cv::Point3d tape_point_hub,
Milind Upadhyay3336f3a2022-04-01 21:45:57 -070068 const Eigen::Transform<S, 3, Eigen::Affine> &H_hub_camera) const;
Milind Upadhyayf61e1482022-02-11 20:42:55 -080069
70 template <typename S>
71 cv::Point_<S> DistanceFromTape(
72 size_t centroid_index,
73 const std::vector<cv::Point_<S>> &tape_points) const;
74
Milind Upadhyay3336f3a2022-04-01 21:45:57 -070075 void DrawProjectedHub(const std::vector<cv::Point2d> &tape_points_proj,
76 cv::Mat view_image) const;
77
Milind Upadhyay8f38ad82022-03-03 10:06:18 -080078 std::vector<BlobDetector::BlobStats> blob_stats_;
79 size_t middle_blob_index_;
Milind Upadhyayf61e1482022-02-11 20:42:55 -080080 std::optional<cv::Mat> image_;
81
82 Eigen::Matrix3d intrinsics_;
83 Eigen::Matrix4d extrinsics_;
84
85 double roll_;
86 double pitch_;
87 double yaw_;
88
89 double distance_;
90 double angle_to_camera_;
91 double camera_height_;
Milind Upadhyay14279de2022-02-26 16:07:53 -080092 double confidence_;
milind-u92195982022-01-22 20:29:31 -080093};
94
95} // namespace y2022::vision
96
Milind Upadhyayf61e1482022-02-11 20:42:55 -080097#endif // Y2022_VISION_TARGET_ESTIMATOR_H_