blob: fb2a1347171a55927f8d1218d51bb1a61474d496 [file] [log] [blame]
Parker Schuh2a1447c2019-02-17 00:25:29 -08001#include "y2019/vision/target_finder.h"
2
3#include "aos/vision/blob/hierarchical_contour_merge.h"
4
5using namespace aos::vision;
6
7namespace y2019 {
8namespace vision {
9
Austin Schuh4d6e9bd2019-03-08 19:54:17 -080010TargetFinder::TargetFinder() : target_template_(Target::MakeTemplate()) {}
Parker Schuh2a1447c2019-02-17 00:25:29 -080011
12aos::vision::RangeImage TargetFinder::Threshold(aos::vision::ImagePtr image) {
13 const uint8_t threshold_value = GetThresholdValue();
Brian Silverman37b15b32019-03-10 13:30:18 -070014 return aos::vision::ThresholdImageWithFunction(
15 image, [&](aos::vision::PixelRef px) {
16 if (px.g > threshold_value && px.b > threshold_value &&
17 px.r > threshold_value) {
18 return true;
19 }
20 return false;
21 });
Parker Schuh2a1447c2019-02-17 00:25:29 -080022}
23
24// Filter blobs on size.
25void TargetFinder::PreFilter(BlobList *imgs) {
26 imgs->erase(
27 std::remove_if(imgs->begin(), imgs->end(),
28 [](RangeImage &img) {
29 // We can drop images with a small number of
30 // pixels, but images
31 // must be over 20px or the math will have issues.
32 return (img.npixels() < 100 || img.height() < 25);
33 }),
34 imgs->end());
35}
36
Ben Fredricksonf7b68522019-03-02 21:19:42 -080037ContourNode* TargetFinder::GetContour(const RangeImage &blob) {
38 alloc_.reset();
39 return RangeImgToContour(blob, &alloc_);
40}
41
Ben Fredricksonec575822019-03-02 22:03:20 -080042// TODO(ben): These values will be moved into the constants.h file.
Ben Fredricksonf7b68522019-03-02 21:19:42 -080043namespace {
44
Ben Fredricksonec575822019-03-02 22:03:20 -080045constexpr double f_x = 481.4957;
46constexpr double c_x = 341.215;
47constexpr double f_y = 484.314;
48constexpr double c_y = 251.29;
Ben Fredricksonf7b68522019-03-02 21:19:42 -080049
Ben Fredricksonec575822019-03-02 22:03:20 -080050constexpr double f_x_prime = 363.1424;
51constexpr double c_x_prime = 337.9895;
52constexpr double f_y_prime = 366.4837;
53constexpr double c_y_prime = 240.0702;
Ben Fredricksonf7b68522019-03-02 21:19:42 -080054
Ben Fredricksonec575822019-03-02 22:03:20 -080055constexpr double k_1 = -0.2739;
56constexpr double k_2 = 0.01583;
57constexpr double k_3 = 0.04201;
Ben Fredricksonf7b68522019-03-02 21:19:42 -080058
59constexpr int iterations = 7;
60
61}
62
Austin Schuhe5015972019-03-09 17:47:34 -080063::Eigen::Vector2f UnWarpPoint(const Point point) {
Ben Fredricksonec575822019-03-02 22:03:20 -080064 const double x0 = ((double)point.x - c_x) / f_x;
65 const double y0 = ((double)point.y - c_y) / f_y;
Ben Fredricksonf7b68522019-03-02 21:19:42 -080066 double x = x0;
67 double y = y0;
68 for (int i = 0; i < iterations; i++) {
69 const double r_sqr = x * x + y * y;
70 const double coeff =
Ben Fredricksonec575822019-03-02 22:03:20 -080071 1.0 + r_sqr * (k_1 + k_2 * r_sqr * (1.0 + k_3 * r_sqr));
Ben Fredricksonf7b68522019-03-02 21:19:42 -080072 x = x0 / coeff;
73 y = y0 / coeff;
74 }
Austin Schuhe5015972019-03-09 17:47:34 -080075 const double nx = x * f_x_prime + c_x_prime;
76 const double ny = y * f_y_prime + c_y_prime;
77 return ::Eigen::Vector2f(nx, ny);
Ben Fredricksonf7b68522019-03-02 21:19:42 -080078}
79
Austin Schuhe5015972019-03-09 17:47:34 -080080::std::vector<::Eigen::Vector2f> TargetFinder::UnWarpContour(
81 ContourNode *start) const {
82 ::std::vector<::Eigen::Vector2f> result;
Ben Fredricksonf7b68522019-03-02 21:19:42 -080083 ContourNode *node = start;
84 while (node->next != start) {
Austin Schuhe5015972019-03-09 17:47:34 -080085 result.push_back(UnWarpPoint(node->pt));
Ben Fredricksonf7b68522019-03-02 21:19:42 -080086 node = node->next;
87 }
Austin Schuhe5015972019-03-09 17:47:34 -080088 result.push_back(UnWarpPoint(node->pt));
89 return result;
Ben Fredricksonf7b68522019-03-02 21:19:42 -080090}
91
Parker Schuh2a1447c2019-02-17 00:25:29 -080092// TODO: Try hierarchical merge for this.
93// Convert blobs into polygons.
Austin Schuh6e56faf2019-03-10 14:04:57 -070094Polygon TargetFinder::FindPolygon(::std::vector<::Eigen::Vector2f> &&contour,
95 bool verbose) {
Parker Schuh2a1447c2019-02-17 00:25:29 -080096 if (verbose) printf("Process Polygon.\n");
Parker Schuh2a1447c2019-02-17 00:25:29 -080097
Austin Schuhe5015972019-03-09 17:47:34 -080098 ::std::vector<::Eigen::Vector2f> slopes;
Parker Schuh2a1447c2019-02-17 00:25:29 -080099
100 // Collect all slopes from the contour.
Austin Schuhe5015972019-03-09 17:47:34 -0800101 ::Eigen::Vector2f previous_point = contour[0];
102 for (size_t i = 0; i < contour.size(); ++i) {
103 ::Eigen::Vector2f next_point = contour[(i + 1) % contour.size()];
Parker Schuh2a1447c2019-02-17 00:25:29 -0800104
Austin Schuhe5015972019-03-09 17:47:34 -0800105 slopes.push_back(next_point - previous_point);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800106
Austin Schuhe5015972019-03-09 17:47:34 -0800107 previous_point = next_point;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800108 }
109
Austin Schuhe5015972019-03-09 17:47:34 -0800110 const int num_points = slopes.size();
111 auto get_pt = [&slopes, num_points](int i) {
112 return slopes[(i + num_points * 2) % num_points];
Austin Schuh335eef12019-03-02 17:04:17 -0800113 };
Parker Schuh2a1447c2019-02-17 00:25:29 -0800114
Austin Schuh6a484962019-03-09 21:51:27 -0800115 // Bigger objects should be more filtered. Filter roughly proportional to the
116 // perimeter of the object.
117 const int range = slopes.size() / 50;
118 if (verbose) printf("Corner range: %d.\n", range);
119
Austin Schuhe5015972019-03-09 17:47:34 -0800120 ::std::vector<::Eigen::Vector2f> filtered_slopes = slopes;
Austin Schuh335eef12019-03-02 17:04:17 -0800121 // Three box filter makith a guassian?
122 // Run gaussian filter over the slopes 3 times. That'll get us pretty close
123 // to running a gausian over it.
124 for (int k = 0; k < 3; ++k) {
Austin Schuh6a484962019-03-09 21:51:27 -0800125 const int window_size = ::std::max(2, range);
Austin Schuhe5015972019-03-09 17:47:34 -0800126 for (size_t i = 0; i < slopes.size(); ++i) {
127 ::Eigen::Vector2f a = ::Eigen::Vector2f::Zero();
Parker Schuh2a1447c2019-02-17 00:25:29 -0800128 for (int j = -window_size; j <= window_size; ++j) {
Austin Schuhe5015972019-03-09 17:47:34 -0800129 ::Eigen::Vector2f p = get_pt(j + i);
130 a += p;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800131 }
Austin Schuhe5015972019-03-09 17:47:34 -0800132 a /= (window_size * 2 + 1);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800133
Austin Schuhe5015972019-03-09 17:47:34 -0800134 filtered_slopes[i] = a;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800135 }
Austin Schuhe5015972019-03-09 17:47:34 -0800136 slopes = filtered_slopes;
Austin Schuh335eef12019-03-02 17:04:17 -0800137 }
Austin Schuh6a484962019-03-09 21:51:27 -0800138 if (verbose) printf("Point count: %zu.\n", slopes.size());
Parker Schuh2a1447c2019-02-17 00:25:29 -0800139
Austin Schuh6a484962019-03-09 21:51:27 -0800140 ::std::vector<float> corner_metric(slopes.size(), 0.0);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800141
Austin Schuh6a484962019-03-09 21:51:27 -0800142 for (size_t i = 0; i < slopes.size(); ++i) {
143 const ::Eigen::Vector2f a = get_pt(i - ::std::max(3, range));
144 const ::Eigen::Vector2f b = get_pt(i + ::std::max(3, range));
145 corner_metric[i] = (a - b).squaredNorm();
146 }
147
148 // We want to find the Nth highest peaks.
149 // Clever algorithm: Find the highest point. Then, walk forwards and
150 // backwards to find the next valley each direction which is over x% lower
151 // than the peak.
152 // We want to ignore those points in the future. Set them to 0.
153 // Repeat until we've found the Nth highest peak.
Parker Schuh2a1447c2019-02-17 00:25:29 -0800154
155 // Find all centers of corners.
Austin Schuhe5015972019-03-09 17:47:34 -0800156 // Because they round, multiple slopes may be a corner.
157 ::std::vector<size_t> edges;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800158
Austin Schuh6a484962019-03-09 21:51:27 -0800159 constexpr float peak_acceptance_ratio = 0.16;
160 constexpr float valley_ratio = 0.75;
161
162 float highest_peak_value = 0.0;
163
164 // Nth higest points.
Austin Schuh32ffac22019-03-09 22:42:02 -0800165 while (edges.size() < 5) {
Austin Schuh6a484962019-03-09 21:51:27 -0800166 const ::std::vector<float>::iterator max_element =
167 ::std::max_element(corner_metric.begin(), corner_metric.end());
168 const size_t highest_index =
169 ::std::distance(corner_metric.begin(), max_element);
170 const float max_value = *max_element;
Austin Schuh32ffac22019-03-09 22:42:02 -0800171 if (edges.size() == 0) {
Austin Schuh6a484962019-03-09 21:51:27 -0800172 highest_peak_value = max_value;
173 }
Austin Schuh32ffac22019-03-09 22:42:02 -0800174 if (max_value < highest_peak_value * peak_acceptance_ratio &&
175 edges.size() == 4) {
Austin Schuh6a484962019-03-09 21:51:27 -0800176 if (verbose)
177 printf("Rejecting index: %zu, %f (%f %%)\n", highest_index, max_value,
178 max_value / highest_peak_value);
179 break;
180 }
181 const float valley_value = max_value * valley_ratio;
182
183 if (verbose)
184 printf("Highest index: %zu, %f (%f %%)\n", highest_index, max_value,
185 max_value / highest_peak_value);
186
Austin Schuh32ffac22019-03-09 22:42:02 -0800187 bool foothill = false;
Austin Schuh6a484962019-03-09 21:51:27 -0800188 {
189 float min_value = max_value;
190 size_t fwd_index = (highest_index + 1) % corner_metric.size();
191 while (true) {
192 const float current_value = corner_metric[fwd_index];
Austin Schuh32ffac22019-03-09 22:42:02 -0800193
194 if (current_value == -1.0) {
195 if (min_value >= valley_value) {
196 if (verbose) printf("Foothill\n");
197 foothill = true;
198 }
199 break;
200 }
201
Austin Schuh6a484962019-03-09 21:51:27 -0800202 min_value = ::std::min(current_value, min_value);
203
Austin Schuh32ffac22019-03-09 22:42:02 -0800204 if (min_value < valley_value && current_value > min_value) {
Austin Schuh6a484962019-03-09 21:51:27 -0800205 break;
206 }
207 // Kill!!!
Austin Schuh32ffac22019-03-09 22:42:02 -0800208 corner_metric[fwd_index] = -1.0;
Austin Schuh6a484962019-03-09 21:51:27 -0800209
210 fwd_index = (fwd_index + 1) % corner_metric.size();
Parker Schuh2a1447c2019-02-17 00:25:29 -0800211 }
212 }
Austin Schuh6a484962019-03-09 21:51:27 -0800213
214 {
215 float min_value = max_value;
216 size_t rev_index =
217 (highest_index - 1 + corner_metric.size()) % corner_metric.size();
218 while (true) {
219 const float current_value = corner_metric[rev_index];
Austin Schuh32ffac22019-03-09 22:42:02 -0800220
221 if (current_value == -1.0) {
222 if (min_value >= valley_value) {
223 if (verbose) printf("Foothill\n");
224 foothill = true;
225 }
226 break;
227 }
228
Austin Schuh6a484962019-03-09 21:51:27 -0800229 min_value = ::std::min(current_value, min_value);
230
Austin Schuh32ffac22019-03-09 22:42:02 -0800231 if (min_value < valley_value && current_value > min_value) {
Austin Schuh6a484962019-03-09 21:51:27 -0800232 break;
233 }
234 // Kill!!!
Austin Schuh32ffac22019-03-09 22:42:02 -0800235 corner_metric[rev_index] = -1.0;
Austin Schuh6a484962019-03-09 21:51:27 -0800236
237 rev_index =
238 (rev_index - 1 + corner_metric.size()) % corner_metric.size();
239 }
Parker Schuh2a1447c2019-02-17 00:25:29 -0800240 }
Austin Schuh6a484962019-03-09 21:51:27 -0800241
Austin Schuh32ffac22019-03-09 22:42:02 -0800242 *max_element = -1.0;
243 if (!foothill) {
244 edges.push_back(highest_index);
245 }
Parker Schuh2a1447c2019-02-17 00:25:29 -0800246 }
247
Austin Schuh6a484962019-03-09 21:51:27 -0800248 ::std::sort(edges.begin(), edges.end());
Parker Schuh2a1447c2019-02-17 00:25:29 -0800249
250 if (verbose) printf("Edge Count (%zu).\n", edges.size());
251
Parker Schuh2a1447c2019-02-17 00:25:29 -0800252 // Run best-fits over each line segment.
Austin Schuh6e56faf2019-03-10 14:04:57 -0700253 Polygon polygon;
Austin Schuh6a484962019-03-09 21:51:27 -0800254 if (edges.size() >= 3) {
Austin Schuhe5015972019-03-09 17:47:34 -0800255 for (size_t i = 0; i < edges.size(); ++i) {
256 // Include the corners in both line fits.
257 const size_t segment_start_index = edges[i];
258 const size_t segment_end_index =
259 (edges[(i + 1) % edges.size()] + 1) % contour.size();
Parker Schuh2a1447c2019-02-17 00:25:29 -0800260 float mx = 0.0;
261 float my = 0.0;
262 int n = 0;
Austin Schuhe5015972019-03-09 17:47:34 -0800263 for (size_t j = segment_start_index; j != segment_end_index;
264 (j = (j + 1) % contour.size())) {
265 mx += contour[j].x();
266 my += contour[j].y();
Parker Schuh2a1447c2019-02-17 00:25:29 -0800267 ++n;
268 // (x - [x] / N) ** 2 = [x * x] - 2 * [x] * [x] / N + [x] * [x] / N / N;
269 }
270 mx /= n;
271 my /= n;
272
273 float xx = 0.0;
274 float xy = 0.0;
275 float yy = 0.0;
Austin Schuhe5015972019-03-09 17:47:34 -0800276 for (size_t j = segment_start_index; j != segment_end_index;
277 (j = (j + 1) % contour.size())) {
278 const float x = contour[j].x() - mx;
279 const float y = contour[j].y() - my;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800280 xx += x * x;
281 xy += x * y;
282 yy += y * y;
283 }
284
285 // TODO: Extract common to hierarchical merge.
Austin Schuh335eef12019-03-02 17:04:17 -0800286 const float neg_b_over_2 = (xx + yy) / 2.0;
287 const float c = (xx * yy - xy * xy);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800288
Austin Schuh335eef12019-03-02 17:04:17 -0800289 const float sqr = sqrt(neg_b_over_2 * neg_b_over_2 - c);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800290
291 {
Austin Schuh335eef12019-03-02 17:04:17 -0800292 const float lam = neg_b_over_2 + sqr;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800293 float x = xy;
294 float y = lam - xx;
295
Austin Schuh335eef12019-03-02 17:04:17 -0800296 const float norm = hypot(x, y);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800297 x /= norm;
298 y /= norm;
299
Austin Schuh6e56faf2019-03-10 14:04:57 -0700300 polygon.segments.push_back(
Parker Schuh2a1447c2019-02-17 00:25:29 -0800301 Segment<2>(Vector<2>(mx, my), Vector<2>(mx + x, my + y)));
302 }
303
304 /* Characteristic polynomial
305 1 lam^2 - (xx + yy) lam + (xx * yy - xy * xy) = 0
306
307 [a b]
308 [c d]
309
310 // covariance matrix.
311 [xx xy] [nx]
312 [xy yy] [ny]
313 */
314 }
315 }
Austin Schuh6e56faf2019-03-10 14:04:57 -0700316 if (verbose) printf("Poly Count (%zu).\n", polygon.segments.size());
317 polygon.contour = ::std::move(contour);
318 return polygon;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800319}
320
321// Convert segments into target components (left or right)
Austin Schuh6e56faf2019-03-10 14:04:57 -0700322::std::vector<TargetComponent> TargetFinder::FillTargetComponentList(
323 const ::std::vector<Polygon> &seg_list, bool verbose) {
324 ::std::vector<TargetComponent> list;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800325 TargetComponent new_target;
Austin Schuh6e56faf2019-03-10 14:04:57 -0700326 for (const Polygon &poly : seg_list) {
Parker Schuh2a1447c2019-02-17 00:25:29 -0800327 // Reject missized pollygons for now. Maybe rectify them here in the future;
Austin Schuh6e56faf2019-03-10 14:04:57 -0700328 if (poly.segments.size() != 4) {
Austin Schuh9f859ca2019-03-06 20:46:01 -0800329 continue;
330 }
Austin Schuh32ffac22019-03-09 22:42:02 -0800331 ::std::vector<Vector<2>> corners;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800332 for (size_t i = 0; i < 4; ++i) {
Austin Schuh6e56faf2019-03-10 14:04:57 -0700333 Vector<2> corner = poly.segments[i].Intersect(poly.segments[(i + 1) % 4]);
Austin Schuh9f859ca2019-03-06 20:46:01 -0800334 if (::std::isnan(corner.x()) || ::std::isnan(corner.y())) {
335 break;
336 }
337 corners.push_back(corner);
338 }
339 if (corners.size() != 4) {
340 continue;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800341 }
342
343 // Select the closest two points. Short side of the rectangle.
344 double min_dist = -1;
Austin Schuh32ffac22019-03-09 22:42:02 -0800345 ::std::pair<size_t, size_t> closest;
Parker Schuh2a1447c2019-02-17 00:25:29 -0800346 for (size_t i = 0; i < 4; ++i) {
347 size_t next = (i + 1) % 4;
348 double nd = corners[i].SquaredDistanceTo(corners[next]);
349 if (min_dist == -1 || nd < min_dist) {
350 min_dist = nd;
351 closest.first = i;
352 closest.second = next;
353 }
354 }
355
356 // Verify our top is above the bottom.
357 size_t bot_index = closest.first;
358 size_t top_index = (closest.first + 2) % 4;
359 if (corners[top_index].y() < corners[bot_index].y()) {
360 closest.first = top_index;
361 closest.second = (top_index + 1) % 4;
362 }
363
364 // Find the major axis.
365 size_t far_first = (closest.first + 2) % 4;
366 size_t far_second = (closest.second + 2) % 4;
367 Segment<2> major_axis(
368 (corners[closest.first] + corners[closest.second]) * 0.5,
369 (corners[far_first] + corners[far_second]) * 0.5);
370 if (major_axis.AsVector().AngleToZero() > M_PI / 180.0 * 120.0 ||
371 major_axis.AsVector().AngleToZero() < M_PI / 180.0 * 60.0) {
372 // Target is angled way too much, drop it.
373 continue;
374 }
375
376 // organize the top points.
377 Vector<2> topA = corners[closest.first] - major_axis.B();
378 new_target.major_axis = major_axis;
379 if (major_axis.AsVector().AngleToZero() > M_PI / 2.0) {
380 // We have a left target since we are leaning positive.
381 new_target.is_right = false;
382 if (topA.AngleTo(major_axis.AsVector()) > 0.0) {
383 // And our A point is left of the major axis.
384 new_target.inside = corners[closest.second];
385 new_target.top = corners[closest.first];
386 } else {
387 // our A point is to the right of the major axis.
388 new_target.inside = corners[closest.first];
389 new_target.top = corners[closest.second];
390 }
391 } else {
392 // We have a right target since we are leaning negative.
393 new_target.is_right = true;
394 if (topA.AngleTo(major_axis.AsVector()) > 0.0) {
395 // And our A point is left of the major axis.
396 new_target.inside = corners[closest.first];
397 new_target.top = corners[closest.second];
398 } else {
399 // our A point is to the right of the major axis.
400 new_target.inside = corners[closest.second];
401 new_target.top = corners[closest.first];
402 }
403 }
404
405 // organize the top points.
406 Vector<2> botA = corners[far_first] - major_axis.A();
407 if (major_axis.AsVector().AngleToZero() > M_PI / 2.0) {
408 // We have a right target since we are leaning positive.
409 if (botA.AngleTo(major_axis.AsVector()) < M_PI) {
410 // And our A point is left of the major axis.
411 new_target.outside = corners[far_second];
412 new_target.bottom = corners[far_first];
413 } else {
414 // our A point is to the right of the major axis.
415 new_target.outside = corners[far_first];
416 new_target.bottom = corners[far_second];
417 }
418 } else {
419 // We have a left target since we are leaning negative.
420 if (botA.AngleTo(major_axis.AsVector()) < M_PI) {
421 // And our A point is left of the major axis.
422 new_target.outside = corners[far_first];
423 new_target.bottom = corners[far_second];
424 } else {
425 // our A point is to the right of the major axis.
426 new_target.outside = corners[far_second];
427 new_target.bottom = corners[far_first];
428 }
429 }
430
431 // This piece of the target should be ready now.
432 list.emplace_back(new_target);
Austin Schuh32ffac22019-03-09 22:42:02 -0800433 if (verbose) printf("Happy with a target\n");
Parker Schuh2a1447c2019-02-17 00:25:29 -0800434 }
435
436 return list;
437}
438
439// Match components into targets.
440std::vector<Target> TargetFinder::FindTargetsFromComponents(
441 const std::vector<TargetComponent> component_list, bool verbose) {
442 std::vector<Target> target_list;
443 using namespace aos::vision;
444 if (component_list.size() < 2) {
445 // We don't enough parts for a target.
446 return target_list;
447 }
448
449 for (size_t i = 0; i < component_list.size(); i++) {
450 const TargetComponent &a = component_list[i];
451 for (size_t j = 0; j < i; j++) {
452 bool target_valid = false;
453 Target new_target;
454 const TargetComponent &b = component_list[j];
455
Parker Schuh2a1447c2019-02-17 00:25:29 -0800456 if (a.is_right && !b.is_right) {
457 if (a.top.x() > b.top.x()) {
458 new_target.right = a;
459 new_target.left = b;
460 target_valid = true;
461 }
462 } else if (!a.is_right && b.is_right) {
463 if (b.top.x() > a.top.x()) {
464 new_target.right = b;
465 new_target.left = a;
466 target_valid = true;
467 }
Alex Perrybac3d3f2019-03-10 14:26:51 -0700468 } else if (verbose) {
469 printf("Found same side components: %s.\n",
470 a.is_right ? "right" : "left");
Parker Schuh2a1447c2019-02-17 00:25:29 -0800471 }
472 if (target_valid) {
473 target_list.emplace_back(new_target);
474 }
475 }
476 }
477 if (verbose) printf("Possible Target: %zu.\n", target_list.size());
478 return target_list;
479}
480
481std::vector<IntermediateResult> TargetFinder::FilterResults(
Alex Perrybac3d3f2019-03-10 14:26:51 -0700482 const std::vector<IntermediateResult> &results, uint64_t print_rate,
483 bool verbose) {
Parker Schuh2a1447c2019-02-17 00:25:29 -0800484 std::vector<IntermediateResult> filtered;
485 for (const IntermediateResult &res : results) {
Alex Perrybac3d3f2019-03-10 14:26:51 -0700486 // Based on a linear regression between error and distance to target.
487 // Closer targets can have a higher error because they are bigger.
488 double acceptable_error = std::max(2 * (21 - 12 * res.extrinsics.z), 50.0);
489 if (res.solver_error < acceptable_error) {
490 if (verbose) {
491 printf("Using an 8 point solve: %f < %f \n", res.solver_error,
492 acceptable_error);
493 }
Parker Schuh2a1447c2019-02-17 00:25:29 -0800494 filtered.emplace_back(res);
Alex Perrybac3d3f2019-03-10 14:26:51 -0700495 } else if (res.backup_solver_error < acceptable_error) {
496 if (verbose) {
497 printf("Using a 4 point solve: %f < %f \n", res.backup_solver_error,
498 acceptable_error);
499 }
500 IntermediateResult backup;
501 backup.extrinsics = res.backup_extrinsics;
502 backup.solver_error= res.backup_solver_error;
503 filtered.emplace_back(backup);
504 } else if (verbose) {
505 printf("Rejecting a target with errors: (%f, %f) > %f \n",
506 res.solver_error, res.backup_solver_error, acceptable_error);
Parker Schuh2a1447c2019-02-17 00:25:29 -0800507 }
508 }
Ben Fredricksona8c3d552019-03-03 14:14:53 -0800509 frame_count_++;
510 if (!filtered.empty()) {
511 valid_result_count_++;
512 }
513 if (print_rate > 0 && frame_count_ > print_rate) {
514 LOG(INFO, "Found (%zu / %zu)(%.2f) targets.\n", valid_result_count_,
515 frame_count_, (double)valid_result_count_ / (double)frame_count_);
516 frame_count_ = 0;
517 valid_result_count_ = 0;
518 }
519
Parker Schuh2a1447c2019-02-17 00:25:29 -0800520 return filtered;
521}
522
523} // namespace vision
524} // namespace y2019