blob: aba35506b7eabb40549da8f24b275555b0f73d87 [file] [log] [blame]
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08001#include "y2022/vision/blob_detector.h"
2
milind-u92195982022-01-22 20:29:31 -08003#include <cmath>
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -08004#include <optional>
milind-u92195982022-01-22 20:29:31 -08005#include <string>
6
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08007#include "aos/network/team_number.h"
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -08008#include "aos/time/time.h"
milind-u92195982022-01-22 20:29:31 -08009#include "opencv2/features2d.hpp"
10#include "opencv2/imgproc.hpp"
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080011
12DEFINE_uint64(green_delta, 50,
13 "Required difference between green pixels vs. red and blue");
14DEFINE_bool(use_outdoors, false,
15 "If true, change thresholds to handle outdoor illumination");
16
17namespace y2022 {
18namespace vision {
19
20cv::Mat BlobDetector::ThresholdImage(cv::Mat rgb_image) {
milind-u61f21e82022-01-23 18:34:11 -080021 cv::Mat binarized_image(cv::Size(rgb_image.cols, rgb_image.rows), CV_8UC1);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080022 for (int row = 0; row < rgb_image.rows; row++) {
23 for (int col = 0; col < rgb_image.cols; col++) {
24 cv::Vec3b pixel = rgb_image.at<cv::Vec3b>(row, col);
25 uint8_t blue = pixel.val[0];
26 uint8_t green = pixel.val[1];
27 uint8_t red = pixel.val[2];
28 // Simple filter that looks for green pixels sufficiently brigher than
29 // red and blue
milind-u92195982022-01-22 20:29:31 -080030 if ((green > blue + FLAGS_green_delta) &&
31 (green > red + FLAGS_green_delta)) {
milind-u61f21e82022-01-23 18:34:11 -080032 binarized_image.at<uint8_t>(row, col) = 255;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080033 } else {
milind-u61f21e82022-01-23 18:34:11 -080034 binarized_image.at<uint8_t>(row, col) = 0;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080035 }
36 }
37 }
38
milind-u61f21e82022-01-23 18:34:11 -080039 return binarized_image;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080040}
41
42std::vector<std::vector<cv::Point>> BlobDetector::FindBlobs(
43 cv::Mat binarized_image) {
44 // find the contours (blob outlines)
45 std::vector<std::vector<cv::Point>> contours;
46 std::vector<cv::Vec4i> hierarchy;
47 cv::findContours(binarized_image, contours, hierarchy, cv::RETR_CCOMP,
48 cv::CHAIN_APPROX_SIMPLE);
49
50 return contours;
51}
52
milind-u61f21e82022-01-23 18:34:11 -080053std::vector<BlobDetector::BlobStats> BlobDetector::ComputeStats(
54 std::vector<std::vector<cv::Point>> blobs) {
55 std::vector<BlobDetector::BlobStats> blob_stats;
56 for (auto blob : blobs) {
57 // Make the blob convex before finding bounding box
58 std::vector<cv::Point> convex_blob;
59 cv::convexHull(blob, convex_blob);
60 auto blob_size = cv::boundingRect(convex_blob).size();
61 cv::Moments moments = cv::moments(convex_blob);
62
63 const auto centroid =
64 cv::Point(moments.m10 / moments.m00, moments.m01 / moments.m00);
65 const double aspect_ratio =
66 static_cast<double>(blob_size.width) / blob_size.height;
67 const double area = moments.m00;
Henry Speisere45e7a22022-02-04 23:17:01 -080068 const size_t num_points = blob.size();
milind-u61f21e82022-01-23 18:34:11 -080069
Henry Speisere45e7a22022-02-04 23:17:01 -080070 blob_stats.emplace_back(
71 BlobStats{centroid, aspect_ratio, area, num_points});
milind-u61f21e82022-01-23 18:34:11 -080072 }
73 return blob_stats;
74}
75
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -080076namespace {
77
78// Linear equation in the form ax + by = c
79struct Line {
80 public:
81 double a, b, c;
82
83 std::optional<cv::Point2d> Intersection(const Line &l) const {
84 // Use Cramer's rule to solve for the intersection
85 const double denominator = Determinant(a, b, l.a, l.b);
86 const double numerator_x = Determinant(c, b, l.c, l.b);
87 const double numerator_y = Determinant(a, c, l.a, l.c);
88
89 std::optional<cv::Point2d> intersection = std::nullopt;
90 // Return nullopt if the denominator is 0, meaning the same slopes
91 if (denominator != 0) {
92 intersection =
93 cv::Point2d(numerator_x / denominator, numerator_y / denominator);
94 }
95
96 return intersection;
97 }
98
99 private: // Determinant of [[a, b], [c, d]]
100 static double Determinant(double a, double b, double c, double d) {
101 return (a * d) - (b * c);
102 }
103};
104
105struct Circle {
106 public:
107 cv::Point2d center;
108 double radius;
109
110 static std::optional<Circle> Fit(std::vector<cv::Point2d> centroids) {
111 CHECK_EQ(centroids.size(), 3ul);
112 // For the 3 points, we have 3 equations in the form
113 // (x - h)^2 + (y - k)^2 = r^2
114 // Manipulate them to solve for the center and radius
115 // (x1 - h)^2 + (y1 - k)^2 = r^2 ->
116 // x1^2 + h^2 - 2x1h + y1^2 + k^2 - 2y1k = r^2
117 // Also, (x2 - h)^2 + (y2 - k)^2 = r^2
118 // Subtracting these two, we get
119 // x1^2 - x2^2 - 2h(x1 - x2) + y1^2 - y2^2 - 2k(y1 - y2) = 0 ->
120 // h(x1 - x2) + k(y1 - y2) = (-x1^2 + x2^2 - y1^2 + y2^2) / -2
121 // Doing the same with equations 1 and 3, we get the second linear equation
122 // h(x1 - x3) + k(y1 - y3) = (-x1^2 + x3^2 - y1^2 + y3^2) / -2
123 // Now, we can solve for their intersection and find the center
124 const auto l =
125 Line{centroids[0].x - centroids[1].x, centroids[0].y - centroids[1].y,
126 (-std::pow(centroids[0].x, 2) + std::pow(centroids[1].x, 2) -
127 std::pow(centroids[0].y, 2) + std::pow(centroids[1].y, 2)) /
128 -2.0};
129 const auto m =
130 Line{centroids[0].x - centroids[2].x, centroids[0].y - centroids[2].y,
131 (-std::pow(centroids[0].x, 2) + std::pow(centroids[2].x, 2) -
132 std::pow(centroids[0].y, 2) + std::pow(centroids[2].y, 2)) /
133 -2.0};
134 const auto center = l.Intersection(m);
135
136 std::optional<Circle> circle = std::nullopt;
137 if (center) {
138 // Now find the radius
139 const double radius = cv::norm(centroids[0] - *center);
140 circle = Circle{*center, radius};
141 }
142 return circle;
143 }
144
145 double DistanceTo(cv::Point2d p) const {
146 // Translate the point so that the circle orgin can be (0, 0)
147 const auto p_prime = cv::Point2d(p.y - center.y, p.x - center.x);
148 // Now, the distance is simply the difference between distance from the
149 // origin to p' and the radius.
150 return std::abs(cv::norm(p_prime) - radius);
151 }
152
153 // Inverted because y-coordinates go backwards
154 bool OnTopHalf(cv::Point2d p) const { return p.y <= center.y; }
155};
156
157} // namespace
158
159std::pair<std::vector<std::vector<cv::Point>>, cv::Point>
160BlobDetector::FilterBlobs(std::vector<std::vector<cv::Point>> blobs,
161 std::vector<BlobDetector::BlobStats> blob_stats) {
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800162 std::vector<std::vector<cv::Point>> filtered_blobs;
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -0800163 std::vector<BlobStats> filtered_stats;
milind-u92195982022-01-22 20:29:31 -0800164
milind-u61f21e82022-01-23 18:34:11 -0800165 auto blob_it = blobs.begin();
166 auto stats_it = blob_stats.begin();
167 while (blob_it < blobs.end() && stats_it < blob_stats.end()) {
milind-u92195982022-01-22 20:29:31 -0800168 // To estimate the maximum y, we can figure out the y value of the blobs
169 // when the camera is the farthest from the target, at the field corner.
170 // We can solve for the pitch of the blob:
171 // blob_pitch = atan((height_tape - height_camera) / depth) + camera_pitch
172 // The triangle with the height of the tape above the camera and the camera
173 // depth is similar to the one with the focal length in y pixels and the y
174 // coordinate offset from the center of the image.
175 // Therefore y_offset = focal_length_y * tan(blob_pitch), and
176 // y = -(y_offset - offset_y)
milind-u61f21e82022-01-23 18:34:11 -0800177 constexpr int kMaxY = 400;
178 constexpr double kTapeAspectRatio = 5.0 / 2.0;
179 constexpr double kAspectRatioThreshold = 1.5;
180 constexpr double kMinArea = 10;
milind-u92195982022-01-22 20:29:31 -0800181 constexpr size_t kMinPoints = 6;
182
milind-u61f21e82022-01-23 18:34:11 -0800183 // Remove all blobs that are at the bottom of the image, have a different
184 // aspect ratio than the tape, or have too little area or points
milind-u92195982022-01-22 20:29:31 -0800185 // TODO(milind): modify to take into account that blobs will be on the side.
milind-u61f21e82022-01-23 18:34:11 -0800186 if ((stats_it->centroid.y <= kMaxY) &&
187 (std::abs(kTapeAspectRatio - stats_it->aspect_ratio) <
188 kAspectRatioThreshold) &&
Henry Speisere45e7a22022-02-04 23:17:01 -0800189 (stats_it->area >= kMinArea) && (stats_it->num_points >= kMinPoints)) {
milind-u61f21e82022-01-23 18:34:11 -0800190 filtered_blobs.push_back(*blob_it);
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -0800191 filtered_stats.push_back(*stats_it);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800192 }
milind-u61f21e82022-01-23 18:34:11 -0800193 blob_it++;
194 stats_it++;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800195 }
milind-u92195982022-01-22 20:29:31 -0800196
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -0800197 // Threshold for mean distance from a blob centroid to a circle.
198 constexpr double kCircleDistanceThreshold = 5.0;
199 std::vector<std::vector<cv::Point>> blob_circle;
200 std::vector<cv::Point2d> centroids;
201
202 // If we see more than this number of blobs after filtering based on
203 // color/size, the circle fit may detect noise so just return no blobs.
Milind Upadhyay2b4404c2022-02-04 21:20:57 -0800204 constexpr size_t kMinFilteredBlobs = 3;
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -0800205 constexpr size_t kMaxFilteredBlobs = 50;
Milind Upadhyay2b4404c2022-02-04 21:20:57 -0800206 if (filtered_blobs.size() >= kMinFilteredBlobs &&
207 filtered_blobs.size() <= kMaxFilteredBlobs) {
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -0800208 constexpr size_t kRansacIterations = 15;
209 for (size_t i = 0; i < kRansacIterations; i++) {
210 // Pick 3 random blobs and see how many fit on their circle
211 const size_t j = std::rand() % filtered_blobs.size();
212 const size_t k = std::rand() % filtered_blobs.size();
213 const size_t l = std::rand() % filtered_blobs.size();
214
215 // Restart if the random indices clash
216 if ((j == k) || (j == l) || (k == l)) {
217 i--;
218 continue;
219 }
220
221 std::vector<std::vector<cv::Point>> current_blobs{
222 filtered_blobs[j], filtered_blobs[k], filtered_blobs[l]};
223 std::vector<cv::Point2d> current_centroids{filtered_stats[j].centroid,
224 filtered_stats[k].centroid,
225 filtered_stats[l].centroid};
226 const std::optional<Circle> circle = Circle::Fit(current_centroids);
227
228 // Make sure that a circle could be created from the points
229 if (!circle) {
230 continue;
231 }
232
233 // Only try to fit points to this circle if all of these are on the top
234 // half, like how the blobs should be
235 if (circle->OnTopHalf(current_centroids[0]) &&
236 circle->OnTopHalf(current_centroids[1]) &&
237 circle->OnTopHalf(current_centroids[2])) {
238 for (size_t m = 0; m < filtered_blobs.size(); m++) {
239 // Add this blob to the list if it is close to the circle, is on the
240 // top half, and isn't one of the other blobs
241 if ((m != i) && (m != j) && (m != k) &&
242 circle->OnTopHalf(filtered_stats[m].centroid) &&
243 (circle->DistanceTo(filtered_stats[m].centroid) <
244 kCircleDistanceThreshold)) {
245 current_blobs.emplace_back(filtered_blobs[m]);
246 current_centroids.emplace_back(filtered_stats[m].centroid);
247 }
248 }
249
250 if (current_blobs.size() > blob_circle.size()) {
251 blob_circle = current_blobs;
252 centroids = current_centroids;
253 }
254 }
255 }
256 }
257
258 cv::Point avg_centroid(-1, -1);
259 if (centroids.size() > 0) {
260 for (auto centroid : centroids) {
261 avg_centroid.x += centroid.x;
262 avg_centroid.y += centroid.y;
263 }
264 avg_centroid.x /= centroids.size();
265 avg_centroid.y /= centroids.size();
266 }
267
268 return {blob_circle, avg_centroid};
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800269}
270
271void BlobDetector::DrawBlobs(
milind-u61f21e82022-01-23 18:34:11 -0800272 cv::Mat view_image,
273 const std::vector<std::vector<cv::Point>> &unfiltered_blobs,
274 const std::vector<std::vector<cv::Point>> &filtered_blobs,
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -0800275 const std::vector<BlobStats> &blob_stats, cv::Point centroid) {
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800276 CHECK_GT(view_image.cols, 0);
277 if (unfiltered_blobs.size() > 0) {
278 // Draw blobs unfilled, with red color border
milind-u92195982022-01-22 20:29:31 -0800279 cv::drawContours(view_image, unfiltered_blobs, -1, cv::Scalar(0, 0, 255),
280 0);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800281 }
282
milind-u92195982022-01-22 20:29:31 -0800283 cv::drawContours(view_image, filtered_blobs, -1, cv::Scalar(0, 255, 0),
284 cv::FILLED);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800285
milind-u92195982022-01-22 20:29:31 -0800286 // Draw blob centroids
milind-u61f21e82022-01-23 18:34:11 -0800287 for (auto stats : blob_stats) {
288 cv::circle(view_image, stats.centroid, 2, cv::Scalar(255, 0, 0),
289 cv::FILLED);
290 }
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -0800291
292 // Draw average centroid
293 cv::circle(view_image, centroid, 3, cv::Scalar(255, 255, 0), cv::FILLED);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800294}
295
296void BlobDetector::ExtractBlobs(
Henry Speisere45e7a22022-02-04 23:17:01 -0800297 cv::Mat rgb_image, cv::Mat &binarized_image,
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800298 std::vector<std::vector<cv::Point>> &filtered_blobs,
299 std::vector<std::vector<cv::Point>> &unfiltered_blobs,
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -0800300 std::vector<BlobStats> &blob_stats, cv::Point &centroid) {
301 auto start = aos::monotonic_clock::now();
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800302 binarized_image = ThresholdImage(rgb_image);
303 unfiltered_blobs = FindBlobs(binarized_image);
milind-u61f21e82022-01-23 18:34:11 -0800304 blob_stats = ComputeStats(unfiltered_blobs);
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -0800305 auto filtered_pair = FilterBlobs(unfiltered_blobs, blob_stats);
306 filtered_blobs = filtered_pair.first;
307 centroid = filtered_pair.second;
308 auto end = aos::monotonic_clock::now();
309 LOG(INFO) << "Blob detection elapsed time: "
310 << std::chrono::duration<double, std::milli>(end - start).count()
311 << " ms";
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800312}
313
314} // namespace vision
315} // namespace y2022