Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 1 | #ifndef _Y2019_VISION_TARGET_FINDER_H_ |
| 2 | #define _Y2019_VISION_TARGET_FINDER_H_ |
| 3 | |
Brian Silverman | 6323677 | 2019-03-23 22:02:44 -0700 | [diff] [blame] | 4 | #include <memory> |
| 5 | |
| 6 | #include "aos/vision/blob/contour.h" |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 7 | #include "aos/vision/blob/region_alloc.h" |
| 8 | #include "aos/vision/blob/threshold.h" |
| 9 | #include "aos/vision/blob/transpose.h" |
| 10 | #include "aos/vision/debug/overlay.h" |
| 11 | #include "aos/vision/math/vector.h" |
| 12 | #include "y2019/vision/target_types.h" |
| 13 | |
Brian Silverman | 6323677 | 2019-03-23 22:02:44 -0700 | [diff] [blame] | 14 | namespace ceres { |
| 15 | |
| 16 | class Context; |
| 17 | |
| 18 | } // namespace ceres |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 19 | namespace y2019::vision { |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 20 | |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 21 | using aos::vision::BlobList; |
| 22 | using aos::vision::ContourNode; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 23 | using aos::vision::ImageRange; |
| 24 | using aos::vision::RangeImage; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 25 | using aos::vision::Vector; |
| 26 | |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 27 | struct Polygon { |
| 28 | ::std::vector<aos::vision::Segment<2>> segments; |
| 29 | ::std::vector<::Eigen::Vector2f> contour; |
| 30 | }; |
| 31 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 32 | class TargetFinder { |
| 33 | public: |
| 34 | TargetFinder(); |
Brian Silverman | 6323677 | 2019-03-23 22:02:44 -0700 | [diff] [blame] | 35 | ~TargetFinder(); |
| 36 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 37 | // Turn a raw image into blob range image. |
| 38 | aos::vision::RangeImage Threshold(aos::vision::ImagePtr image); |
| 39 | |
| 40 | // Value against which we threshold. |
Austin Schuh | 2851e7f | 2019-03-06 20:45:19 -0800 | [diff] [blame] | 41 | static uint8_t GetThresholdValue() { return 100; } |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 42 | |
Austin Schuh | 3b1586a | 2019-05-02 13:46:52 -0700 | [diff] [blame] | 43 | int PixelCount(BlobList *imgs); |
| 44 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 45 | // filter out obvious or durranged blobs. |
| 46 | void PreFilter(BlobList *imgs); |
| 47 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 48 | ContourNode *GetContour(const RangeImage &blob); |
| 49 | ::std::vector<::Eigen::Vector2f> UnWarpContour(ContourNode *start) const; |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 50 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 51 | // Turn a blob into a polgygon. |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 52 | Polygon FindPolygon(::std::vector<::Eigen::Vector2f> &&contour, bool verbose); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 53 | |
| 54 | // Turn a bloblist into components of a target. |
| 55 | std::vector<TargetComponent> FillTargetComponentList( |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 56 | const ::std::vector<Polygon> &seg_list, bool verbose); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 57 | |
| 58 | // Piece the compenents together into a target. |
| 59 | std::vector<Target> FindTargetsFromComponents( |
| 60 | const std::vector<TargetComponent> component_list, bool verbose); |
| 61 | |
| 62 | // Given a target solve for the transformation of the template target. |
Alex Perry | 2431927 | 2019-04-07 12:31:29 -0700 | [diff] [blame] | 63 | // |
| 64 | // This is safe to call concurrently. |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 65 | IntermediateResult ProcessTargetToResult(const Target &target, bool verbose); |
| 66 | |
Austin Schuh | e6cfbe3 | 2019-03-10 18:05:57 -0700 | [diff] [blame] | 67 | // Returns true if a target is good, and false otherwise. Picks the 4 vs 8 |
| 68 | // point solution depending on which one looks right. |
| 69 | bool MaybePickAndUpdateResult(IntermediateResult *result, bool verbose); |
| 70 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 71 | std::vector<IntermediateResult> FilterResults( |
Alex Perry | bac3d3f | 2019-03-10 14:26:51 -0700 | [diff] [blame] | 72 | const std::vector<IntermediateResult> &results, uint64_t print_rate, |
| 73 | bool verbose); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 74 | |
Alex Perry | 5b1e8e3 | 2019-04-07 13:25:31 -0700 | [diff] [blame] | 75 | bool TestExposure(const std::vector<IntermediateResult> &results, |
Austin Schuh | 3b1586a | 2019-05-02 13:46:52 -0700 | [diff] [blame] | 76 | int pixel_count, int *desired_exposure); |
Alex Perry | 5b1e8e3 | 2019-04-07 13:25:31 -0700 | [diff] [blame] | 77 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 78 | // Get the local overlay for debug if we are doing that. |
| 79 | aos::vision::PixelLinesOverlay *GetOverlay() { return &overlay_; } |
| 80 | |
| 81 | // Convert target location into meters and radians. |
| 82 | void GetAngleDist(const aos::vision::Vector<2> &target, double down_angle, |
| 83 | double *dist, double *angle); |
| 84 | |
| 85 | // Return the template target in a normalized space. |
| 86 | const Target &GetTemplateTarget() { return target_template_; } |
| 87 | |
| 88 | const IntrinsicParams &intrinsics() const { return intrinsics_; } |
Alex Perry | 3bf1bee | 2019-02-23 20:01:15 -0800 | [diff] [blame] | 89 | IntrinsicParams *mutable_intrinsics() { return &intrinsics_; } |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 90 | |
| 91 | private: |
| 92 | // Find a loosly connected target. |
| 93 | double DetectConnectedTarget(const RangeImage &img); |
| 94 | |
| 95 | aos::vision::PixelLinesOverlay overlay_; |
| 96 | |
| 97 | aos::vision::AnalysisAllocator alloc_; |
| 98 | |
| 99 | // The template for the default target in the standard space. |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 100 | const Target target_template_; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 101 | |
| 102 | IntrinsicParams intrinsics_; |
| 103 | |
| 104 | ExtrinsicParams default_extrinsics_; |
Ben Fredrickson | a8c3d55 | 2019-03-03 14:14:53 -0800 | [diff] [blame] | 105 | |
Brian Silverman | 6323677 | 2019-03-23 22:02:44 -0700 | [diff] [blame] | 106 | const std::unique_ptr<ceres::Context> ceres_context_; |
| 107 | |
Ben Fredrickson | a8c3d55 | 2019-03-03 14:14:53 -0800 | [diff] [blame] | 108 | // Counts for logging. |
| 109 | size_t frame_count_; |
| 110 | size_t valid_result_count_; |
Alex Perry | 5b1e8e3 | 2019-04-07 13:25:31 -0700 | [diff] [blame] | 111 | |
Austin Schuh | 3b1586a | 2019-05-02 13:46:52 -0700 | [diff] [blame] | 112 | int close_bucket_ = 0; |
| 113 | |
Alex Perry | 5b1e8e3 | 2019-04-07 13:25:31 -0700 | [diff] [blame] | 114 | int current_exposure_ = 0; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
Stephan Pleines | d99b1ee | 2024-02-02 20:56:44 -0800 | [diff] [blame] | 117 | } // namespace y2019::vision |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 118 | |
| 119 | #endif |