blob: 8ef67faea9c6959c286b14c8c3a44fd107747cdc [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"
Austin Schuh75921532019-03-09 18:46:34 -080011#include "gflags/gflags.h"
Parker Schuh2a1447c2019-02-17 00:25:29 -080012
13using aos::vision::ImageRange;
14using aos::vision::ImageFormat;
15using aos::vision::RangeImage;
16using aos::vision::AnalysisAllocator;
17using aos::vision::BlobList;
18using aos::vision::Vector;
19using aos::vision::Segment;
20using aos::vision::PixelRef;
21
Austin Schuh75921532019-03-09 18:46:34 -080022DEFINE_int32(camera, 10, "The camera to use the intrinsics for");
23
Parker Schuh2a1447c2019-02-17 00:25:29 -080024namespace y2019 {
25namespace vision {
26
27std::vector<PixelRef> GetNColors(size_t num_colors) {
28 std::vector<PixelRef> colors;
29 for (size_t i = 0; i < num_colors; ++i) {
30 int quadrent = i * 6 / num_colors;
31 uint8_t alpha = (256 * 6 * i - quadrent * num_colors * 256) / num_colors;
32 uint8_t inv_alpha = 255 - alpha;
33 switch (quadrent) {
34 case 0:
35 colors.push_back(PixelRef{255, alpha, 0});
36 break;
37 case 1:
38 colors.push_back(PixelRef{inv_alpha, 255, 0});
39 break;
40 case 2:
41 colors.push_back(PixelRef{0, 255, alpha});
42 break;
43 case 3:
44 colors.push_back(PixelRef{0, inv_alpha, 255});
45 break;
46 case 4:
47 colors.push_back(PixelRef{alpha, 0, 255});
48 break;
49 case 5:
50 colors.push_back(PixelRef{255, 0, inv_alpha});
51 break;
52 }
53 }
54 return colors;
55}
56
57class FilterHarness : public aos::vision::FilterHarness {
58 public:
Austin Schuh75921532019-03-09 18:46:34 -080059 FilterHarness() {
60 *(target_finder_.mutable_intrinsics()) = GetCamera(FLAGS_camera)->intrinsics;
61 }
Parker Schuh2a1447c2019-02-17 00:25:29 -080062 aos::vision::RangeImage Threshold(aos::vision::ImagePtr image) override {
Austin Schuh75921532019-03-09 18:46:34 -080063 return target_finder_.Threshold(image);
Parker Schuh2a1447c2019-02-17 00:25:29 -080064 }
65
66 void InstallViewer(aos::vision::BlobStreamViewer *viewer) override {
67 viewer_ = viewer;
Austin Schuh9e5ca2b2019-03-06 20:43:17 -080068 viewer_->SetScale(2.0);
Parker Schuh2a1447c2019-02-17 00:25:29 -080069 overlays_.push_back(&overlay_);
Austin Schuh75921532019-03-09 18:46:34 -080070 overlays_.push_back(target_finder_.GetOverlay());
Parker Schuh2a1447c2019-02-17 00:25:29 -080071 viewer_->view()->SetOverlays(&overlays_);
72 }
73
74 void DrawBlob(const RangeImage &blob, PixelRef color) {
75 if (viewer_) {
76 BlobList list;
77 list.push_back(blob);
78 viewer_->DrawBlobList(list, color);
79 }
80 }
81
82 bool HandleBlobs(BlobList imgs, ImageFormat fmt) override {
83 imgs_last_ = imgs;
84 fmt_last_ = fmt;
85 // reset for next drawing cycle
86 for (auto &overlay : overlays_) {
87 overlay->Reset();
88 }
89
90 if (draw_select_blob_ || draw_raw_poly_ || draw_components_ ||
91 draw_raw_target_ || draw_raw_IR_ || draw_results_) {
92 printf("_____ New Image _____\n");
93 }
94
95 // Remove bad blobs.
Austin Schuh75921532019-03-09 18:46:34 -080096 target_finder_.PreFilter(&imgs);
Parker Schuh2a1447c2019-02-17 00:25:29 -080097
98 // Find polygons from blobs.
Austin Schuh6e56faf2019-03-10 14:04:57 -070099 std::vector<Polygon> raw_polys;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800100 for (const RangeImage &blob : imgs) {
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800101 // Convert blobs to contours in the corrected space.
Austin Schuh75921532019-03-09 18:46:34 -0800102 ContourNode *contour = target_finder_.GetContour(blob);
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800103 if (draw_contours_) {
104 DrawContour(contour, {255, 0, 0});
105 }
Austin Schuh6e56faf2019-03-10 14:04:57 -0700106 ::std::vector<::Eigen::Vector2f> unwarped_contour =
Austin Schuh75921532019-03-09 18:46:34 -0800107 target_finder_.UnWarpContour(contour);
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800108 if (draw_contours_) {
Austin Schuhe5015972019-03-09 17:47:34 -0800109 DrawContour(unwarped_contour, {0, 0, 255});
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800110 }
111
112 // Process to polygons.
Austin Schuh6e56faf2019-03-10 14:04:57 -0700113 const Polygon polygon = target_finder_.FindPolygon(
114 ::std::move(unwarped_contour), draw_raw_poly_);
115 if (polygon.segments.empty()) {
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800116 if (!draw_contours_) {
117 DrawBlob(blob, {255, 0, 0});
118 }
Parker Schuh2a1447c2019-02-17 00:25:29 -0800119 } else {
120 raw_polys.push_back(polygon);
121 if (draw_select_blob_) {
122 DrawBlob(blob, {0, 0, 255});
123 }
124 if (draw_raw_poly_) {
Austin Schuh6e56faf2019-03-10 14:04:57 -0700125 std::vector<PixelRef> colors = GetNColors(polygon.segments.size());
Parker Schuh2a1447c2019-02-17 00:25:29 -0800126 std::vector<Vector<2>> corners;
Austin Schuh6e56faf2019-03-10 14:04:57 -0700127 for (size_t i = 0; i < polygon.segments.size(); ++i) {
128 corners.push_back(polygon.segments[i].Intersect(
129 polygon.segments[(i + 1) % polygon.segments.size()]));
Parker Schuh2a1447c2019-02-17 00:25:29 -0800130 }
131
Austin Schuh6e56faf2019-03-10 14:04:57 -0700132 for (size_t i = 0; i < polygon.segments.size(); ++i) {
133 overlay_.AddLine(corners[i],
134 corners[(i + 1) % polygon.segments.size()],
Austin Schuhe5015972019-03-09 17:47:34 -0800135 colors[i]);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800136 }
137 }
138 }
139 }
140
141 // Calculate each component side of a possible target.
142 std::vector<TargetComponent> target_component_list =
Austin Schuh32ffac22019-03-09 22:42:02 -0800143 target_finder_.FillTargetComponentList(raw_polys, draw_components_);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800144 if (draw_components_) {
145 for (const TargetComponent &comp : target_component_list) {
146 DrawComponent(comp, {0, 255, 255}, {0, 255, 255}, {255, 0, 0},
147 {0, 0, 255});
148 }
149 }
150
151 // Put the compenents together into targets.
Austin Schuh75921532019-03-09 18:46:34 -0800152 std::vector<Target> target_list = target_finder_.FindTargetsFromComponents(
Parker Schuh2a1447c2019-02-17 00:25:29 -0800153 target_component_list, draw_raw_target_);
154 if (draw_raw_target_) {
155 for (const Target &target : target_list) {
156 DrawTarget(target);
157 }
158 }
159
160 // Use the solver to generate an intermediate version of our results.
161 std::vector<IntermediateResult> results;
162 for (const Target &target : target_list) {
Austin Schuh75921532019-03-09 18:46:34 -0800163 results.emplace_back(
164 target_finder_.ProcessTargetToResult(target, draw_raw_IR_));
Parker Schuh2a1447c2019-02-17 00:25:29 -0800165 if (draw_raw_IR_) DrawResult(results.back(), {255, 128, 0});
166 }
167
168 // Check that our current results match possible solutions.
Alex Perrybac3d3f2019-03-10 14:26:51 -0700169 results = target_finder_.FilterResults(results, 0, draw_results_);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800170 if (draw_results_) {
171 for (const IntermediateResult &res : results) {
Parker Schuh2a1447c2019-02-17 00:25:29 -0800172 DrawTarget(res, {0, 255, 0});
173 }
174 }
175
176 // If the target list is not empty then we found a target.
177 return !results.empty();
178 }
179
180 std::function<void(uint32_t)> RegisterKeyPress() override {
181 return [this](uint32_t key) {
182 (void)key;
183 if (key == 'z') {
184 draw_results_ = !draw_results_;
185 } else if (key == 'x') {
186 draw_raw_IR_ = !draw_raw_IR_;
187 } else if (key == 'c') {
188 draw_raw_target_ = !draw_raw_target_;
189 } else if (key == 'v') {
190 draw_components_ = !draw_components_;
191 } else if (key == 'b') {
192 draw_raw_poly_ = !draw_raw_poly_;
193 } else if (key == 'n') {
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800194 draw_contours_ = !draw_contours_;
195 } else if (key == 'm') {
Parker Schuh2a1447c2019-02-17 00:25:29 -0800196 draw_select_blob_ = !draw_select_blob_;
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800197 } else if (key == 'h') {
198 printf("Key Mappings:\n");
199 printf(" z: Toggle drawing final target pose.\n");
200 printf(" x: Toggle drawing re-projected targets and print solver results.\n");
201 printf(" c: Toggle drawing proposed target groupings.\n");
202 printf(" v: Toggle drawing ordered target components.\n");
203 printf(" b: Toggle drawing proposed target components.\n");
204 printf(" n: Toggle drawing countours before and after warping.\n");
205 printf(" m: Toggle drawing raw blob data (may need to change image to toggle a redraw).\n");
206 printf(" h: Print this message.\n");
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800207 printf(" a: May log camera image to /tmp/debug_viewer_jpeg_<#>.yuyv\n");
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800208 printf(" q: Exit the application.\n");
Parker Schuh2a1447c2019-02-17 00:25:29 -0800209 } else if (key == 'q') {
210 printf("User requested shutdown.\n");
211 exit(0);
212 }
213 HandleBlobs(imgs_last_, fmt_last_);
214 viewer_->Redraw();
215 };
216 }
217
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800218 void DrawContour(ContourNode *contour, PixelRef color) {
219 if (viewer_) {
220 for (ContourNode *node = contour; node->next != contour;) {
221 Vector<2> a(node->pt.x, node->pt.y);
222 Vector<2> b(node->next->pt.x, node->next->pt.y);
223 overlay_.AddLine(a, b, color);
224 node = node->next;
225 }
226 }
227 }
228
Austin Schuhe5015972019-03-09 17:47:34 -0800229 void DrawContour(const ::std::vector<::Eigen::Vector2f> &contour,
230 PixelRef color) {
231 if (viewer_) {
232 for (size_t i = 0; i < contour.size(); ++i) {
233 Vector<2> a(contour[i].x(), contour[i].y());
234 Vector<2> b(contour[(i + 1) % contour.size()].x(),
235 contour[(i + 1) % contour.size()].y());
236 overlay_.AddLine(a, b, color);
237 }
238 }
239 }
240
Parker Schuh2a1447c2019-02-17 00:25:29 -0800241 void DrawComponent(const TargetComponent &comp, PixelRef top_color,
242 PixelRef bot_color, PixelRef in_color,
243 PixelRef out_color) {
244 overlay_.AddLine(comp.top, comp.inside, top_color);
245 overlay_.AddLine(comp.bottom, comp.outside, bot_color);
246
247 overlay_.AddLine(comp.bottom, comp.inside, in_color);
248 overlay_.AddLine(comp.top, comp.outside, out_color);
249 }
250
251 void DrawTarget(const Target &target) {
252 Vector<2> leftTop = (target.left.top + target.left.inside) * 0.5;
253 Vector<2> rightTop = (target.right.top + target.right.inside) * 0.5;
254 overlay_.AddLine(leftTop, rightTop, {255, 215, 0});
255
256 Vector<2> leftBot = (target.left.bottom + target.left.outside) * 0.5;
257 Vector<2> rightBot = (target.right.bottom + target.right.outside) * 0.5;
258 overlay_.AddLine(leftBot, rightBot, {255, 215, 0});
259
260 overlay_.AddLine(leftTop, leftBot, {255, 215, 0});
261 overlay_.AddLine(rightTop, rightBot, {255, 215, 0});
262 }
263
264 void DrawResult(const IntermediateResult &result, PixelRef color) {
Austin Schuh75921532019-03-09 18:46:34 -0800265 Target target = Project(target_finder_.GetTemplateTarget(), intrinsics(),
266 result.extrinsics);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800267 DrawComponent(target.left, color, color, color, color);
268 DrawComponent(target.right, color, color, color, color);
269 }
270
271 void DrawTarget(const IntermediateResult &result, PixelRef color) {
Austin Schuh75921532019-03-09 18:46:34 -0800272 Target target = Project(target_finder_.GetTemplateTarget(), intrinsics(),
273 result.extrinsics);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800274 Segment<2> leftAx((target.left.top + target.left.inside) * 0.5,
275 (target.left.bottom + target.left.outside) * 0.5);
276 leftAx.Set(leftAx.A() * 0.9 + leftAx.B() * 0.1,
277 leftAx.B() * 0.9 + leftAx.A() * 0.1);
278 overlay_.AddLine(leftAx, color);
279
280 Segment<2> rightAx((target.right.top + target.right.inside) * 0.5,
281 (target.right.bottom + target.right.outside) * 0.5);
282 rightAx.Set(rightAx.A() * 0.9 + rightAx.B() * 0.1,
283 rightAx.B() * 0.9 + rightAx.A() * 0.1);
284 overlay_.AddLine(rightAx, color);
285
286 overlay_.AddLine(leftAx.A(), rightAx.A(), color);
287 overlay_.AddLine(leftAx.B(), rightAx.B(), color);
288 Vector<3> p1(0.0, 0.0, 100.0);
289
290 Vector<3> p2 =
291 Rotate(intrinsics().mount_angle, result.extrinsics.r1, 0.0, p1);
292 Vector<2> p3(p2.x(), p2.y());
293 overlay_.AddLine(leftAx.A(), p3 + leftAx.A(), {0, 255, 0});
294 overlay_.AddLine(leftAx.B(), p3 + leftAx.B(), {0, 255, 0});
295 overlay_.AddLine(rightAx.A(), p3 + rightAx.A(), {0, 255, 0});
296 overlay_.AddLine(rightAx.B(), p3 + rightAx.B(), {0, 255, 0});
297
298 overlay_.AddLine(p3 + leftAx.A(), p3 + leftAx.B(), {0, 255, 0});
299 overlay_.AddLine(p3 + leftAx.A(), p3 + rightAx.A(), {0, 255, 0});
300 overlay_.AddLine(p3 + rightAx.A(), p3 + rightAx.B(), {0, 255, 0});
301 overlay_.AddLine(p3 + leftAx.B(), p3 + rightAx.B(), {0, 255, 0});
302 }
303
Austin Schuh75921532019-03-09 18:46:34 -0800304 const IntrinsicParams &intrinsics() const {
305 return target_finder_.intrinsics();
306 }
Parker Schuh2a1447c2019-02-17 00:25:29 -0800307
308 private:
309 // implementation of the filter pipeline.
Austin Schuh75921532019-03-09 18:46:34 -0800310 TargetFinder target_finder_;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800311 aos::vision::BlobStreamViewer *viewer_ = nullptr;
312 aos::vision::PixelLinesOverlay overlay_;
313 std::vector<aos::vision::OverlayBase *> overlays_;
314 BlobList imgs_last_;
315 ImageFormat fmt_last_;
316 bool draw_select_blob_ = false;
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800317 bool draw_contours_ = false;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800318 bool draw_raw_poly_ = false;
319 bool draw_components_ = false;
320 bool draw_raw_target_ = false;
321 bool draw_raw_IR_ = false;
322 bool draw_results_ = true;
323};
324
325} // namespace vision
326} // namespace y2017
327
328int main(int argc, char **argv) {
329 y2019::vision::FilterHarness filter_harness;
Austin Schuh75921532019-03-09 18:46:34 -0800330 ::gflags::ParseCommandLineFlags(&argc, &argv, true);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800331 aos::vision::DebugFrameworkMain(argc, argv, &filter_harness,
332 aos::vision::CameraParams());
333}