blob: ebae3b4f6f6ef04c17a67daa6086e0f25edfa6c9 [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
55 std::vector<IntermediateResult> FilterResults(
Ben Fredricksona8c3d552019-03-03 14:14:53 -080056 const std::vector<IntermediateResult> &results, uint64_t print_rate);
Parker Schuh2a1447c2019-02-17 00:25:29 -080057
58 // Get the local overlay for debug if we are doing that.
59 aos::vision::PixelLinesOverlay *GetOverlay() { return &overlay_; }
60
61 // Convert target location into meters and radians.
62 void GetAngleDist(const aos::vision::Vector<2> &target, double down_angle,
63 double *dist, double *angle);
64
65 // Return the template target in a normalized space.
66 const Target &GetTemplateTarget() { return target_template_; }
67
68 const IntrinsicParams &intrinsics() const { return intrinsics_; }
Alex Perry3bf1bee2019-02-23 20:01:15 -080069 IntrinsicParams *mutable_intrinsics() { return &intrinsics_; }
Parker Schuh2a1447c2019-02-17 00:25:29 -080070
71 private:
72 // Find a loosly connected target.
73 double DetectConnectedTarget(const RangeImage &img);
74
75 aos::vision::PixelLinesOverlay overlay_;
76
77 aos::vision::AnalysisAllocator alloc_;
78
79 // The template for the default target in the standard space.
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080080 const Target target_template_;
Parker Schuh2a1447c2019-02-17 00:25:29 -080081
82 IntrinsicParams intrinsics_;
83
84 ExtrinsicParams default_extrinsics_;
Ben Fredricksona8c3d552019-03-03 14:14:53 -080085
86 // Counts for logging.
87 size_t frame_count_;
88 size_t valid_result_count_;
Parker Schuh2a1447c2019-02-17 00:25:29 -080089};
90
91} // namespace vision
92} // namespace y2019
93
94#endif