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