blob: 0f1575c4dc8fb79b34d0c80c3d88495c6b2107c1 [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
Brian Silverman63236772019-03-23 22:02:44 -07004#include <memory>
5
6#include "aos/vision/blob/contour.h"
Parker Schuh2a1447c2019-02-17 00:25:29 -08007#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 Silverman63236772019-03-23 22:02:44 -070014namespace ceres {
15
16class Context;
17
18} // namespace ceres
Parker Schuh2a1447c2019-02-17 00:25:29 -080019namespace y2019 {
20namespace vision {
21
22using aos::vision::ImageRange;
23using aos::vision::RangeImage;
24using aos::vision::BlobList;
25using aos::vision::Vector;
Ben Fredricksonf7b68522019-03-02 21:19:42 -080026using aos::vision::ContourNode;
Parker Schuh2a1447c2019-02-17 00:25:29 -080027
Austin Schuh6e56faf2019-03-10 14:04:57 -070028struct Polygon {
29 ::std::vector<aos::vision::Segment<2>> segments;
30 ::std::vector<::Eigen::Vector2f> contour;
31};
32
Parker Schuh2a1447c2019-02-17 00:25:29 -080033class TargetFinder {
34 public:
35 TargetFinder();
Brian Silverman63236772019-03-23 22:02:44 -070036 ~TargetFinder();
37
Parker Schuh2a1447c2019-02-17 00:25:29 -080038 // Turn a raw image into blob range image.
39 aos::vision::RangeImage Threshold(aos::vision::ImagePtr image);
40
41 // Value against which we threshold.
Austin Schuh2851e7f2019-03-06 20:45:19 -080042 static uint8_t GetThresholdValue() { return 100; }
Parker Schuh2a1447c2019-02-17 00:25:29 -080043
44 // filter out obvious or durranged blobs.
45 void PreFilter(BlobList *imgs);
46
Austin Schuhe5015972019-03-09 17:47:34 -080047 ContourNode *GetContour(const RangeImage &blob);
48 ::std::vector<::Eigen::Vector2f> UnWarpContour(ContourNode *start) const;
Ben Fredricksonf7b68522019-03-02 21:19:42 -080049
Parker Schuh2a1447c2019-02-17 00:25:29 -080050 // Turn a blob into a polgygon.
Austin Schuh6e56faf2019-03-10 14:04:57 -070051 Polygon FindPolygon(::std::vector<::Eigen::Vector2f> &&contour, bool verbose);
Parker Schuh2a1447c2019-02-17 00:25:29 -080052
53 // Turn a bloblist into components of a target.
54 std::vector<TargetComponent> FillTargetComponentList(
Austin Schuh6e56faf2019-03-10 14:04:57 -070055 const ::std::vector<Polygon> &seg_list, bool verbose);
Parker Schuh2a1447c2019-02-17 00:25:29 -080056
57 // Piece the compenents together into a target.
58 std::vector<Target> FindTargetsFromComponents(
59 const std::vector<TargetComponent> component_list, bool verbose);
60
61 // Given a target solve for the transformation of the template target.
Alex Perry24319272019-04-07 12:31:29 -070062 //
63 // This is safe to call concurrently.
Parker Schuh2a1447c2019-02-17 00:25:29 -080064 IntermediateResult ProcessTargetToResult(const Target &target, bool verbose);
65
Austin Schuhe6cfbe32019-03-10 18:05:57 -070066 // Returns true if a target is good, and false otherwise. Picks the 4 vs 8
67 // point solution depending on which one looks right.
68 bool MaybePickAndUpdateResult(IntermediateResult *result, bool verbose);
69
Parker Schuh2a1447c2019-02-17 00:25:29 -080070 std::vector<IntermediateResult> FilterResults(
Alex Perrybac3d3f2019-03-10 14:26:51 -070071 const std::vector<IntermediateResult> &results, uint64_t print_rate,
72 bool verbose);
Parker Schuh2a1447c2019-02-17 00:25:29 -080073
Alex Perry5b1e8e32019-04-07 13:25:31 -070074 bool TestExposure(const std::vector<IntermediateResult> &results,
75 int *desired_exposure);
76
Parker Schuh2a1447c2019-02-17 00:25:29 -080077 // Get the local overlay for debug if we are doing that.
78 aos::vision::PixelLinesOverlay *GetOverlay() { return &overlay_; }
79
80 // Convert target location into meters and radians.
81 void GetAngleDist(const aos::vision::Vector<2> &target, double down_angle,
82 double *dist, double *angle);
83
84 // Return the template target in a normalized space.
85 const Target &GetTemplateTarget() { return target_template_; }
86
87 const IntrinsicParams &intrinsics() const { return intrinsics_; }
Alex Perry3bf1bee2019-02-23 20:01:15 -080088 IntrinsicParams *mutable_intrinsics() { return &intrinsics_; }
Parker Schuh2a1447c2019-02-17 00:25:29 -080089
90 private:
91 // Find a loosly connected target.
92 double DetectConnectedTarget(const RangeImage &img);
93
94 aos::vision::PixelLinesOverlay overlay_;
95
96 aos::vision::AnalysisAllocator alloc_;
97
98 // The template for the default target in the standard space.
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080099 const Target target_template_;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800100
101 IntrinsicParams intrinsics_;
102
103 ExtrinsicParams default_extrinsics_;
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800104
Brian Silverman63236772019-03-23 22:02:44 -0700105 const std::unique_ptr<ceres::Context> ceres_context_;
106
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800107 // Counts for logging.
108 size_t frame_count_;
109 size_t valid_result_count_;
Alex Perry5b1e8e32019-04-07 13:25:31 -0700110
111 int current_exposure_ = 0;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800112};
113
114} // namespace vision
115} // namespace y2019
116
117#endif