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" |
Brian Silverman | 6323677 | 2019-03-23 22:02:44 -0700 | [diff] [blame] | 4 | #include "ceres/ceres.h" |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 5 | |
| 6 | using namespace aos::vision; |
| 7 | |
| 8 | namespace y2019 { |
| 9 | namespace vision { |
| 10 | |
Brian Silverman | 6323677 | 2019-03-23 22:02:44 -0700 | [diff] [blame] | 11 | TargetFinder::TargetFinder() |
| 12 | : target_template_(Target::MakeTemplate()), |
| 13 | ceres_context_(ceres::Context::Create()) {} |
| 14 | |
| 15 | TargetFinder::~TargetFinder() {} |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 16 | |
| 17 | aos::vision::RangeImage TargetFinder::Threshold(aos::vision::ImagePtr image) { |
| 18 | const uint8_t threshold_value = GetThresholdValue(); |
Brian Silverman | 37b15b3 | 2019-03-10 13:30:18 -0700 | [diff] [blame] | 19 | return aos::vision::ThresholdImageWithFunction( |
| 20 | image, [&](aos::vision::PixelRef px) { |
| 21 | if (px.g > threshold_value && px.b > threshold_value && |
| 22 | px.r > threshold_value) { |
| 23 | return true; |
| 24 | } |
| 25 | return false; |
| 26 | }); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | // Filter blobs on size. |
| 30 | void TargetFinder::PreFilter(BlobList *imgs) { |
| 31 | imgs->erase( |
| 32 | std::remove_if(imgs->begin(), imgs->end(), |
| 33 | [](RangeImage &img) { |
| 34 | // We can drop images with a small number of |
| 35 | // pixels, but images |
| 36 | // must be over 20px or the math will have issues. |
| 37 | return (img.npixels() < 100 || img.height() < 25); |
| 38 | }), |
| 39 | imgs->end()); |
| 40 | } |
| 41 | |
Austin Schuh | 7d2ef03 | 2019-03-10 14:59:34 -0700 | [diff] [blame] | 42 | ContourNode *TargetFinder::GetContour(const RangeImage &blob) { |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 43 | alloc_.reset(); |
| 44 | return RangeImgToContour(blob, &alloc_); |
| 45 | } |
| 46 | |
Ben Fredrickson | ec57582 | 2019-03-02 22:03:20 -0800 | [diff] [blame] | 47 | // TODO(ben): These values will be moved into the constants.h file. |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 48 | namespace { |
| 49 | |
Austin Schuh | 7d2ef03 | 2019-03-10 14:59:34 -0700 | [diff] [blame] | 50 | ::Eigen::Vector2f AosVectorToEigenVector(Vector<2> in) { |
| 51 | return ::Eigen::Vector2f(in.x(), in.y()); |
| 52 | } |
| 53 | |
Ben Fredrickson | ec57582 | 2019-03-02 22:03:20 -0800 | [diff] [blame] | 54 | constexpr double f_x = 481.4957; |
| 55 | constexpr double c_x = 341.215; |
| 56 | constexpr double f_y = 484.314; |
| 57 | constexpr double c_y = 251.29; |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 58 | |
Ben Fredrickson | ec57582 | 2019-03-02 22:03:20 -0800 | [diff] [blame] | 59 | constexpr double f_x_prime = 363.1424; |
| 60 | constexpr double c_x_prime = 337.9895; |
| 61 | constexpr double f_y_prime = 366.4837; |
| 62 | constexpr double c_y_prime = 240.0702; |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 63 | |
Ben Fredrickson | ec57582 | 2019-03-02 22:03:20 -0800 | [diff] [blame] | 64 | constexpr double k_1 = -0.2739; |
| 65 | constexpr double k_2 = 0.01583; |
| 66 | constexpr double k_3 = 0.04201; |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 67 | |
| 68 | constexpr int iterations = 7; |
| 69 | |
| 70 | } |
| 71 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 72 | ::Eigen::Vector2f UnWarpPoint(const Point point) { |
Ben Fredrickson | ec57582 | 2019-03-02 22:03:20 -0800 | [diff] [blame] | 73 | const double x0 = ((double)point.x - c_x) / f_x; |
| 74 | const double y0 = ((double)point.y - c_y) / f_y; |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 75 | double x = x0; |
| 76 | double y = y0; |
| 77 | for (int i = 0; i < iterations; i++) { |
| 78 | const double r_sqr = x * x + y * y; |
Alex Perry | 2ca1574 | 2019-03-10 20:38:40 -0700 | [diff] [blame] | 79 | const double coeff = 1.0 + r_sqr * (k_1 + r_sqr * (k_2 + r_sqr * (k_3))); |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 80 | x = x0 / coeff; |
| 81 | y = y0 / coeff; |
| 82 | } |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 83 | const double nx = x * f_x_prime + c_x_prime; |
| 84 | const double ny = y * f_y_prime + c_y_prime; |
| 85 | return ::Eigen::Vector2f(nx, ny); |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 88 | ::std::vector<::Eigen::Vector2f> TargetFinder::UnWarpContour( |
| 89 | ContourNode *start) const { |
| 90 | ::std::vector<::Eigen::Vector2f> result; |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 91 | ContourNode *node = start; |
| 92 | while (node->next != start) { |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 93 | result.push_back(UnWarpPoint(node->pt)); |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 94 | node = node->next; |
| 95 | } |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 96 | result.push_back(UnWarpPoint(node->pt)); |
| 97 | return result; |
Ben Fredrickson | f7b6852 | 2019-03-02 21:19:42 -0800 | [diff] [blame] | 98 | } |
| 99 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 100 | // TODO: Try hierarchical merge for this. |
| 101 | // Convert blobs into polygons. |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 102 | Polygon TargetFinder::FindPolygon(::std::vector<::Eigen::Vector2f> &&contour, |
| 103 | bool verbose) { |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 104 | if (verbose) printf("Process Polygon.\n"); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 105 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 106 | ::std::vector<::Eigen::Vector2f> slopes; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 107 | |
| 108 | // Collect all slopes from the contour. |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 109 | ::Eigen::Vector2f previous_point = contour[0]; |
| 110 | for (size_t i = 0; i < contour.size(); ++i) { |
| 111 | ::Eigen::Vector2f next_point = contour[(i + 1) % contour.size()]; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 112 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 113 | slopes.push_back(next_point - previous_point); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 114 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 115 | previous_point = next_point; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 118 | const int num_points = slopes.size(); |
| 119 | auto get_pt = [&slopes, num_points](int i) { |
| 120 | return slopes[(i + num_points * 2) % num_points]; |
Austin Schuh | 335eef1 | 2019-03-02 17:04:17 -0800 | [diff] [blame] | 121 | }; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 122 | |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 123 | // Bigger objects should be more filtered. Filter roughly proportional to the |
| 124 | // perimeter of the object. |
| 125 | const int range = slopes.size() / 50; |
| 126 | if (verbose) printf("Corner range: %d.\n", range); |
| 127 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 128 | ::std::vector<::Eigen::Vector2f> filtered_slopes = slopes; |
Austin Schuh | 335eef1 | 2019-03-02 17:04:17 -0800 | [diff] [blame] | 129 | // Three box filter makith a guassian? |
| 130 | // Run gaussian filter over the slopes 3 times. That'll get us pretty close |
| 131 | // to running a gausian over it. |
| 132 | for (int k = 0; k < 3; ++k) { |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 133 | const int window_size = ::std::max(2, range); |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 134 | for (size_t i = 0; i < slopes.size(); ++i) { |
| 135 | ::Eigen::Vector2f a = ::Eigen::Vector2f::Zero(); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 136 | for (int j = -window_size; j <= window_size; ++j) { |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 137 | ::Eigen::Vector2f p = get_pt(j + i); |
| 138 | a += p; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 139 | } |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 140 | a /= (window_size * 2 + 1); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 141 | |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 142 | filtered_slopes[i] = a; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 143 | } |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 144 | slopes = filtered_slopes; |
Austin Schuh | 335eef1 | 2019-03-02 17:04:17 -0800 | [diff] [blame] | 145 | } |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 146 | if (verbose) printf("Point count: %zu.\n", slopes.size()); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 147 | |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 148 | ::std::vector<float> corner_metric(slopes.size(), 0.0); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 149 | |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 150 | for (size_t i = 0; i < slopes.size(); ++i) { |
| 151 | const ::Eigen::Vector2f a = get_pt(i - ::std::max(3, range)); |
| 152 | const ::Eigen::Vector2f b = get_pt(i + ::std::max(3, range)); |
| 153 | corner_metric[i] = (a - b).squaredNorm(); |
| 154 | } |
| 155 | |
| 156 | // We want to find the Nth highest peaks. |
| 157 | // Clever algorithm: Find the highest point. Then, walk forwards and |
| 158 | // backwards to find the next valley each direction which is over x% lower |
| 159 | // than the peak. |
| 160 | // We want to ignore those points in the future. Set them to 0. |
| 161 | // Repeat until we've found the Nth highest peak. |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 162 | |
| 163 | // Find all centers of corners. |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 164 | // Because they round, multiple slopes may be a corner. |
| 165 | ::std::vector<size_t> edges; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 166 | |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 167 | constexpr float peak_acceptance_ratio = 0.16; |
| 168 | constexpr float valley_ratio = 0.75; |
| 169 | |
| 170 | float highest_peak_value = 0.0; |
| 171 | |
| 172 | // Nth higest points. |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 173 | while (edges.size() < 5) { |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 174 | const ::std::vector<float>::iterator max_element = |
| 175 | ::std::max_element(corner_metric.begin(), corner_metric.end()); |
| 176 | const size_t highest_index = |
| 177 | ::std::distance(corner_metric.begin(), max_element); |
| 178 | const float max_value = *max_element; |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 179 | if (edges.size() == 0) { |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 180 | highest_peak_value = max_value; |
| 181 | } |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 182 | if (max_value < highest_peak_value * peak_acceptance_ratio && |
| 183 | edges.size() == 4) { |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 184 | if (verbose) |
| 185 | printf("Rejecting index: %zu, %f (%f %%)\n", highest_index, max_value, |
| 186 | max_value / highest_peak_value); |
| 187 | break; |
| 188 | } |
| 189 | const float valley_value = max_value * valley_ratio; |
| 190 | |
| 191 | if (verbose) |
| 192 | printf("Highest index: %zu, %f (%f %%)\n", highest_index, max_value, |
| 193 | max_value / highest_peak_value); |
| 194 | |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 195 | bool foothill = false; |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 196 | { |
| 197 | float min_value = max_value; |
| 198 | size_t fwd_index = (highest_index + 1) % corner_metric.size(); |
| 199 | while (true) { |
| 200 | const float current_value = corner_metric[fwd_index]; |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 201 | |
| 202 | if (current_value == -1.0) { |
| 203 | if (min_value >= valley_value) { |
| 204 | if (verbose) printf("Foothill\n"); |
| 205 | foothill = true; |
| 206 | } |
| 207 | break; |
| 208 | } |
| 209 | |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 210 | min_value = ::std::min(current_value, min_value); |
| 211 | |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 212 | if (min_value < valley_value && current_value > min_value) { |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 213 | break; |
| 214 | } |
| 215 | // Kill!!! |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 216 | corner_metric[fwd_index] = -1.0; |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 217 | |
| 218 | fwd_index = (fwd_index + 1) % corner_metric.size(); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 219 | } |
| 220 | } |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 221 | |
| 222 | { |
| 223 | float min_value = max_value; |
| 224 | size_t rev_index = |
| 225 | (highest_index - 1 + corner_metric.size()) % corner_metric.size(); |
| 226 | while (true) { |
| 227 | const float current_value = corner_metric[rev_index]; |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 228 | |
| 229 | if (current_value == -1.0) { |
| 230 | if (min_value >= valley_value) { |
| 231 | if (verbose) printf("Foothill\n"); |
| 232 | foothill = true; |
| 233 | } |
| 234 | break; |
| 235 | } |
| 236 | |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 237 | min_value = ::std::min(current_value, min_value); |
| 238 | |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 239 | if (min_value < valley_value && current_value > min_value) { |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 240 | break; |
| 241 | } |
| 242 | // Kill!!! |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 243 | corner_metric[rev_index] = -1.0; |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 244 | |
| 245 | rev_index = |
| 246 | (rev_index - 1 + corner_metric.size()) % corner_metric.size(); |
| 247 | } |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 248 | } |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 249 | |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 250 | *max_element = -1.0; |
| 251 | if (!foothill) { |
| 252 | edges.push_back(highest_index); |
| 253 | } |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 254 | } |
| 255 | |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 256 | ::std::sort(edges.begin(), edges.end()); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 257 | |
| 258 | if (verbose) printf("Edge Count (%zu).\n", edges.size()); |
| 259 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 260 | // Run best-fits over each line segment. |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 261 | Polygon polygon; |
Austin Schuh | 6a48496 | 2019-03-09 21:51:27 -0800 | [diff] [blame] | 262 | if (edges.size() >= 3) { |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 263 | for (size_t i = 0; i < edges.size(); ++i) { |
| 264 | // Include the corners in both line fits. |
| 265 | const size_t segment_start_index = edges[i]; |
| 266 | const size_t segment_end_index = |
| 267 | (edges[(i + 1) % edges.size()] + 1) % contour.size(); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 268 | float mx = 0.0; |
| 269 | float my = 0.0; |
| 270 | int n = 0; |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 271 | for (size_t j = segment_start_index; j != segment_end_index; |
| 272 | (j = (j + 1) % contour.size())) { |
| 273 | mx += contour[j].x(); |
| 274 | my += contour[j].y(); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 275 | ++n; |
| 276 | // (x - [x] / N) ** 2 = [x * x] - 2 * [x] * [x] / N + [x] * [x] / N / N; |
| 277 | } |
| 278 | mx /= n; |
| 279 | my /= n; |
| 280 | |
| 281 | float xx = 0.0; |
| 282 | float xy = 0.0; |
| 283 | float yy = 0.0; |
Austin Schuh | e501597 | 2019-03-09 17:47:34 -0800 | [diff] [blame] | 284 | for (size_t j = segment_start_index; j != segment_end_index; |
| 285 | (j = (j + 1) % contour.size())) { |
| 286 | const float x = contour[j].x() - mx; |
| 287 | const float y = contour[j].y() - my; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 288 | xx += x * x; |
| 289 | xy += x * y; |
| 290 | yy += y * y; |
| 291 | } |
| 292 | |
| 293 | // TODO: Extract common to hierarchical merge. |
Austin Schuh | 335eef1 | 2019-03-02 17:04:17 -0800 | [diff] [blame] | 294 | const float neg_b_over_2 = (xx + yy) / 2.0; |
| 295 | const float c = (xx * yy - xy * xy); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 296 | |
Austin Schuh | 335eef1 | 2019-03-02 17:04:17 -0800 | [diff] [blame] | 297 | 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] | 298 | |
| 299 | { |
Austin Schuh | 335eef1 | 2019-03-02 17:04:17 -0800 | [diff] [blame] | 300 | const float lam = neg_b_over_2 + sqr; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 301 | float x = xy; |
| 302 | float y = lam - xx; |
| 303 | |
Austin Schuh | 335eef1 | 2019-03-02 17:04:17 -0800 | [diff] [blame] | 304 | const float norm = hypot(x, y); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 305 | x /= norm; |
| 306 | y /= norm; |
| 307 | |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 308 | polygon.segments.push_back( |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 309 | Segment<2>(Vector<2>(mx, my), Vector<2>(mx + x, my + y))); |
| 310 | } |
| 311 | |
| 312 | /* Characteristic polynomial |
| 313 | 1 lam^2 - (xx + yy) lam + (xx * yy - xy * xy) = 0 |
| 314 | |
| 315 | [a b] |
| 316 | [c d] |
| 317 | |
| 318 | // covariance matrix. |
| 319 | [xx xy] [nx] |
| 320 | [xy yy] [ny] |
| 321 | */ |
| 322 | } |
| 323 | } |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 324 | if (verbose) printf("Poly Count (%zu).\n", polygon.segments.size()); |
| 325 | polygon.contour = ::std::move(contour); |
| 326 | return polygon; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // Convert segments into target components (left or right) |
Austin Schuh | 6e56faf | 2019-03-10 14:04:57 -0700 | [diff] [blame] | 330 | ::std::vector<TargetComponent> TargetFinder::FillTargetComponentList( |
| 331 | const ::std::vector<Polygon> &seg_list, bool verbose) { |
| 332 | ::std::vector<TargetComponent> list; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 333 | TargetComponent new_target; |
Austin Schuh | 7d2ef03 | 2019-03-10 14:59:34 -0700 | [diff] [blame] | 334 | for (const Polygon &polygon : seg_list) { |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 335 | // Reject missized pollygons for now. Maybe rectify them here in the future; |
Austin Schuh | 7d2ef03 | 2019-03-10 14:59:34 -0700 | [diff] [blame] | 336 | if (polygon.segments.size() != 4) { |
Austin Schuh | 9f859ca | 2019-03-06 20:46:01 -0800 | [diff] [blame] | 337 | continue; |
| 338 | } |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 339 | ::std::vector<Vector<2>> corners; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 340 | for (size_t i = 0; i < 4; ++i) { |
Austin Schuh | 7d2ef03 | 2019-03-10 14:59:34 -0700 | [diff] [blame] | 341 | Vector<2> corner = |
| 342 | polygon.segments[i].Intersect(polygon.segments[(i + 1) % 4]); |
Austin Schuh | 9f859ca | 2019-03-06 20:46:01 -0800 | [diff] [blame] | 343 | if (::std::isnan(corner.x()) || ::std::isnan(corner.y())) { |
| 344 | break; |
| 345 | } |
| 346 | corners.push_back(corner); |
| 347 | } |
| 348 | if (corners.size() != 4) { |
| 349 | continue; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | // Select the closest two points. Short side of the rectangle. |
| 353 | double min_dist = -1; |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 354 | ::std::pair<size_t, size_t> closest; |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 355 | for (size_t i = 0; i < 4; ++i) { |
| 356 | size_t next = (i + 1) % 4; |
| 357 | double nd = corners[i].SquaredDistanceTo(corners[next]); |
| 358 | if (min_dist == -1 || nd < min_dist) { |
| 359 | min_dist = nd; |
| 360 | closest.first = i; |
| 361 | closest.second = next; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // Verify our top is above the bottom. |
| 366 | size_t bot_index = closest.first; |
| 367 | size_t top_index = (closest.first + 2) % 4; |
| 368 | if (corners[top_index].y() < corners[bot_index].y()) { |
| 369 | closest.first = top_index; |
| 370 | closest.second = (top_index + 1) % 4; |
| 371 | } |
| 372 | |
| 373 | // Find the major axis. |
| 374 | size_t far_first = (closest.first + 2) % 4; |
| 375 | size_t far_second = (closest.second + 2) % 4; |
| 376 | Segment<2> major_axis( |
| 377 | (corners[closest.first] + corners[closest.second]) * 0.5, |
| 378 | (corners[far_first] + corners[far_second]) * 0.5); |
| 379 | if (major_axis.AsVector().AngleToZero() > M_PI / 180.0 * 120.0 || |
| 380 | major_axis.AsVector().AngleToZero() < M_PI / 180.0 * 60.0) { |
| 381 | // Target is angled way too much, drop it. |
| 382 | continue; |
| 383 | } |
| 384 | |
| 385 | // organize the top points. |
| 386 | Vector<2> topA = corners[closest.first] - major_axis.B(); |
| 387 | new_target.major_axis = major_axis; |
| 388 | if (major_axis.AsVector().AngleToZero() > M_PI / 2.0) { |
| 389 | // We have a left target since we are leaning positive. |
| 390 | new_target.is_right = false; |
| 391 | if (topA.AngleTo(major_axis.AsVector()) > 0.0) { |
| 392 | // And our A point is left of the major axis. |
| 393 | new_target.inside = corners[closest.second]; |
| 394 | new_target.top = corners[closest.first]; |
| 395 | } else { |
| 396 | // our A point is to the right of the major axis. |
| 397 | new_target.inside = corners[closest.first]; |
| 398 | new_target.top = corners[closest.second]; |
| 399 | } |
| 400 | } else { |
| 401 | // We have a right target since we are leaning negative. |
| 402 | new_target.is_right = true; |
| 403 | if (topA.AngleTo(major_axis.AsVector()) > 0.0) { |
| 404 | // And our A point is left of the major axis. |
| 405 | new_target.inside = corners[closest.first]; |
| 406 | new_target.top = corners[closest.second]; |
| 407 | } else { |
| 408 | // our A point is to the right of the major axis. |
| 409 | new_target.inside = corners[closest.second]; |
| 410 | new_target.top = corners[closest.first]; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // organize the top points. |
| 415 | Vector<2> botA = corners[far_first] - major_axis.A(); |
| 416 | if (major_axis.AsVector().AngleToZero() > M_PI / 2.0) { |
| 417 | // We have a right target since we are leaning positive. |
| 418 | if (botA.AngleTo(major_axis.AsVector()) < M_PI) { |
| 419 | // And our A point is left of the major axis. |
| 420 | new_target.outside = corners[far_second]; |
| 421 | new_target.bottom = corners[far_first]; |
| 422 | } else { |
| 423 | // our A point is to the right of the major axis. |
| 424 | new_target.outside = corners[far_first]; |
| 425 | new_target.bottom = corners[far_second]; |
| 426 | } |
| 427 | } else { |
| 428 | // We have a left target since we are leaning negative. |
| 429 | if (botA.AngleTo(major_axis.AsVector()) < M_PI) { |
| 430 | // And our A point is left of the major axis. |
| 431 | new_target.outside = corners[far_first]; |
| 432 | new_target.bottom = corners[far_second]; |
| 433 | } else { |
| 434 | // our A point is to the right of the major axis. |
| 435 | new_target.outside = corners[far_second]; |
| 436 | new_target.bottom = corners[far_first]; |
| 437 | } |
| 438 | } |
| 439 | |
Austin Schuh | 7d2ef03 | 2019-03-10 14:59:34 -0700 | [diff] [blame] | 440 | // Take the vector which points from the bottom to the top of the target |
| 441 | // along the outside edge. |
| 442 | const ::Eigen::Vector2f outer_edge_vector = |
| 443 | AosVectorToEigenVector(new_target.top - new_target.outside); |
| 444 | // Now, dot each point in the perimeter along this vector. The one with the |
| 445 | // smallest component will be the one closest to the bottom along this |
| 446 | // direction vector. |
| 447 | ::Eigen::Vector2f smallest_point = polygon.contour[0]; |
| 448 | float smallest_value = outer_edge_vector.transpose() * smallest_point; |
| 449 | for (const ::Eigen::Vector2f point : polygon.contour) { |
| 450 | const float current_value = outer_edge_vector.transpose() * point; |
| 451 | if (current_value < smallest_value) { |
| 452 | smallest_value = current_value; |
| 453 | smallest_point = point; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // This piece of the target should be ready now. |
| 458 | new_target.bottom_point = smallest_point; |
| 459 | if (verbose) { |
| 460 | printf("Lowest point in the blob is (%f, %f)\n", smallest_point.x(), |
| 461 | smallest_point.y()); |
| 462 | } |
| 463 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 464 | // This piece of the target should be ready now. |
| 465 | list.emplace_back(new_target); |
Austin Schuh | 7d2ef03 | 2019-03-10 14:59:34 -0700 | [diff] [blame] | 466 | |
Austin Schuh | 32ffac2 | 2019-03-09 22:42:02 -0800 | [diff] [blame] | 467 | if (verbose) printf("Happy with a target\n"); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | return list; |
| 471 | } |
| 472 | |
| 473 | // Match components into targets. |
| 474 | std::vector<Target> TargetFinder::FindTargetsFromComponents( |
| 475 | const std::vector<TargetComponent> component_list, bool verbose) { |
| 476 | std::vector<Target> target_list; |
| 477 | using namespace aos::vision; |
| 478 | if (component_list.size() < 2) { |
| 479 | // We don't enough parts for a target. |
| 480 | return target_list; |
| 481 | } |
| 482 | |
| 483 | for (size_t i = 0; i < component_list.size(); i++) { |
| 484 | const TargetComponent &a = component_list[i]; |
| 485 | for (size_t j = 0; j < i; j++) { |
| 486 | bool target_valid = false; |
| 487 | Target new_target; |
| 488 | const TargetComponent &b = component_list[j]; |
| 489 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 490 | if (a.is_right && !b.is_right) { |
| 491 | if (a.top.x() > b.top.x()) { |
| 492 | new_target.right = a; |
| 493 | new_target.left = b; |
| 494 | target_valid = true; |
| 495 | } |
| 496 | } else if (!a.is_right && b.is_right) { |
| 497 | if (b.top.x() > a.top.x()) { |
| 498 | new_target.right = b; |
| 499 | new_target.left = a; |
| 500 | target_valid = true; |
| 501 | } |
Alex Perry | bac3d3f | 2019-03-10 14:26:51 -0700 | [diff] [blame] | 502 | } else if (verbose) { |
| 503 | printf("Found same side components: %s.\n", |
| 504 | a.is_right ? "right" : "left"); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 505 | } |
| 506 | if (target_valid) { |
| 507 | target_list.emplace_back(new_target); |
| 508 | } |
| 509 | } |
| 510 | } |
| 511 | if (verbose) printf("Possible Target: %zu.\n", target_list.size()); |
| 512 | return target_list; |
| 513 | } |
| 514 | |
Austin Schuh | e6cfbe3 | 2019-03-10 18:05:57 -0700 | [diff] [blame] | 515 | bool TargetFinder::MaybePickAndUpdateResult(IntermediateResult *result, |
| 516 | bool verbose) { |
| 517 | // Based on a linear regression between error and distance to target. |
| 518 | // Closer targets can have a higher error because they are bigger. |
| 519 | const double acceptable_error = |
Alex Perry | ebc6dd5 | 2019-03-16 07:54:48 -0700 | [diff] [blame] | 520 | std::max(2 * (75 - 12 * result->extrinsics.z), 75.0); |
Austin Schuh | 4563988 | 2019-03-24 19:20:42 -0700 | [diff] [blame] | 521 | if (!result->good_corners) { |
| 522 | if (verbose) { |
| 523 | printf("Rejecting a target with bad corners: (%f, %f)\n", |
| 524 | result->solver_error, result->backup_solver_error); |
| 525 | } |
| 526 | } else if (result->solver_error < acceptable_error) { |
Austin Schuh | e6cfbe3 | 2019-03-10 18:05:57 -0700 | [diff] [blame] | 527 | if (verbose) { |
| 528 | printf("Using an 8 point solve: %f < %f \n", result->solver_error, |
| 529 | acceptable_error); |
| 530 | } |
| 531 | return true; |
| 532 | } else if (result->backup_solver_error < acceptable_error) { |
| 533 | if (verbose) { |
| 534 | printf("Using a 4 point solve: %f < %f \n", result->backup_solver_error, |
| 535 | acceptable_error); |
| 536 | } |
| 537 | IntermediateResult backup; |
| 538 | result->extrinsics = result->backup_extrinsics; |
| 539 | result->solver_error = result->backup_solver_error; |
| 540 | return true; |
| 541 | } else if (verbose) { |
| 542 | printf("Rejecting a target with errors: (%f, %f) > %f \n", |
| 543 | result->solver_error, result->backup_solver_error, acceptable_error); |
| 544 | } |
| 545 | return false; |
| 546 | } |
| 547 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 548 | std::vector<IntermediateResult> TargetFinder::FilterResults( |
Alex Perry | bac3d3f | 2019-03-10 14:26:51 -0700 | [diff] [blame] | 549 | const std::vector<IntermediateResult> &results, uint64_t print_rate, |
| 550 | bool verbose) { |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 551 | std::vector<IntermediateResult> filtered; |
| 552 | for (const IntermediateResult &res : results) { |
Austin Schuh | e6cfbe3 | 2019-03-10 18:05:57 -0700 | [diff] [blame] | 553 | IntermediateResult updatable_result = res; |
| 554 | if (MaybePickAndUpdateResult(&updatable_result, verbose)) { |
| 555 | filtered.emplace_back(updatable_result); |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 556 | } |
| 557 | } |
Alex Perry | 5b1e8e3 | 2019-04-07 13:25:31 -0700 | [diff] [blame^] | 558 | |
| 559 | // Sort the target list so that the widest (ie closest) target is first. |
| 560 | sort(filtered.begin(), filtered.end(), |
| 561 | [](const IntermediateResult &a, const IntermediateResult &b) |
| 562 | -> bool { return a.target_width > b.target_width; }); |
| 563 | |
Ben Fredrickson | a8c3d55 | 2019-03-03 14:14:53 -0800 | [diff] [blame] | 564 | frame_count_++; |
| 565 | if (!filtered.empty()) { |
| 566 | valid_result_count_++; |
| 567 | } |
| 568 | if (print_rate > 0 && frame_count_ > print_rate) { |
Brian Silverman | 6323677 | 2019-03-23 22:02:44 -0700 | [diff] [blame] | 569 | LOG(INFO) << "Found (" << valid_result_count_ << " / " << frame_count_ |
| 570 | << ")(" << ((double)valid_result_count_ / (double)frame_count_) |
| 571 | << " targets."; |
Ben Fredrickson | a8c3d55 | 2019-03-03 14:14:53 -0800 | [diff] [blame] | 572 | frame_count_ = 0; |
| 573 | valid_result_count_ = 0; |
| 574 | } |
| 575 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 576 | return filtered; |
| 577 | } |
| 578 | |
Alex Perry | 5b1e8e3 | 2019-04-07 13:25:31 -0700 | [diff] [blame^] | 579 | bool TargetFinder::TestExposure(const std::vector<IntermediateResult> &results, |
| 580 | int *desired_exposure) { |
| 581 | // TODO(ben): Add these values to config file. |
| 582 | constexpr double low_dist = 0.8; |
| 583 | constexpr double high_dist = 2.0; |
| 584 | constexpr int low_exposure = 60; |
| 585 | constexpr int mid_exposure = 300; |
| 586 | constexpr int high_exposure = 500; |
| 587 | |
| 588 | bool needs_update = false; |
| 589 | if (results.size() > 0) { |
| 590 | // We are seeing a target so lets use an exposure |
| 591 | // based on the distance to that target. |
| 592 | // First result should always be the closest target. |
| 593 | if (results[0].extrinsics.z < low_dist) { |
| 594 | *desired_exposure = low_exposure; |
| 595 | } else if (results[0].extrinsics.z > high_dist) { |
| 596 | *desired_exposure = high_exposure; |
| 597 | } else { |
| 598 | *desired_exposure = mid_exposure; |
| 599 | } |
| 600 | if (*desired_exposure != current_exposure_) { |
| 601 | needs_update = true; |
| 602 | current_exposure_ = *desired_exposure; |
| 603 | } |
| 604 | } else { |
| 605 | // We don't see a target, but part of the problem might |
| 606 | // be the exposure setting. Lets try changing it and see |
| 607 | // if things get better. |
| 608 | const int offset = std::rand() % 10; |
| 609 | |
| 610 | // At random with 3/X probability try a higher or lower. |
| 611 | if (offset == 0) { |
| 612 | if (low_exposure != current_exposure_) { |
| 613 | needs_update = true; |
| 614 | current_exposure_ = low_exposure; |
| 615 | *desired_exposure = low_exposure; |
| 616 | } |
| 617 | } else if (offset == 1) { |
| 618 | if (mid_exposure != current_exposure_) { |
| 619 | needs_update = true; |
| 620 | current_exposure_ = mid_exposure; |
| 621 | *desired_exposure = mid_exposure; |
| 622 | } |
| 623 | } else if (offset == 2) { |
| 624 | if (high_exposure != current_exposure_) { |
| 625 | needs_update = true; |
| 626 | current_exposure_ = high_exposure; |
| 627 | *desired_exposure = high_exposure; |
| 628 | } |
| 629 | } |
| 630 | // If one of our cases is not hit don't change anything. |
| 631 | } |
| 632 | return needs_update; |
| 633 | } |
| 634 | |
Parker Schuh | 2a1447c | 2019-02-17 00:25:29 -0800 | [diff] [blame] | 635 | } // namespace vision |
| 636 | } // namespace y2019 |