blob: c7de67aeb580d0ed2ac49a66acf4bfc66bce8da1 [file] [log] [blame]
Parker Schuh2a1447c2019-02-17 00:25:29 -08001#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 Fredricksonf7b68522019-03-02 21:19:42 -08007#include "aos/vision/blob/contour.h"
Parker Schuh2a1447c2019-02-17 00:25:29 -08008#include "aos/vision/debug/overlay.h"
9#include "aos/vision/math/vector.h"
10#include "y2019/vision/target_types.h"
11
12namespace y2019 {
13namespace vision {
14
15using aos::vision::ImageRange;
16using aos::vision::RangeImage;
17using aos::vision::BlobList;
18using aos::vision::Vector;
Ben Fredricksonf7b68522019-03-02 21:19:42 -080019using aos::vision::ContourNode;
Parker Schuh2a1447c2019-02-17 00:25:29 -080020
Austin Schuh6e56faf2019-03-10 14:04:57 -070021struct Polygon {
22 ::std::vector<aos::vision::Segment<2>> segments;
23 ::std::vector<::Eigen::Vector2f> contour;
24};
25
Parker Schuh2a1447c2019-02-17 00:25:29 -080026class 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 Schuh2851e7f2019-03-06 20:45:19 -080033 static uint8_t GetThresholdValue() { return 100; }
Parker Schuh2a1447c2019-02-17 00:25:29 -080034
35 // filter out obvious or durranged blobs.
36 void PreFilter(BlobList *imgs);
37
Austin Schuhe5015972019-03-09 17:47:34 -080038 ContourNode *GetContour(const RangeImage &blob);
39 ::std::vector<::Eigen::Vector2f> UnWarpContour(ContourNode *start) const;
Ben Fredricksonf7b68522019-03-02 21:19:42 -080040
Parker Schuh2a1447c2019-02-17 00:25:29 -080041 // Turn a blob into a polgygon.
Austin Schuh6e56faf2019-03-10 14:04:57 -070042 Polygon FindPolygon(::std::vector<::Eigen::Vector2f> &&contour, bool verbose);
Parker Schuh2a1447c2019-02-17 00:25:29 -080043
44 // Turn a bloblist into components of a target.
45 std::vector<TargetComponent> FillTargetComponentList(
Austin Schuh6e56faf2019-03-10 14:04:57 -070046 const ::std::vector<Polygon> &seg_list, bool verbose);
Parker Schuh2a1447c2019-02-17 00:25:29 -080047
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 Schuhe6cfbe32019-03-10 18:05:57 -070055 // 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 Schuh2a1447c2019-02-17 00:25:29 -080059 std::vector<IntermediateResult> FilterResults(
Alex Perrybac3d3f2019-03-10 14:26:51 -070060 const std::vector<IntermediateResult> &results, uint64_t print_rate,
61 bool verbose);
Parker Schuh2a1447c2019-02-17 00:25:29 -080062
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 Perry3bf1bee2019-02-23 20:01:15 -080074 IntrinsicParams *mutable_intrinsics() { return &intrinsics_; }
Parker Schuh2a1447c2019-02-17 00:25:29 -080075
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 Schuh4d6e9bd2019-03-08 19:54:17 -080085 const Target target_template_;
Parker Schuh2a1447c2019-02-17 00:25:29 -080086
87 IntrinsicParams intrinsics_;
88
89 ExtrinsicParams default_extrinsics_;
Ben Fredricksona8c3d552019-03-03 14:14:53 -080090
91 // Counts for logging.
92 size_t frame_count_;
93 size_t valid_result_count_;
Parker Schuh2a1447c2019-02-17 00:25:29 -080094};
95
96} // namespace vision
97} // namespace y2019
98
99#endif