Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 1 | #include "y2019/vision/target_finder.h" |
| 2 | |
| 3 | #include "aos/vision/blob/hierarchical_contour_merge.h" |
| 4 | |
| 5 | using namespace aos::vision; |
| 6 | |
| 7 | namespace y2019 { |
| 8 | namespace vision { |
| 9 | |
Austin Schuh | 4d6e9bd | 2019-03-08 19:54:17 -0800 | [diff] [blame] | 10 | TargetFinder::TargetFinder() : target_template_(Target::MakeTemplate()) {} |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 11 | |
| 12 | aos::vision::RangeImage TargetFinder::Threshold(aos::vision::ImagePtr image) { |
| 13 | const uint8_t threshold_value = GetThresholdValue(); |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 14 | 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 Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | // Filter blobs on size. |
| 25 | void 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 Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 37 | ContourNode* TargetFinder::GetContour(const RangeImage &blob) { |
| 38 | alloc_.reset(); |
| 39 | return RangeImgToContour(blob, &alloc_); |
| 40 | } |
| 41 | |
Ben Fredrickson | ec57582 | 2019-03-02 22:03:20 -0800 | [diff] [blame] | 42 | // TODO(ben): These values will be moved into the constants.h file. |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 43 | namespace { |
| 44 | |
Ben Fredrickson | ec57582 | 2019-03-02 22:03:20 -0800 | [diff] [blame] | 45 | constexpr double f_x = 481.4957; |
| 46 | constexpr double c_x = 341.215; |
| 47 | constexpr double f_y = 484.314; |
| 48 | constexpr double c_y = 251.29; |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 49 | |
Ben Fredrickson | ec57582 | 2019-03-02 22:03:20 -0800 | [diff] [blame] | 50 | constexpr double f_x_prime = 363.1424; |
| 51 | constexpr double c_x_prime = 337.9895; |
| 52 | constexpr double f_y_prime = 366.4837; |
| 53 | constexpr double c_y_prime = 240.0702; |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 54 | |
Ben Fredrickson | ec57582 | 2019-03-02 22:03:20 -0800 | [diff] [blame] | 55 | constexpr double k_1 = -0.2739; |
| 56 | constexpr double k_2 = 0.01583; |
| 57 | constexpr double k_3 = 0.04201; |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 58 | |
| 59 | constexpr int iterations = 7; |
| 60 | |
| 61 | } |
| 62 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 63 | ::Eigen::Vector2f UnWarpPoint(const Point point) { |
Ben Fredrickson | ec57582 | 2019-03-02 22:03:20 -0800 | [diff] [blame] | 64 | const double x0 = ((double)point.x - c_x) / f_x; |
| 65 | const double y0 = ((double)point.y - c_y) / f_y; |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 66 | 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 Fredrickson | ec57582 | 2019-03-02 22:03:20 -0800 | [diff] [blame] | 71 | 1.0 + r_sqr * (k_1 + k_2 * r_sqr * (1.0 + k_3 * r_sqr)); |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 72 | x = x0 / coeff; |
| 73 | y = y0 / coeff; |
| 74 | } |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 75 | 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 Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 78 | } |
| 79 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 80 | ::std::vector<::Eigen::Vector2f> TargetFinder::UnWarpContour( |
| 81 | ContourNode *start) const { |
| 82 | ::std::vector<::Eigen::Vector2f> result; |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 83 | ContourNode *node = start; |
| 84 | while (node->next != start) { |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 85 | result.push_back(UnWarpPoint(node->pt)); |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 86 | node = node->next; |
| 87 | } |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 88 | result.push_back(UnWarpPoint(node->pt)); |
| 89 | return result; |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 90 | } |
| 91 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 92 | // TODO: Try hierarchical merge for this. |
| 93 | // Convert blobs into polygons. |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 94 | Polygon TargetFinder::FindPolygon(::std::vector<::Eigen::Vector2f> &&contour, |
| 95 | bool verbose) { |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 96 | if (verbose) printf("Process Polygon.\n"); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 97 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 98 | ::std::vector<::Eigen::Vector2f> slopes; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 99 | |
| 100 | // Collect all slopes from the contour. |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 101 | ::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 Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 104 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 105 | slopes.push_back(next_point - previous_point); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 106 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 107 | previous_point = next_point; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 110 | 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 Schuh | 335eef1 | 2019-03-02 17:04:17 -0800 | [diff] [blame] | 113 | }; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 114 | |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 115 | // 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 Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 120 | ::std::vector<::Eigen::Vector2f> filtered_slopes = slopes; |
Austin Schuh | 335eef1 | 2019-03-02 17:04:17 -0800 | [diff] [blame] | 121 | // 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 Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 125 | const int window_size = ::std::max(2, range); |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 126 | for (size_t i = 0; i < slopes.size(); ++i) { |
| 127 | ::Eigen::Vector2f a = ::Eigen::Vector2f::Zero(); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 128 | for (int j = -window_size; j <= window_size; ++j) { |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 129 | ::Eigen::Vector2f p = get_pt(j + i); |
| 130 | a += p; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 131 | } |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 132 | a /= (window_size * 2 + 1); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 133 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 134 | filtered_slopes[i] = a; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 135 | } |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 136 | slopes = filtered_slopes; |
Austin Schuh | 335eef1 | 2019-03-02 17:04:17 -0800 | [diff] [blame] | 137 | } |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 138 | if (verbose) printf("Point count: %zu.\n", slopes.size()); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 139 | |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 140 | ::std::vector<float> corner_metric(slopes.size(), 0.0); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 141 | |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 142 | 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 Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 154 | |
| 155 | // Find all centers of corners. |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 156 | // Because they round, multiple slopes may be a corner. |
| 157 | ::std::vector<size_t> edges; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 158 | |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 159 | 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 Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 165 | while (edges.size() < 5) { |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 166 | 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 Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 171 | if (edges.size() == 0) { |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 172 | highest_peak_value = max_value; |
| 173 | } |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 174 | if (max_value < highest_peak_value * peak_acceptance_ratio && |
| 175 | edges.size() == 4) { |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 176 | 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 Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 187 | bool foothill = false; |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 188 | { |
| 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 Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 193 | |
| 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 Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 202 | min_value = ::std::min(current_value, min_value); |
| 203 | |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 204 | if (min_value < valley_value && current_value > min_value) { |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 205 | break; |
| 206 | } |
| 207 | // Kill!!! |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 208 | corner_metric[fwd_index] = -1.0; |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 209 | |
| 210 | fwd_index = (fwd_index + 1) % corner_metric.size(); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 211 | } |
| 212 | } |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 213 | |
| 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 Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 220 | |
| 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 Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 229 | min_value = ::std::min(current_value, min_value); |
| 230 | |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 231 | if (min_value < valley_value && current_value > min_value) { |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 232 | break; |
| 233 | } |
| 234 | // Kill!!! |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 235 | corner_metric[rev_index] = -1.0; |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 236 | |
| 237 | rev_index = |
| 238 | (rev_index - 1 + corner_metric.size()) % corner_metric.size(); |
| 239 | } |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 240 | } |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 241 | |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 242 | *max_element = -1.0; |
| 243 | if (!foothill) { |
| 244 | edges.push_back(highest_index); |
| 245 | } |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 246 | } |
| 247 | |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 248 | ::std::sort(edges.begin(), edges.end()); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 249 | |
| 250 | if (verbose) printf("Edge Count (%zu).\n", edges.size()); |
| 251 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 252 | // Run best-fits over each line segment. |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 253 | Polygon polygon; |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 254 | if (edges.size() >= 3) { |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 255 | 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 Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 260 | float mx = 0.0; |
| 261 | float my = 0.0; |
| 262 | int n = 0; |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 263 | 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 Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 267 | ++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 Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 276 | 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 Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 280 | xx += x * x; |
| 281 | xy += x * y; |
| 282 | yy += y * y; |
| 283 | } |
| 284 | |
| 285 | // TODO: Extract common to hierarchical merge. |
Austin Schuh | 335eef1 | 2019-03-02 17:04:17 -0800 | [diff] [blame] | 286 | const float neg_b_over_2 = (xx + yy) / 2.0; |
| 287 | const float c = (xx * yy - xy * xy); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 288 | |
Austin Schuh | 335eef1 | 2019-03-02 17:04:17 -0800 | [diff] [blame] | 289 | const float sqr = sqrt(neg_b_over_2 * neg_b_over_2 - c); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 290 | |
| 291 | { |
Austin Schuh | 335eef1 | 2019-03-02 17:04:17 -0800 | [diff] [blame] | 292 | const float lam = neg_b_over_2 + sqr; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 293 | float x = xy; |
| 294 | float y = lam - xx; |
| 295 | |
Austin Schuh | 335eef1 | 2019-03-02 17:04:17 -0800 | [diff] [blame] | 296 | const float norm = hypot(x, y); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 297 | x /= norm; |
| 298 | y /= norm; |
| 299 | |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 300 | polygon.segments.push_back( |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 301 | 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 Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 316 | if (verbose) printf("Poly Count (%zu).\n", polygon.segments.size()); |
| 317 | polygon.contour = ::std::move(contour); |
| 318 | return polygon; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | // Convert segments into target components (left or right) |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 322 | ::std::vector<TargetComponent> TargetFinder::FillTargetComponentList( |
| 323 | const ::std::vector<Polygon> &seg_list, bool verbose) { |
| 324 | ::std::vector<TargetComponent> list; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 325 | TargetComponent new_target; |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 326 | for (const Polygon &poly : seg_list) { |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 327 | // Reject missized pollygons for now. Maybe rectify them here in the future; |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 328 | if (poly.segments.size() != 4) { |
Austin Schuh | 9f859ca | 2019-03-06 20:46:01 -0800 | [diff] [blame] | 329 | continue; |
| 330 | } |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 331 | ::std::vector<Vector<2>> corners; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 332 | for (size_t i = 0; i < 4; ++i) { |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 333 | Vector<2> corner = poly.segments[i].Intersect(poly.segments[(i + 1) % 4]); |
Austin Schuh | 9f859ca | 2019-03-06 20:46:01 -0800 | [diff] [blame] | 334 | 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 Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | // Select the closest two points. Short side of the rectangle. |
| 344 | double min_dist = -1; |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 345 | ::std::pair<size_t, size_t> closest; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 346 | 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 Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 433 | if (verbose) printf("Happy with a target\n"); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | return list; |
| 437 | } |
| 438 | |
| 439 | // Match components into targets. |
| 440 | std::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 Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 456 | 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 Perry | bac3d3f | 2019-03-10 14:26:51 -0700 | [diff] [blame] | 468 | } else if (verbose) { |
| 469 | printf("Found same side components: %s.\n", |
| 470 | a.is_right ? "right" : "left"); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 471 | } |
| 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 | |
| 481 | std::vector<IntermediateResult> TargetFinder::FilterResults( |
Alex Perry | bac3d3f | 2019-03-10 14:26:51 -0700 | [diff] [blame] | 482 | const std::vector<IntermediateResult> &results, uint64_t print_rate, |
| 483 | bool verbose) { |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 484 | std::vector<IntermediateResult> filtered; |
| 485 | for (const IntermediateResult &res : results) { |
Alex Perry | bac3d3f | 2019-03-10 14:26:51 -0700 | [diff] [blame] | 486 | // 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 Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 494 | filtered.emplace_back(res); |
Alex Perry | bac3d3f | 2019-03-10 14:26:51 -0700 | [diff] [blame] | 495 | } 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 Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 507 | } |
| 508 | } |
Ben Fredrickson | a8c3d55 | 2019-03-03 14:14:53 -0800 | [diff] [blame] | 509 | 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 Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 520 | return filtered; |
| 521 | } |
| 522 | |
| 523 | } // namespace vision |
| 524 | } // namespace y2019 |