blob: ac170e8523ddb0b4ae85d24e443bd45529ca3c4a [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>
Austin Schuha685b5d2022-04-02 14:53:54 -070071 size_t ClosestTape(size_t centroid_index,
72 const std::vector<cv::Point_<S>> &tape_points) const;
73
74 template <typename S>
75 cv::Point_<S> DistanceFromTapeIndex(
Milind Upadhyay1c76a042022-04-02 20:42:42 -070076 size_t centroid_index, size_t tape_index,
Milind Upadhyayf61e1482022-02-11 20:42:55 -080077 const std::vector<cv::Point_<S>> &tape_points) const;
78
Milind Upadhyay3336f3a2022-04-01 21:45:57 -070079 void DrawProjectedHub(const std::vector<cv::Point2d> &tape_points_proj,
80 cv::Mat view_image) const;
81
Milind Upadhyay8f38ad82022-03-03 10:06:18 -080082 std::vector<BlobDetector::BlobStats> blob_stats_;
83 size_t middle_blob_index_;
Milind Upadhyay1c76a042022-04-02 20:42:42 -070084 double max_blob_area_;
Milind Upadhyayf61e1482022-02-11 20:42:55 -080085 std::optional<cv::Mat> image_;
86
87 Eigen::Matrix3d intrinsics_;
88 Eigen::Matrix4d extrinsics_;
89
90 double roll_;
91 double pitch_;
92 double yaw_;
93
94 double distance_;
95 double angle_to_camera_;
96 double camera_height_;
Milind Upadhyay14279de2022-02-26 16:07:53 -080097 double confidence_;
milind-u92195982022-01-22 20:29:31 -080098};
99
100} // namespace y2022::vision
101
Milind Upadhyayf61e1482022-02-11 20:42:55 -0800102#endif // Y2022_VISION_TARGET_ESTIMATOR_H_