blob: 533dcf6f32e9fdf6fe9f410082add1cd4477ecac [file] [log] [blame]
Parker Schuh2a1447c2019-02-17 00:25:29 -08001#include <Eigen/Dense>
2#include <iostream>
3
4#include "y2019/vision/target_finder.h"
5
6#include "aos/vision/blob/move_scale.h"
7#include "aos/vision/blob/stream_view.h"
8#include "aos/vision/blob/transpose.h"
9#include "aos/vision/debug/debug_framework.h"
10#include "aos/vision/math/vector.h"
11
12using aos::vision::ImageRange;
13using aos::vision::ImageFormat;
14using aos::vision::RangeImage;
15using aos::vision::AnalysisAllocator;
16using aos::vision::BlobList;
17using aos::vision::Vector;
18using aos::vision::Segment;
19using aos::vision::PixelRef;
20
21namespace y2019 {
22namespace vision {
23
24std::vector<PixelRef> GetNColors(size_t num_colors) {
25 std::vector<PixelRef> colors;
26 for (size_t i = 0; i < num_colors; ++i) {
27 int quadrent = i * 6 / num_colors;
28 uint8_t alpha = (256 * 6 * i - quadrent * num_colors * 256) / num_colors;
29 uint8_t inv_alpha = 255 - alpha;
30 switch (quadrent) {
31 case 0:
32 colors.push_back(PixelRef{255, alpha, 0});
33 break;
34 case 1:
35 colors.push_back(PixelRef{inv_alpha, 255, 0});
36 break;
37 case 2:
38 colors.push_back(PixelRef{0, 255, alpha});
39 break;
40 case 3:
41 colors.push_back(PixelRef{0, inv_alpha, 255});
42 break;
43 case 4:
44 colors.push_back(PixelRef{alpha, 0, 255});
45 break;
46 case 5:
47 colors.push_back(PixelRef{255, 0, inv_alpha});
48 break;
49 }
50 }
51 return colors;
52}
53
54class FilterHarness : public aos::vision::FilterHarness {
55 public:
56 aos::vision::RangeImage Threshold(aos::vision::ImagePtr image) override {
57 return finder_.Threshold(image);
58 }
59
60 void InstallViewer(aos::vision::BlobStreamViewer *viewer) override {
61 viewer_ = viewer;
62 viewer_->SetScale(0.75);
63 overlays_.push_back(&overlay_);
64 overlays_.push_back(finder_.GetOverlay());
65 viewer_->view()->SetOverlays(&overlays_);
66 }
67
68 void DrawBlob(const RangeImage &blob, PixelRef color) {
69 if (viewer_) {
70 BlobList list;
71 list.push_back(blob);
72 viewer_->DrawBlobList(list, color);
73 }
74 }
75
76 bool HandleBlobs(BlobList imgs, ImageFormat fmt) override {
77 imgs_last_ = imgs;
78 fmt_last_ = fmt;
79 // reset for next drawing cycle
80 for (auto &overlay : overlays_) {
81 overlay->Reset();
82 }
83
84 if (draw_select_blob_ || draw_raw_poly_ || draw_components_ ||
85 draw_raw_target_ || draw_raw_IR_ || draw_results_) {
86 printf("_____ New Image _____\n");
87 }
88
89 // Remove bad blobs.
90 finder_.PreFilter(&imgs);
91
92 // Find polygons from blobs.
93 std::vector<std::vector<Segment<2>>> raw_polys;
94 for (const RangeImage &blob : imgs) {
Ben Fredricksonf7b68522019-03-02 21:19:42 -080095 // Convert blobs to contours in the corrected space.
96 ContourNode* contour = finder_.GetContour(blob);
97 if (draw_contours_) {
98 DrawContour(contour, {255, 0, 0});
99 }
100 finder_.UnWarpContour(contour);
101 if (draw_contours_) {
102 DrawContour(contour, {0, 0, 255});
103 }
104
105 // Process to polygons.
Parker Schuh2a1447c2019-02-17 00:25:29 -0800106 std::vector<Segment<2>> polygon =
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800107 finder_.FillPolygon(contour, draw_raw_poly_);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800108 if (polygon.empty()) {
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800109 if (!draw_contours_) {
110 DrawBlob(blob, {255, 0, 0});
111 }
Parker Schuh2a1447c2019-02-17 00:25:29 -0800112 } else {
113 raw_polys.push_back(polygon);
114 if (draw_select_blob_) {
115 DrawBlob(blob, {0, 0, 255});
116 }
117 if (draw_raw_poly_) {
118 std::vector<PixelRef> colors = GetNColors(polygon.size());
119 std::vector<Vector<2>> corners;
120 for (size_t i = 0; i < 4; ++i) {
121 corners.push_back(polygon[i].Intersect(polygon[(i + 1) % 4]));
122 }
123
124 for (size_t i = 0; i < 4; ++i) {
125 overlay_.AddLine(corners[i], corners[(i + 1) % 4], colors[i]);
126 }
127 }
128 }
129 }
130
131 // Calculate each component side of a possible target.
132 std::vector<TargetComponent> target_component_list =
133 finder_.FillTargetComponentList(raw_polys);
134 if (draw_components_) {
135 for (const TargetComponent &comp : target_component_list) {
136 DrawComponent(comp, {0, 255, 255}, {0, 255, 255}, {255, 0, 0},
137 {0, 0, 255});
138 }
139 }
140
141 // Put the compenents together into targets.
142 std::vector<Target> target_list = finder_.FindTargetsFromComponents(
143 target_component_list, draw_raw_target_);
144 if (draw_raw_target_) {
145 for (const Target &target : target_list) {
146 DrawTarget(target);
147 }
148 }
149
150 // Use the solver to generate an intermediate version of our results.
151 std::vector<IntermediateResult> results;
152 for (const Target &target : target_list) {
153 results.emplace_back(finder_.ProcessTargetToResult(target, true));
154 if (draw_raw_IR_) DrawResult(results.back(), {255, 128, 0});
155 }
156
157 // Check that our current results match possible solutions.
158 results = finder_.FilterResults(results);
159 if (draw_results_) {
160 for (const IntermediateResult &res : results) {
161 DrawResult(res, {0, 255, 0});
162 DrawTarget(res, {0, 255, 0});
163 }
164 }
165
166 // If the target list is not empty then we found a target.
167 return !results.empty();
168 }
169
170 std::function<void(uint32_t)> RegisterKeyPress() override {
171 return [this](uint32_t key) {
172 (void)key;
173 if (key == 'z') {
174 draw_results_ = !draw_results_;
175 } else if (key == 'x') {
176 draw_raw_IR_ = !draw_raw_IR_;
177 } else if (key == 'c') {
178 draw_raw_target_ = !draw_raw_target_;
179 } else if (key == 'v') {
180 draw_components_ = !draw_components_;
181 } else if (key == 'b') {
182 draw_raw_poly_ = !draw_raw_poly_;
183 } else if (key == 'n') {
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800184 draw_contours_ = !draw_contours_;
185 } else if (key == 'm') {
Parker Schuh2a1447c2019-02-17 00:25:29 -0800186 draw_select_blob_ = !draw_select_blob_;
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800187 } else if (key == 'h') {
188 printf("Key Mappings:\n");
189 printf(" z: Toggle drawing final target pose.\n");
190 printf(" x: Toggle drawing re-projected targets and print solver results.\n");
191 printf(" c: Toggle drawing proposed target groupings.\n");
192 printf(" v: Toggle drawing ordered target components.\n");
193 printf(" b: Toggle drawing proposed target components.\n");
194 printf(" n: Toggle drawing countours before and after warping.\n");
195 printf(" m: Toggle drawing raw blob data (may need to change image to toggle a redraw).\n");
196 printf(" h: Print this message.\n");
Ben Fredricksonec575822019-03-02 22:03:20 -0800197 printf(" a: May log camera image to /tmp/debug_viewer_jpeg_<#>.yuyv"
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800198 printf(" q: Exit the application.\n");
Parker Schuh2a1447c2019-02-17 00:25:29 -0800199 } else if (key == 'q') {
200 printf("User requested shutdown.\n");
201 exit(0);
202 }
203 HandleBlobs(imgs_last_, fmt_last_);
204 viewer_->Redraw();
205 };
206 }
207
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800208 void DrawContour(ContourNode *contour, PixelRef color) {
209 if (viewer_) {
210 for (ContourNode *node = contour; node->next != contour;) {
211 Vector<2> a(node->pt.x, node->pt.y);
212 Vector<2> b(node->next->pt.x, node->next->pt.y);
213 overlay_.AddLine(a, b, color);
214 node = node->next;
215 }
216 }
217 }
218
Parker Schuh2a1447c2019-02-17 00:25:29 -0800219 void DrawComponent(const TargetComponent &comp, PixelRef top_color,
220 PixelRef bot_color, PixelRef in_color,
221 PixelRef out_color) {
222 overlay_.AddLine(comp.top, comp.inside, top_color);
223 overlay_.AddLine(comp.bottom, comp.outside, bot_color);
224
225 overlay_.AddLine(comp.bottom, comp.inside, in_color);
226 overlay_.AddLine(comp.top, comp.outside, out_color);
227 }
228
229 void DrawTarget(const Target &target) {
230 Vector<2> leftTop = (target.left.top + target.left.inside) * 0.5;
231 Vector<2> rightTop = (target.right.top + target.right.inside) * 0.5;
232 overlay_.AddLine(leftTop, rightTop, {255, 215, 0});
233
234 Vector<2> leftBot = (target.left.bottom + target.left.outside) * 0.5;
235 Vector<2> rightBot = (target.right.bottom + target.right.outside) * 0.5;
236 overlay_.AddLine(leftBot, rightBot, {255, 215, 0});
237
238 overlay_.AddLine(leftTop, leftBot, {255, 215, 0});
239 overlay_.AddLine(rightTop, rightBot, {255, 215, 0});
240 }
241
242 void DrawResult(const IntermediateResult &result, PixelRef color) {
243 Target target =
244 Project(finder_.GetTemplateTarget(), intrinsics(), result.extrinsics);
245 DrawComponent(target.left, color, color, color, color);
246 DrawComponent(target.right, color, color, color, color);
247 }
248
249 void DrawTarget(const IntermediateResult &result, PixelRef color) {
250 Target target =
251 Project(finder_.GetTemplateTarget(), intrinsics(), result.extrinsics);
252 Segment<2> leftAx((target.left.top + target.left.inside) * 0.5,
253 (target.left.bottom + target.left.outside) * 0.5);
254 leftAx.Set(leftAx.A() * 0.9 + leftAx.B() * 0.1,
255 leftAx.B() * 0.9 + leftAx.A() * 0.1);
256 overlay_.AddLine(leftAx, color);
257
258 Segment<2> rightAx((target.right.top + target.right.inside) * 0.5,
259 (target.right.bottom + target.right.outside) * 0.5);
260 rightAx.Set(rightAx.A() * 0.9 + rightAx.B() * 0.1,
261 rightAx.B() * 0.9 + rightAx.A() * 0.1);
262 overlay_.AddLine(rightAx, color);
263
264 overlay_.AddLine(leftAx.A(), rightAx.A(), color);
265 overlay_.AddLine(leftAx.B(), rightAx.B(), color);
266 Vector<3> p1(0.0, 0.0, 100.0);
267
268 Vector<3> p2 =
269 Rotate(intrinsics().mount_angle, result.extrinsics.r1, 0.0, p1);
270 Vector<2> p3(p2.x(), p2.y());
271 overlay_.AddLine(leftAx.A(), p3 + leftAx.A(), {0, 255, 0});
272 overlay_.AddLine(leftAx.B(), p3 + leftAx.B(), {0, 255, 0});
273 overlay_.AddLine(rightAx.A(), p3 + rightAx.A(), {0, 255, 0});
274 overlay_.AddLine(rightAx.B(), p3 + rightAx.B(), {0, 255, 0});
275
276 overlay_.AddLine(p3 + leftAx.A(), p3 + leftAx.B(), {0, 255, 0});
277 overlay_.AddLine(p3 + leftAx.A(), p3 + rightAx.A(), {0, 255, 0});
278 overlay_.AddLine(p3 + rightAx.A(), p3 + rightAx.B(), {0, 255, 0});
279 overlay_.AddLine(p3 + leftAx.B(), p3 + rightAx.B(), {0, 255, 0});
280 }
281
282 const IntrinsicParams &intrinsics() const { return finder_.intrinsics(); }
283
284 private:
285 // implementation of the filter pipeline.
286 TargetFinder finder_;
287 aos::vision::BlobStreamViewer *viewer_ = nullptr;
288 aos::vision::PixelLinesOverlay overlay_;
289 std::vector<aos::vision::OverlayBase *> overlays_;
290 BlobList imgs_last_;
291 ImageFormat fmt_last_;
292 bool draw_select_blob_ = false;
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800293 bool draw_contours_ = false;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800294 bool draw_raw_poly_ = false;
295 bool draw_components_ = false;
296 bool draw_raw_target_ = false;
297 bool draw_raw_IR_ = false;
298 bool draw_results_ = true;
299};
300
301} // namespace vision
302} // namespace y2017
303
304int main(int argc, char **argv) {
305 y2019::vision::FilterHarness filter_harness;
306 aos::vision::DebugFrameworkMain(argc, argv, &filter_harness,
307 aos::vision::CameraParams());
308}