blob: b2ad1be2af56a1799351e344c5ea550995b1dad8 [file] [log] [blame]
Parker Schuh2a1447c2019-02-17 00:25:29 -08001#include <iostream>
2
Philipp Schrader790cb542023-07-05 21:06:52 -07003#include "gflags/gflags.h"
4#include <Eigen/Dense>
Parker Schuh2a1447c2019-02-17 00:25:29 -08005
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"
Philipp Schrader790cb542023-07-05 21:06:52 -070011#include "y2019/vision/target_finder.h"
Parker Schuh2a1447c2019-02-17 00:25:29 -080012
Parker Schuh2a1447c2019-02-17 00:25:29 -080013using aos::vision::AnalysisAllocator;
14using aos::vision::BlobList;
Philipp Schrader790cb542023-07-05 21:06:52 -070015using aos::vision::ImageFormat;
16using aos::vision::ImageRange;
Parker Schuh2a1447c2019-02-17 00:25:29 -080017using aos::vision::PixelRef;
Philipp Schrader790cb542023-07-05 21:06:52 -070018using aos::vision::RangeImage;
19using aos::vision::Segment;
20using aos::vision::Vector;
Parker Schuh2a1447c2019-02-17 00:25:29 -080021
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:
Philipp Schrader790cb542023-07-05 21:06:52 -070059 FilterHarness() {
60 *(target_finder_.mutable_intrinsics()) =
61 GetCamera(FLAGS_camera)->intrinsics;
62 }
Parker Schuh2a1447c2019-02-17 00:25:29 -080063 aos::vision::RangeImage Threshold(aos::vision::ImagePtr image) override {
Austin Schuh75921532019-03-09 18:46:34 -080064 return target_finder_.Threshold(image);
Parker Schuh2a1447c2019-02-17 00:25:29 -080065 }
66
67 void InstallViewer(aos::vision::BlobStreamViewer *viewer) override {
68 viewer_ = viewer;
Austin Schuh9e5ca2b2019-03-06 20:43:17 -080069 viewer_->SetScale(2.0);
Parker Schuh2a1447c2019-02-17 00:25:29 -080070 overlays_.push_back(&overlay_);
Austin Schuh75921532019-03-09 18:46:34 -080071 overlays_.push_back(target_finder_.GetOverlay());
Parker Schuh2a1447c2019-02-17 00:25:29 -080072 viewer_->view()->SetOverlays(&overlays_);
73 }
74
75 void DrawBlob(const RangeImage &blob, PixelRef color) {
76 if (viewer_) {
77 BlobList list;
78 list.push_back(blob);
79 viewer_->DrawBlobList(list, color);
80 }
81 }
82
83 bool HandleBlobs(BlobList imgs, ImageFormat fmt) override {
Austin Schuhdc477af2019-03-28 10:31:18 -070084 const CameraGeometry camera_geometry = GetCamera(FLAGS_camera)->geometry;
Parker Schuh2a1447c2019-02-17 00:25:29 -080085 imgs_last_ = imgs;
86 fmt_last_ = fmt;
87 // reset for next drawing cycle
88 for (auto &overlay : overlays_) {
89 overlay->Reset();
90 }
91
92 if (draw_select_blob_ || draw_raw_poly_ || draw_components_ ||
93 draw_raw_target_ || draw_raw_IR_ || draw_results_) {
94 printf("_____ New Image _____\n");
95 }
96
Austin Schuhf71c3e62019-05-08 20:48:41 -070097 const int num_pixels = target_finder_.PixelCount(&imgs);
98 printf("Number pixels: %d\n", num_pixels);
99
Parker Schuh2a1447c2019-02-17 00:25:29 -0800100 // Remove bad blobs.
Austin Schuh75921532019-03-09 18:46:34 -0800101 target_finder_.PreFilter(&imgs);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800102
103 // Find polygons from blobs.
Austin Schuh7d2ef032019-03-10 14:59:34 -0700104 ::std::vector<Polygon> raw_polys;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800105 for (const RangeImage &blob : imgs) {
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800106 // Convert blobs to contours in the corrected space.
Austin Schuh75921532019-03-09 18:46:34 -0800107 ContourNode *contour = target_finder_.GetContour(blob);
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800108 if (draw_contours_) {
109 DrawContour(contour, {255, 0, 0});
110 }
Austin Schuh6e56faf2019-03-10 14:04:57 -0700111 ::std::vector<::Eigen::Vector2f> unwarped_contour =
Austin Schuh75921532019-03-09 18:46:34 -0800112 target_finder_.UnWarpContour(contour);
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800113 if (draw_contours_) {
Austin Schuhe5015972019-03-09 17:47:34 -0800114 DrawContour(unwarped_contour, {0, 0, 255});
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800115 }
116
117 // Process to polygons.
Austin Schuh6e56faf2019-03-10 14:04:57 -0700118 const Polygon polygon = target_finder_.FindPolygon(
119 ::std::move(unwarped_contour), draw_raw_poly_);
120 if (polygon.segments.empty()) {
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800121 if (!draw_contours_) {
122 DrawBlob(blob, {255, 0, 0});
123 }
Parker Schuh2a1447c2019-02-17 00:25:29 -0800124 } else {
125 raw_polys.push_back(polygon);
126 if (draw_select_blob_) {
127 DrawBlob(blob, {0, 0, 255});
128 }
129 if (draw_raw_poly_) {
Austin Schuh6e56faf2019-03-10 14:04:57 -0700130 std::vector<PixelRef> colors = GetNColors(polygon.segments.size());
Parker Schuh2a1447c2019-02-17 00:25:29 -0800131 std::vector<Vector<2>> corners;
Austin Schuh6e56faf2019-03-10 14:04:57 -0700132 for (size_t i = 0; i < polygon.segments.size(); ++i) {
133 corners.push_back(polygon.segments[i].Intersect(
134 polygon.segments[(i + 1) % polygon.segments.size()]));
Parker Schuh2a1447c2019-02-17 00:25:29 -0800135 }
136
Austin Schuh6e56faf2019-03-10 14:04:57 -0700137 for (size_t i = 0; i < polygon.segments.size(); ++i) {
138 overlay_.AddLine(corners[i],
139 corners[(i + 1) % polygon.segments.size()],
Austin Schuhe5015972019-03-09 17:47:34 -0800140 colors[i]);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800141 }
142 }
143 }
144 }
145
146 // Calculate each component side of a possible target.
147 std::vector<TargetComponent> target_component_list =
Austin Schuh32ffac22019-03-09 22:42:02 -0800148 target_finder_.FillTargetComponentList(raw_polys, draw_components_);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800149 if (draw_components_) {
Austin Schuh7d2ef032019-03-10 14:59:34 -0700150 for (const TargetComponent &component : target_component_list) {
151 DrawComponent(component, {0, 255, 255}, {0, 255, 255}, {255, 0, 0},
Parker Schuh2a1447c2019-02-17 00:25:29 -0800152 {0, 0, 255});
Austin Schuh7d2ef032019-03-10 14:59:34 -0700153 overlay_.DrawCross(component.bottom_point, 4, {128, 0, 255});
Parker Schuh2a1447c2019-02-17 00:25:29 -0800154 }
155 }
156
157 // Put the compenents together into targets.
Austin Schuh75921532019-03-09 18:46:34 -0800158 std::vector<Target> target_list = target_finder_.FindTargetsFromComponents(
Parker Schuh2a1447c2019-02-17 00:25:29 -0800159 target_component_list, draw_raw_target_);
160 if (draw_raw_target_) {
161 for (const Target &target : target_list) {
162 DrawTarget(target);
163 }
164 }
165
166 // Use the solver to generate an intermediate version of our results.
167 std::vector<IntermediateResult> results;
168 for (const Target &target : target_list) {
Austin Schuh75921532019-03-09 18:46:34 -0800169 results.emplace_back(
170 target_finder_.ProcessTargetToResult(target, draw_raw_IR_));
Austin Schuhe6cfbe32019-03-10 18:05:57 -0700171 if (draw_raw_IR_) {
172 IntermediateResult updatable_result = results.back();
173 target_finder_.MaybePickAndUpdateResult(&updatable_result,
174 draw_raw_IR_);
175 DrawResult(updatable_result, {255, 128, 0});
176 }
Parker Schuh2a1447c2019-02-17 00:25:29 -0800177 }
178
179 // Check that our current results match possible solutions.
Alex Perrybac3d3f2019-03-10 14:26:51 -0700180 results = target_finder_.FilterResults(results, 0, draw_results_);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800181 if (draw_results_) {
Austin Schuhdc477af2019-03-28 10:31:18 -0700182 for (const IntermediateResult &result : results) {
183 ::std::cout << "Found target x: "
184 << camera_geometry.location[0] +
185 ::std::cos(camera_geometry.heading +
186 result.extrinsics.r2) *
187 result.extrinsics.z
188 << ::std::endl;
189 ::std::cout << "Found target y: "
190 << camera_geometry.location[1] +
191 ::std::sin(camera_geometry.heading +
192 result.extrinsics.r2) *
193 result.extrinsics.z
194 << ::std::endl;
195 ::std::cout << "Found target z: "
196 << camera_geometry.location[2] + result.extrinsics.y
197 << ::std::endl;
198 DrawTarget(result, {0, 255, 0});
Parker Schuh2a1447c2019-02-17 00:25:29 -0800199 }
200 }
201
Alex Perry5b1e8e32019-04-07 13:25:31 -0700202 int desired_exposure;
Austin Schuhf71c3e62019-05-08 20:48:41 -0700203 if (target_finder_.TestExposure(results, num_pixels, &desired_exposure)) {
Alex Perry5b1e8e32019-04-07 13:25:31 -0700204 printf("Switching exposure to %d.\n", desired_exposure);
205 SetExposure(desired_exposure);
206 }
207
Parker Schuh2a1447c2019-02-17 00:25:29 -0800208 // If the target list is not empty then we found a target.
209 return !results.empty();
210 }
211
212 std::function<void(uint32_t)> RegisterKeyPress() override {
213 return [this](uint32_t key) {
214 (void)key;
215 if (key == 'z') {
216 draw_results_ = !draw_results_;
217 } else if (key == 'x') {
218 draw_raw_IR_ = !draw_raw_IR_;
219 } else if (key == 'c') {
220 draw_raw_target_ = !draw_raw_target_;
221 } else if (key == 'v') {
222 draw_components_ = !draw_components_;
223 } else if (key == 'b') {
224 draw_raw_poly_ = !draw_raw_poly_;
225 } else if (key == 'n') {
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800226 draw_contours_ = !draw_contours_;
227 } else if (key == 'm') {
Parker Schuh2a1447c2019-02-17 00:25:29 -0800228 draw_select_blob_ = !draw_select_blob_;
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800229 } else if (key == 'h') {
230 printf("Key Mappings:\n");
231 printf(" z: Toggle drawing final target pose.\n");
Philipp Schrader790cb542023-07-05 21:06:52 -0700232 printf(
233 " x: Toggle drawing re-projected targets and print solver "
234 "results.\n");
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800235 printf(" c: Toggle drawing proposed target groupings.\n");
236 printf(" v: Toggle drawing ordered target components.\n");
237 printf(" b: Toggle drawing proposed target components.\n");
238 printf(" n: Toggle drawing countours before and after warping.\n");
Philipp Schrader790cb542023-07-05 21:06:52 -0700239 printf(
240 " m: Toggle drawing raw blob data (may need to change image to "
241 "toggle a redraw).\n");
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800242 printf(" h: Print this message.\n");
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800243 printf(" a: May log camera image to /tmp/debug_viewer_jpeg_<#>.yuyv\n");
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800244 printf(" q: Exit the application.\n");
Parker Schuh2a1447c2019-02-17 00:25:29 -0800245 } else if (key == 'q') {
246 printf("User requested shutdown.\n");
247 exit(0);
248 }
249 HandleBlobs(imgs_last_, fmt_last_);
250 viewer_->Redraw();
251 };
252 }
253
Ben Fredricksonf7b68522019-03-02 21:19:42 -0800254 void DrawContour(ContourNode *contour, PixelRef color) {
255 if (viewer_) {
256 for (ContourNode *node = contour; node->next != contour;) {
257 Vector<2> a(node->pt.x, node->pt.y);
258 Vector<2> b(node->next->pt.x, node->next->pt.y);
259 overlay_.AddLine(a, b, color);
260 node = node->next;
261 }
262 }
263 }
264
Austin Schuhe5015972019-03-09 17:47:34 -0800265 void DrawContour(const ::std::vector<::Eigen::Vector2f> &contour,
266 PixelRef color) {
267 if (viewer_) {
268 for (size_t i = 0; i < contour.size(); ++i) {
269 Vector<2> a(contour[i].x(), contour[i].y());
270 Vector<2> b(contour[(i + 1) % contour.size()].x(),
271 contour[(i + 1) % contour.size()].y());
272 overlay_.AddLine(a, b, color);
273 }
274 }
275 }
276
Parker Schuh2a1447c2019-02-17 00:25:29 -0800277 void DrawComponent(const TargetComponent &comp, PixelRef top_color,
278 PixelRef bot_color, PixelRef in_color,
279 PixelRef out_color) {
280 overlay_.AddLine(comp.top, comp.inside, top_color);
281 overlay_.AddLine(comp.bottom, comp.outside, bot_color);
282
283 overlay_.AddLine(comp.bottom, comp.inside, in_color);
284 overlay_.AddLine(comp.top, comp.outside, out_color);
285 }
286
287 void DrawTarget(const Target &target) {
288 Vector<2> leftTop = (target.left.top + target.left.inside) * 0.5;
289 Vector<2> rightTop = (target.right.top + target.right.inside) * 0.5;
290 overlay_.AddLine(leftTop, rightTop, {255, 215, 0});
291
292 Vector<2> leftBot = (target.left.bottom + target.left.outside) * 0.5;
293 Vector<2> rightBot = (target.right.bottom + target.right.outside) * 0.5;
294 overlay_.AddLine(leftBot, rightBot, {255, 215, 0});
295
296 overlay_.AddLine(leftTop, leftBot, {255, 215, 0});
297 overlay_.AddLine(rightTop, rightBot, {255, 215, 0});
298 }
299
300 void DrawResult(const IntermediateResult &result, PixelRef color) {
Austin Schuh75921532019-03-09 18:46:34 -0800301 Target target = Project(target_finder_.GetTemplateTarget(), intrinsics(),
302 result.extrinsics);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800303 DrawComponent(target.left, color, color, color, color);
304 DrawComponent(target.right, color, color, color, color);
305 }
306
307 void DrawTarget(const IntermediateResult &result, PixelRef color) {
Austin Schuh75921532019-03-09 18:46:34 -0800308 Target target = Project(target_finder_.GetTemplateTarget(), intrinsics(),
309 result.extrinsics);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800310 Segment<2> leftAx((target.left.top + target.left.inside) * 0.5,
311 (target.left.bottom + target.left.outside) * 0.5);
312 leftAx.Set(leftAx.A() * 0.9 + leftAx.B() * 0.1,
313 leftAx.B() * 0.9 + leftAx.A() * 0.1);
314 overlay_.AddLine(leftAx, color);
315
316 Segment<2> rightAx((target.right.top + target.right.inside) * 0.5,
317 (target.right.bottom + target.right.outside) * 0.5);
318 rightAx.Set(rightAx.A() * 0.9 + rightAx.B() * 0.1,
319 rightAx.B() * 0.9 + rightAx.A() * 0.1);
320 overlay_.AddLine(rightAx, color);
321
322 overlay_.AddLine(leftAx.A(), rightAx.A(), color);
323 overlay_.AddLine(leftAx.B(), rightAx.B(), color);
324 Vector<3> p1(0.0, 0.0, 100.0);
325
326 Vector<3> p2 =
327 Rotate(intrinsics().mount_angle, result.extrinsics.r1, 0.0, p1);
328 Vector<2> p3(p2.x(), p2.y());
329 overlay_.AddLine(leftAx.A(), p3 + leftAx.A(), {0, 255, 0});
330 overlay_.AddLine(leftAx.B(), p3 + leftAx.B(), {0, 255, 0});
331 overlay_.AddLine(rightAx.A(), p3 + rightAx.A(), {0, 255, 0});
332 overlay_.AddLine(rightAx.B(), p3 + rightAx.B(), {0, 255, 0});
333
334 overlay_.AddLine(p3 + leftAx.A(), p3 + leftAx.B(), {0, 255, 0});
335 overlay_.AddLine(p3 + leftAx.A(), p3 + rightAx.A(), {0, 255, 0});
336 overlay_.AddLine(p3 + rightAx.A(), p3 + rightAx.B(), {0, 255, 0});
337 overlay_.AddLine(p3 + leftAx.B(), p3 + rightAx.B(), {0, 255, 0});
338 }
339
Austin Schuh75921532019-03-09 18:46:34 -0800340 const IntrinsicParams &intrinsics() const {
341 return target_finder_.intrinsics();
342 }
Parker Schuh2a1447c2019-02-17 00:25:29 -0800343
344 private:
345 // implementation of the filter pipeline.
Austin Schuh75921532019-03-09 18:46:34 -0800346 TargetFinder target_finder_;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800347 aos::vision::BlobStreamViewer *viewer_ = nullptr;
348 aos::vision::PixelLinesOverlay overlay_;
349 std::vector<aos::vision::OverlayBase *> overlays_;
350 BlobList imgs_last_;
351 ImageFormat fmt_last_;
352 bool draw_select_blob_ = false;
Austin Schuh45639882019-03-24 19:20:42 -0700353 bool draw_contours_ = true;
354 bool draw_raw_poly_ = true;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800355 bool draw_components_ = false;
356 bool draw_raw_target_ = false;
Austin Schuh45639882019-03-24 19:20:42 -0700357 bool draw_raw_IR_ = true;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800358 bool draw_results_ = true;
359};
360
361} // namespace vision
Philipp Schrader790cb542023-07-05 21:06:52 -0700362} // namespace y2019
Parker Schuh2a1447c2019-02-17 00:25:29 -0800363
364int main(int argc, char **argv) {
Austin Schuh75921532019-03-09 18:46:34 -0800365 ::gflags::ParseCommandLineFlags(&argc, &argv, true);
Austin Schuha60e37c2019-03-10 19:31:09 -0700366
367 y2019::vision::FilterHarness filter_harness;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800368 aos::vision::DebugFrameworkMain(argc, argv, &filter_harness,
369 aos::vision::CameraParams());
370}