Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 1 | #ifndef Y2022_VISION_TARGET_ESTIMATOR_H_ |
| 2 | #define Y2022_VISION_TARGET_ESTIMATOR_H_ |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 3 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 4 | #include <optional> |
| 5 | |
| 6 | #include "Eigen/Dense" |
| 7 | #include "Eigen/Geometry" |
| 8 | #include "opencv2/core/types.hpp" |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 9 | #include "opencv2/imgproc.hpp" |
Milind Upadhyay | 8f38ad8 | 2022-03-03 10:06:18 -0800 | [diff] [blame^] | 10 | #include "y2022/vision/blob_detector.h" |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 11 | #include "y2022/vision/target_estimate_generated.h" |
| 12 | |
| 13 | namespace y2022::vision { |
Milind Upadhyay | 14279de | 2022-02-26 16:07:53 -0800 | [diff] [blame] | 14 | |
Milind Upadhyay | 8f38ad8 | 2022-03-03 10:06:18 -0800 | [diff] [blame^] | 15 | // Class to estimate the distance and rotation of the camera from the |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 16 | // target. |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 17 | class TargetEstimator { |
| 18 | public: |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 19 | TargetEstimator(cv::Mat intrinsics, cv::Mat extrinsics); |
| 20 | |
| 21 | // Runs the solver to estimate the target |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 22 | // If image != std::nullopt, the solver's progress will be displayed |
| 23 | // graphically. |
Milind Upadhyay | 8f38ad8 | 2022-03-03 10:06:18 -0800 | [diff] [blame^] | 24 | void Solve(const std::vector<BlobDetector::BlobStats> &blob_stats, |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 25 | 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 Upadhyay | 14279de | 2022-02-26 16:07:53 -0800 | [diff] [blame] | 48 | inline double confidence() const { return confidence_; } |
| 49 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 50 | // 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-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 54 | |
| 55 | private: |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 56 | // 3d points of the visible pieces of tape in the hub's frame |
| 57 | static const std::vector<cv::Point3d> kTapePoints; |
Milind Upadhyay | 8f38ad8 | 2022-03-03 10:06:18 -0800 | [diff] [blame^] | 58 | // 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 Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 66 | |
| 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 Upadhyay | 8f38ad8 | 2022-03-03 10:06:18 -0800 | [diff] [blame^] | 72 | std::vector<BlobDetector::BlobStats> blob_stats_; |
| 73 | size_t middle_blob_index_; |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 74 | 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 Upadhyay | 14279de | 2022-02-26 16:07:53 -0800 | [diff] [blame] | 86 | double confidence_; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 87 | }; |
| 88 | |
| 89 | } // namespace y2022::vision |
| 90 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 91 | #endif // Y2022_VISION_TARGET_ESTIMATOR_H_ |