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