Parker Schuh | f7481be | 2017-03-04 18:24:33 -0800 | [diff] [blame] | 1 | #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 | |
| 9 | using aos::vision::ImageRange; |
| 10 | using aos::vision::RangeImage; |
| 11 | using aos::vision::BlobList; |
| 12 | using aos::vision::Vector; |
| 13 | |
| 14 | namespace y2017 { |
| 15 | namespace vision { |
| 16 | |
| 17 | // This polynomial exists in transpose space. |
| 18 | struct TargetComponent { |
| 19 | const RangeImage *img = nullptr; |
| 20 | |
| 21 | // Polynomial constants. |
| 22 | double a; |
| 23 | double b; |
| 24 | double c_0; |
| 25 | double c_1; |
| 26 | |
| 27 | double mini; |
| 28 | |
| 29 | double CenterPolyOne() { return -b / (2.0 * a); } |
| 30 | double CenterPolyTwo() { return (c_0); } |
| 31 | |
| 32 | double EvalMinAt(double c) { |
| 33 | double min = CenterPolyOne(); |
| 34 | return a * (min * min) + b * min + c; |
| 35 | } |
| 36 | |
| 37 | double EvalMinTop() { return EvalMinAt(c_1); } |
| 38 | double EvalMinBot() { return EvalMinAt(c_0); } |
| 39 | |
| 40 | // Fit error is not normalized by n. |
| 41 | double fit_error; |
| 42 | int n; |
| 43 | |
| 44 | RangeImage RenderShifted() const; |
| 45 | }; |
| 46 | |
| 47 | // Convert back to screen space for final result. |
| 48 | struct Target { |
| 49 | TargetComponent comp1; |
| 50 | |
| 51 | TargetComponent comp2; |
| 52 | aos::vision::Vector<2> screen_coord; |
| 53 | }; |
| 54 | |
| 55 | class TargetFinder { |
| 56 | public: |
| 57 | // Turn a bloblist into components of a target. |
| 58 | std::vector<TargetComponent> FillTargetComponentList(const BlobList &blobs); |
| 59 | |
| 60 | // Turn a raw image into blob range image. |
| 61 | aos::vision::RangeImage Threshold(aos::vision::ImagePtr image); |
| 62 | |
| 63 | // filter out obvious or durranged blobs. |
| 64 | void PreFilter(BlobList &imgs); |
| 65 | |
| 66 | // Piece the compenents together into a target. |
| 67 | bool FindTargetFromComponents(std::vector<TargetComponent> component_list, |
| 68 | Target *final_target); |
| 69 | |
| 70 | // Get the local overlay for debug if we are doing that. |
| 71 | aos::vision::PixelLinesOverlay *GetOverlay() { return &overlay_; } |
| 72 | |
Parker Schuh | abb6b6c | 2017-03-11 16:31:24 -0800 | [diff] [blame^] | 73 | // Convert target location into meters and radians. |
| 74 | void GetAngleDist(const aos::vision::Vector<2>& target, double down_angle, |
| 75 | double *dist, double *angle); |
| 76 | |
Parker Schuh | f7481be | 2017-03-04 18:24:33 -0800 | [diff] [blame] | 77 | private: |
| 78 | // Find a loosly connected target. |
| 79 | double DetectConnectedTarget(const RangeImage &img); |
| 80 | |
| 81 | // TODO(ben): move to overlay |
| 82 | void DrawCross(aos::vision::Vector<2> center, aos::vision::PixelRef color); |
| 83 | |
| 84 | aos::vision::PixelLinesOverlay overlay_; |
| 85 | }; |
| 86 | |
| 87 | } // namespace vision |
| 88 | } // namespace y2017 |
| 89 | |
| 90 | #endif // _Y2017_VISION_TARGET_FINDER_H_ |