blob: 5c89a6d602c5937adc2ce160826304c4e9f00af3 [file] [log] [blame]
Parker Schuhf7481be2017-03-04 18:24:33 -08001#ifndef _Y2017_VISION_TARGET_FINDER_H_
2#define _Y2017_VISION_TARGET_FINDER_H_
3
4#include "aos/vision/blob/threshold.h"
5#include "aos/vision/blob/transpose.h"
6#include "aos/vision/debug/overlay.h"
7#include "aos/vision/math/vector.h"
8
Philipp Schrader790cb542023-07-05 21:06:52 -07009using aos::vision::BlobList;
Parker Schuhf7481be2017-03-04 18:24:33 -080010using aos::vision::ImageRange;
11using aos::vision::RangeImage;
Parker Schuhf7481be2017-03-04 18:24:33 -080012using aos::vision::Vector;
13
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080014namespace y2017::vision {
Parker Schuhf7481be2017-03-04 18:24:33 -080015
16// This polynomial exists in transpose space.
17struct TargetComponent {
18 const RangeImage *img = nullptr;
19
20 // Polynomial constants.
21 double a;
22 double b;
23 double c_0;
24 double c_1;
25
26 double mini;
27
28 double CenterPolyOne() { return -b / (2.0 * a); }
29 double CenterPolyTwo() { return (c_0); }
30
31 double EvalMinAt(double c) {
32 double min = CenterPolyOne();
33 return a * (min * min) + b * min + c;
34 }
35
36 double EvalMinTop() { return EvalMinAt(c_1); }
37 double EvalMinBot() { return EvalMinAt(c_0); }
38
39 // Fit error is not normalized by n.
40 double fit_error;
41 int n;
42
43 RangeImage RenderShifted() const;
44};
45
46// Convert back to screen space for final result.
47struct Target {
48 TargetComponent comp1;
49
50 TargetComponent comp2;
51 aos::vision::Vector<2> screen_coord;
52};
53
54class TargetFinder {
55 public:
56 // Turn a bloblist into components of a target.
57 std::vector<TargetComponent> FillTargetComponentList(const BlobList &blobs);
58
59 // Turn a raw image into blob range image.
60 aos::vision::RangeImage Threshold(aos::vision::ImagePtr image);
61
62 // filter out obvious or durranged blobs.
63 void PreFilter(BlobList &imgs);
64
65 // Piece the compenents together into a target.
66 bool FindTargetFromComponents(std::vector<TargetComponent> component_list,
67 Target *final_target);
68
69 // Get the local overlay for debug if we are doing that.
70 aos::vision::PixelLinesOverlay *GetOverlay() { return &overlay_; }
71
Parker Schuhabb6b6c2017-03-11 16:31:24 -080072 // Convert target location into meters and radians.
Philipp Schrader790cb542023-07-05 21:06:52 -070073 void GetAngleDist(const aos::vision::Vector<2> &target, double down_angle,
Parker Schuhabb6b6c2017-03-11 16:31:24 -080074 double *dist, double *angle);
75
Parker Schuhf7481be2017-03-04 18:24:33 -080076 private:
77 // Find a loosly connected target.
78 double DetectConnectedTarget(const RangeImage &img);
79
80 // TODO(ben): move to overlay
81 void DrawCross(aos::vision::Vector<2> center, aos::vision::PixelRef color);
82
83 aos::vision::PixelLinesOverlay overlay_;
84};
85
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080086} // namespace y2017::vision
Parker Schuhf7481be2017-03-04 18:24:33 -080087
88#endif // _Y2017_VISION_TARGET_FINDER_H_