blob: 35f2af3fb6bd6b8b1aa7c3dcc42cb84076ea35d2 [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;
Austin Schuh9e5ca2b2019-03-06 20:43:17 -080062 viewer_->SetScale(2.0);
Parker Schuh2a1447c2019-02-17 00:25:29 -080063 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) {
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800153 results.emplace_back(finder_.ProcessTargetToResult(target, draw_raw_IR_));
Parker Schuh2a1447c2019-02-17 00:25:29 -0800154 if (draw_raw_IR_) DrawResult(results.back(), {255, 128, 0});
155 }
156
157 // Check that our current results match possible solutions.
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800158 results = finder_.FilterResults(results, 0);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800159 if (draw_results_) {
160 for (const IntermediateResult &res : results) {
Parker Schuh2a1447c2019-02-17 00:25:29 -0800161 DrawTarget(res, {0, 255, 0});
162 }
163 }
164
165 // If the target list is not empty then we found a target.
166 return !results.empty();
167 }
168
169 std::function<void(uint32_t)> RegisterKeyPress() override {
170 return [this](uint32_t key) {
171 (void)key;
172 if (key == 'z') {
173 draw_results_ = !draw_results_;
174 } else if (key == 'x') {
175 draw_raw_IR_ = !draw_raw_IR_;
176 } else if (key == 'c') {
177 draw_raw_target_ = !draw_raw_target_;
178 } else if (key == 'v') {
179 draw_components_ = !draw_components_;
180 } else if (key == 'b') {
181 draw_raw_poly_ = !draw_raw_poly_;
182 } else if (key == 'n') {
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800183 draw_contours_ = !draw_contours_;
184 } else if (key == 'm') {
Parker Schuh2a1447c2019-02-17 00:25:29 -0800185 draw_select_blob_ = !draw_select_blob_;
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800186 } else if (key == 'h') {
187 printf("Key Mappings:\n");
188 printf(" z: Toggle drawing final target pose.\n");
189 printf(" x: Toggle drawing re-projected targets and print solver results.\n");
190 printf(" c: Toggle drawing proposed target groupings.\n");
191 printf(" v: Toggle drawing ordered target components.\n");
192 printf(" b: Toggle drawing proposed target components.\n");
193 printf(" n: Toggle drawing countours before and after warping.\n");
194 printf(" m: Toggle drawing raw blob data (may need to change image to toggle a redraw).\n");
195 printf(" h: Print this message.\n");
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800196 printf(" a: May log camera image to /tmp/debug_viewer_jpeg_<#>.yuyv\n");
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800197 printf(" q: Exit the application.\n");
Parker Schuh2a1447c2019-02-17 00:25:29 -0800198 } else if (key == 'q') {
199 printf("User requested shutdown.\n");
200 exit(0);
201 }
202 HandleBlobs(imgs_last_, fmt_last_);
203 viewer_->Redraw();
204 };
205 }
206
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800207 void DrawContour(ContourNode *contour, PixelRef color) {
208 if (viewer_) {
209 for (ContourNode *node = contour; node->next != contour;) {
210 Vector<2> a(node->pt.x, node->pt.y);
211 Vector<2> b(node->next->pt.x, node->next->pt.y);
212 overlay_.AddLine(a, b, color);
213 node = node->next;
214 }
215 }
216 }
217
Parker Schuh2a1447c2019-02-17 00:25:29 -0800218 void DrawComponent(const TargetComponent &comp, PixelRef top_color,
219 PixelRef bot_color, PixelRef in_color,
220 PixelRef out_color) {
221 overlay_.AddLine(comp.top, comp.inside, top_color);
222 overlay_.AddLine(comp.bottom, comp.outside, bot_color);
223
224 overlay_.AddLine(comp.bottom, comp.inside, in_color);
225 overlay_.AddLine(comp.top, comp.outside, out_color);
226 }
227
228 void DrawTarget(const Target &target) {
229 Vector<2> leftTop = (target.left.top + target.left.inside) * 0.5;
230 Vector<2> rightTop = (target.right.top + target.right.inside) * 0.5;
231 overlay_.AddLine(leftTop, rightTop, {255, 215, 0});
232
233 Vector<2> leftBot = (target.left.bottom + target.left.outside) * 0.5;
234 Vector<2> rightBot = (target.right.bottom + target.right.outside) * 0.5;
235 overlay_.AddLine(leftBot, rightBot, {255, 215, 0});
236
237 overlay_.AddLine(leftTop, leftBot, {255, 215, 0});
238 overlay_.AddLine(rightTop, rightBot, {255, 215, 0});
239 }
240
241 void DrawResult(const IntermediateResult &result, PixelRef color) {
242 Target target =
243 Project(finder_.GetTemplateTarget(), intrinsics(), result.extrinsics);
244 DrawComponent(target.left, color, color, color, color);
245 DrawComponent(target.right, color, color, color, color);
246 }
247
248 void DrawTarget(const IntermediateResult &result, PixelRef color) {
249 Target target =
250 Project(finder_.GetTemplateTarget(), intrinsics(), result.extrinsics);
251 Segment<2> leftAx((target.left.top + target.left.inside) * 0.5,
252 (target.left.bottom + target.left.outside) * 0.5);
253 leftAx.Set(leftAx.A() * 0.9 + leftAx.B() * 0.1,
254 leftAx.B() * 0.9 + leftAx.A() * 0.1);
255 overlay_.AddLine(leftAx, color);
256
257 Segment<2> rightAx((target.right.top + target.right.inside) * 0.5,
258 (target.right.bottom + target.right.outside) * 0.5);
259 rightAx.Set(rightAx.A() * 0.9 + rightAx.B() * 0.1,
260 rightAx.B() * 0.9 + rightAx.A() * 0.1);
261 overlay_.AddLine(rightAx, color);
262
263 overlay_.AddLine(leftAx.A(), rightAx.A(), color);
264 overlay_.AddLine(leftAx.B(), rightAx.B(), color);
265 Vector<3> p1(0.0, 0.0, 100.0);
266
267 Vector<3> p2 =
268 Rotate(intrinsics().mount_angle, result.extrinsics.r1, 0.0, p1);
269 Vector<2> p3(p2.x(), p2.y());
270 overlay_.AddLine(leftAx.A(), p3 + leftAx.A(), {0, 255, 0});
271 overlay_.AddLine(leftAx.B(), p3 + leftAx.B(), {0, 255, 0});
272 overlay_.AddLine(rightAx.A(), p3 + rightAx.A(), {0, 255, 0});
273 overlay_.AddLine(rightAx.B(), p3 + rightAx.B(), {0, 255, 0});
274
275 overlay_.AddLine(p3 + leftAx.A(), p3 + leftAx.B(), {0, 255, 0});
276 overlay_.AddLine(p3 + leftAx.A(), p3 + rightAx.A(), {0, 255, 0});
277 overlay_.AddLine(p3 + rightAx.A(), p3 + rightAx.B(), {0, 255, 0});
278 overlay_.AddLine(p3 + leftAx.B(), p3 + rightAx.B(), {0, 255, 0});
279 }
280
281 const IntrinsicParams &intrinsics() const { return finder_.intrinsics(); }
282
283 private:
284 // implementation of the filter pipeline.
285 TargetFinder finder_;
286 aos::vision::BlobStreamViewer *viewer_ = nullptr;
287 aos::vision::PixelLinesOverlay overlay_;
288 std::vector<aos::vision::OverlayBase *> overlays_;
289 BlobList imgs_last_;
290 ImageFormat fmt_last_;
291 bool draw_select_blob_ = false;
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800292 bool draw_contours_ = false;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800293 bool draw_raw_poly_ = false;
294 bool draw_components_ = false;
295 bool draw_raw_target_ = false;
296 bool draw_raw_IR_ = false;
297 bool draw_results_ = true;
298};
299
300} // namespace vision
301} // namespace y2017
302
303int main(int argc, char **argv) {
304 y2019::vision::FilterHarness filter_harness;
305 aos::vision::DebugFrameworkMain(argc, argv, &filter_harness,
306 aos::vision::CameraParams());
307}