Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 1 | #include "y2022/vision/blob_detector.h" |
| 2 | |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 3 | #include <cmath> |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 4 | #include <optional> |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 5 | #include <string> |
| 6 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 7 | #include "aos/network/team_number.h" |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 8 | #include "aos/time/time.h" |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 9 | #include "opencv2/features2d.hpp" |
| 10 | #include "opencv2/imgproc.hpp" |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 11 | |
Yash Chainani | 6acad6f | 2022-02-03 10:52:53 -0800 | [diff] [blame] | 12 | DEFINE_uint64(red_delta, 100, |
| 13 | "Required difference between green pixels vs. red"); |
| 14 | DEFINE_uint64(blue_delta, 50, |
| 15 | "Required difference between green pixels vs. blue"); |
| 16 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 17 | DEFINE_bool(use_outdoors, false, |
| 18 | "If true, change thresholds to handle outdoor illumination"); |
Yash Chainani | 6acad6f | 2022-02-03 10:52:53 -0800 | [diff] [blame] | 19 | DEFINE_uint64(outdoors_red_delta, 100, |
| 20 | "Difference between green pixels vs. red, when outdoors"); |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 21 | DEFINE_uint64(outdoors_blue_delta, 1, |
Yash Chainani | 6acad6f | 2022-02-03 10:52:53 -0800 | [diff] [blame] | 22 | "Difference between green pixels vs. blue, when outdoors"); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 23 | |
| 24 | namespace y2022 { |
| 25 | namespace vision { |
| 26 | |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 27 | cv::Mat BlobDetector::ThresholdImage(cv::Mat bgr_image) { |
Yash Chainani | 6acad6f | 2022-02-03 10:52:53 -0800 | [diff] [blame] | 28 | size_t red_delta = FLAGS_red_delta; |
| 29 | size_t blue_delta = FLAGS_blue_delta; |
| 30 | |
| 31 | if (FLAGS_use_outdoors) { |
| 32 | red_delta = FLAGS_outdoors_red_delta; |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 33 | blue_delta = FLAGS_outdoors_blue_delta; |
Yash Chainani | 6acad6f | 2022-02-03 10:52:53 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 36 | cv::Mat binarized_image(cv::Size(bgr_image.cols, bgr_image.rows), CV_8UC1); |
| 37 | for (int row = 0; row < bgr_image.rows; row++) { |
| 38 | for (int col = 0; col < bgr_image.cols; col++) { |
| 39 | cv::Vec3b pixel = bgr_image.at<cv::Vec3b>(row, col); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 40 | uint8_t blue = pixel.val[0]; |
| 41 | uint8_t green = pixel.val[1]; |
| 42 | uint8_t red = pixel.val[2]; |
| 43 | // Simple filter that looks for green pixels sufficiently brigher than |
| 44 | // red and blue |
Yash Chainani | 6acad6f | 2022-02-03 10:52:53 -0800 | [diff] [blame] | 45 | if ((green > blue + blue_delta) && (green > red + red_delta)) { |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 46 | binarized_image.at<uint8_t>(row, col) = 255; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 47 | } else { |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 48 | binarized_image.at<uint8_t>(row, col) = 0; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 53 | return binarized_image; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | std::vector<std::vector<cv::Point>> BlobDetector::FindBlobs( |
| 57 | cv::Mat binarized_image) { |
| 58 | // find the contours (blob outlines) |
| 59 | std::vector<std::vector<cv::Point>> contours; |
| 60 | std::vector<cv::Vec4i> hierarchy; |
| 61 | cv::findContours(binarized_image, contours, hierarchy, cv::RETR_CCOMP, |
| 62 | cv::CHAIN_APPROX_SIMPLE); |
| 63 | |
| 64 | return contours; |
| 65 | } |
| 66 | |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 67 | std::vector<BlobDetector::BlobStats> BlobDetector::ComputeStats( |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 68 | const std::vector<std::vector<cv::Point>> &blobs) { |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 69 | std::vector<BlobDetector::BlobStats> blob_stats; |
| 70 | for (auto blob : blobs) { |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 71 | auto blob_size = cv::boundingRect(blob).size(); |
| 72 | cv::Moments moments = cv::moments(blob); |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 73 | |
| 74 | const auto centroid = |
| 75 | cv::Point(moments.m10 / moments.m00, moments.m01 / moments.m00); |
| 76 | const double aspect_ratio = |
| 77 | static_cast<double>(blob_size.width) / blob_size.height; |
| 78 | const double area = moments.m00; |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame] | 79 | const size_t num_points = blob.size(); |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 80 | |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame] | 81 | blob_stats.emplace_back( |
| 82 | BlobStats{centroid, aspect_ratio, area, num_points}); |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 83 | } |
| 84 | return blob_stats; |
| 85 | } |
| 86 | |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 87 | namespace { |
| 88 | |
| 89 | // Linear equation in the form ax + by = c |
| 90 | struct Line { |
| 91 | public: |
| 92 | double a, b, c; |
| 93 | |
| 94 | std::optional<cv::Point2d> Intersection(const Line &l) const { |
| 95 | // Use Cramer's rule to solve for the intersection |
| 96 | const double denominator = Determinant(a, b, l.a, l.b); |
| 97 | const double numerator_x = Determinant(c, b, l.c, l.b); |
| 98 | const double numerator_y = Determinant(a, c, l.a, l.c); |
| 99 | |
| 100 | std::optional<cv::Point2d> intersection = std::nullopt; |
| 101 | // Return nullopt if the denominator is 0, meaning the same slopes |
| 102 | if (denominator != 0) { |
| 103 | intersection = |
| 104 | cv::Point2d(numerator_x / denominator, numerator_y / denominator); |
| 105 | } |
| 106 | |
| 107 | return intersection; |
| 108 | } |
| 109 | |
| 110 | private: // Determinant of [[a, b], [c, d]] |
| 111 | static double Determinant(double a, double b, double c, double d) { |
| 112 | return (a * d) - (b * c); |
| 113 | } |
| 114 | }; |
| 115 | |
| 116 | struct Circle { |
| 117 | public: |
| 118 | cv::Point2d center; |
| 119 | double radius; |
| 120 | |
| 121 | static std::optional<Circle> Fit(std::vector<cv::Point2d> centroids) { |
| 122 | CHECK_EQ(centroids.size(), 3ul); |
| 123 | // For the 3 points, we have 3 equations in the form |
| 124 | // (x - h)^2 + (y - k)^2 = r^2 |
| 125 | // Manipulate them to solve for the center and radius |
| 126 | // (x1 - h)^2 + (y1 - k)^2 = r^2 -> |
| 127 | // x1^2 + h^2 - 2x1h + y1^2 + k^2 - 2y1k = r^2 |
| 128 | // Also, (x2 - h)^2 + (y2 - k)^2 = r^2 |
| 129 | // Subtracting these two, we get |
| 130 | // x1^2 - x2^2 - 2h(x1 - x2) + y1^2 - y2^2 - 2k(y1 - y2) = 0 -> |
| 131 | // h(x1 - x2) + k(y1 - y2) = (-x1^2 + x2^2 - y1^2 + y2^2) / -2 |
| 132 | // Doing the same with equations 1 and 3, we get the second linear equation |
| 133 | // h(x1 - x3) + k(y1 - y3) = (-x1^2 + x3^2 - y1^2 + y3^2) / -2 |
| 134 | // Now, we can solve for their intersection and find the center |
| 135 | const auto l = |
| 136 | Line{centroids[0].x - centroids[1].x, centroids[0].y - centroids[1].y, |
| 137 | (-std::pow(centroids[0].x, 2) + std::pow(centroids[1].x, 2) - |
| 138 | std::pow(centroids[0].y, 2) + std::pow(centroids[1].y, 2)) / |
| 139 | -2.0}; |
| 140 | const auto m = |
| 141 | Line{centroids[0].x - centroids[2].x, centroids[0].y - centroids[2].y, |
| 142 | (-std::pow(centroids[0].x, 2) + std::pow(centroids[2].x, 2) - |
| 143 | std::pow(centroids[0].y, 2) + std::pow(centroids[2].y, 2)) / |
| 144 | -2.0}; |
| 145 | const auto center = l.Intersection(m); |
| 146 | |
| 147 | std::optional<Circle> circle = std::nullopt; |
| 148 | if (center) { |
| 149 | // Now find the radius |
| 150 | const double radius = cv::norm(centroids[0] - *center); |
| 151 | circle = Circle{*center, radius}; |
| 152 | } |
| 153 | return circle; |
| 154 | } |
| 155 | |
| 156 | double DistanceTo(cv::Point2d p) const { |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 157 | const auto p_prime = TranslateToOrigin(p); |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 158 | // Now, the distance is simply the difference between distance from the |
| 159 | // origin to p' and the radius. |
| 160 | return std::abs(cv::norm(p_prime) - radius); |
| 161 | } |
| 162 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 163 | double AngleOf(cv::Point2d p) const { |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 164 | auto p_prime = TranslateToOrigin(p); |
| 165 | // Flip the y because y values go downwards. |
| 166 | p_prime.y *= -1; |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 167 | return std::atan2(p_prime.y, p_prime.x); |
| 168 | } |
| 169 | |
| 170 | // TODO(milind): handle wrapping around 2pi |
| 171 | bool InAngleRange(cv::Point2d p, double theta_min, double theta_max) const { |
| 172 | const double theta = AngleOf(p); |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 173 | return (theta >= theta_min && theta <= theta_max); |
| 174 | } |
| 175 | |
| 176 | private: |
| 177 | // Translate the point on the circle |
| 178 | // as if the circle's center is the origin (0,0) |
| 179 | cv::Point2d TranslateToOrigin(cv::Point2d p) const { |
| 180 | return cv::Point2d(p.x - center.x, p.y - center.y); |
| 181 | } |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | } // namespace |
| 185 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 186 | void BlobDetector::FilterBlobs(BlobResult *blob_result) { |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 187 | std::vector<std::vector<cv::Point>> filtered_blobs; |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 188 | std::vector<BlobStats> filtered_stats; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 189 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 190 | auto blob_it = blob_result->unfiltered_blobs.begin(); |
| 191 | auto stats_it = blob_result->blob_stats.begin(); |
| 192 | while (blob_it < blob_result->unfiltered_blobs.end() && |
| 193 | stats_it < blob_result->blob_stats.end()) { |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 194 | constexpr double kTapeAspectRatio = 5.0 / 2.0; |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 195 | constexpr double kAspectRatioThreshold = 1.6; |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 196 | constexpr double kMinArea = 10; |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 197 | constexpr size_t kMinNumPoints = 6; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 198 | |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 199 | // Remove all blobs that are at the bottom of the image, have a different |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 200 | // aspect ratio than the tape, or have too little area or points. |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 201 | if ((std::abs(1.0 - kTapeAspectRatio / stats_it->aspect_ratio) < |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 202 | kAspectRatioThreshold) && |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 203 | (stats_it->area >= kMinArea) && |
| 204 | (stats_it->num_points >= kMinNumPoints)) { |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 205 | filtered_blobs.push_back(*blob_it); |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 206 | filtered_stats.push_back(*stats_it); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 207 | } |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 208 | blob_it++; |
| 209 | stats_it++; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 210 | } |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 211 | |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 212 | // Threshold for mean distance from a blob centroid to a circle. |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 213 | constexpr double kCircleDistanceThreshold = 10.0; |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 214 | // We should only expect to see blobs between these angles on a circle. |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 215 | constexpr double kDegToRad = M_PI / 180.0; |
| 216 | constexpr double kMinBlobAngle = 50.0 * kDegToRad; |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 217 | constexpr double kMaxBlobAngle = M_PI - kMinBlobAngle; |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 218 | std::vector<std::vector<cv::Point>> blob_circle; |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 219 | Circle circle; |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 220 | std::vector<cv::Point2d> centroids; |
| 221 | |
| 222 | // If we see more than this number of blobs after filtering based on |
| 223 | // color/size, the circle fit may detect noise so just return no blobs. |
Milind Upadhyay | 2b4404c | 2022-02-04 21:20:57 -0800 | [diff] [blame] | 224 | constexpr size_t kMinFilteredBlobs = 3; |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 225 | constexpr size_t kMaxFilteredBlobs = 50; |
Milind Upadhyay | 2b4404c | 2022-02-04 21:20:57 -0800 | [diff] [blame] | 226 | if (filtered_blobs.size() >= kMinFilteredBlobs && |
| 227 | filtered_blobs.size() <= kMaxFilteredBlobs) { |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 228 | constexpr size_t kRansacIterations = 15; |
| 229 | for (size_t i = 0; i < kRansacIterations; i++) { |
| 230 | // Pick 3 random blobs and see how many fit on their circle |
| 231 | const size_t j = std::rand() % filtered_blobs.size(); |
| 232 | const size_t k = std::rand() % filtered_blobs.size(); |
| 233 | const size_t l = std::rand() % filtered_blobs.size(); |
| 234 | |
| 235 | // Restart if the random indices clash |
| 236 | if ((j == k) || (j == l) || (k == l)) { |
| 237 | i--; |
| 238 | continue; |
| 239 | } |
| 240 | |
| 241 | std::vector<std::vector<cv::Point>> current_blobs{ |
| 242 | filtered_blobs[j], filtered_blobs[k], filtered_blobs[l]}; |
| 243 | std::vector<cv::Point2d> current_centroids{filtered_stats[j].centroid, |
| 244 | filtered_stats[k].centroid, |
| 245 | filtered_stats[l].centroid}; |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 246 | const std::optional<Circle> current_circle = |
| 247 | Circle::Fit(current_centroids); |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 248 | |
| 249 | // Make sure that a circle could be created from the points |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 250 | if (!current_circle) { |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 251 | continue; |
| 252 | } |
| 253 | |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 254 | // Only try to fit points to this circle if all of these are between |
| 255 | // certain angles. |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 256 | if (current_circle->InAngleRange(current_centroids[0], kMinBlobAngle, |
| 257 | kMaxBlobAngle) && |
| 258 | current_circle->InAngleRange(current_centroids[1], kMinBlobAngle, |
| 259 | kMaxBlobAngle) && |
| 260 | current_circle->InAngleRange(current_centroids[2], kMinBlobAngle, |
| 261 | kMaxBlobAngle)) { |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 262 | for (size_t m = 0; m < filtered_blobs.size(); m++) { |
| 263 | // Add this blob to the list if it is close to the circle, is on the |
| 264 | // top half, and isn't one of the other blobs |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 265 | if ((m != j) && (m != k) && (m != l) && |
| 266 | current_circle->InAngleRange(filtered_stats[m].centroid, |
| 267 | kMinBlobAngle, kMaxBlobAngle) && |
| 268 | (current_circle->DistanceTo(filtered_stats[m].centroid) < |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 269 | kCircleDistanceThreshold)) { |
| 270 | current_blobs.emplace_back(filtered_blobs[m]); |
| 271 | current_centroids.emplace_back(filtered_stats[m].centroid); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | if (current_blobs.size() > blob_circle.size()) { |
| 276 | blob_circle = current_blobs; |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 277 | circle = *current_circle; |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 278 | centroids = current_centroids; |
| 279 | } |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | cv::Point avg_centroid(-1, -1); |
| 285 | if (centroids.size() > 0) { |
| 286 | for (auto centroid : centroids) { |
| 287 | avg_centroid.x += centroid.x; |
| 288 | avg_centroid.y += centroid.y; |
| 289 | } |
| 290 | avg_centroid.x /= centroids.size(); |
| 291 | avg_centroid.y /= centroids.size(); |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 292 | |
| 293 | for (auto centroid : centroids) { |
| 294 | blob_result->filtered_centroids.emplace_back( |
| 295 | static_cast<int>(centroid.x), static_cast<int>(centroid.y)); |
| 296 | } |
| 297 | |
| 298 | // Sort the filtered centroids to make them go from left to right |
| 299 | std::sort(blob_result->filtered_centroids.begin(), |
| 300 | blob_result->filtered_centroids.end(), |
| 301 | [&circle](cv::Point p, cv::Point q) { |
| 302 | // If the angle is greater, it is more left and should be |
| 303 | // considered "less" for sorting |
| 304 | return circle.AngleOf(p) > circle.AngleOf(q); |
| 305 | }); |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 306 | } |
| 307 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 308 | blob_result->filtered_blobs = blob_circle; |
| 309 | blob_result->centroid = avg_centroid; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 310 | } |
| 311 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 312 | void BlobDetector::DrawBlobs(const BlobResult &blob_result, |
| 313 | cv::Mat view_image) { |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 314 | CHECK_GT(view_image.cols, 0); |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 315 | if (blob_result.unfiltered_blobs.size() > 0) { |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 316 | // Draw blobs unfilled, with red color border |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 317 | cv::drawContours(view_image, blob_result.unfiltered_blobs, -1, |
| 318 | cv::Scalar(0, 0, 255), 0); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 319 | } |
| 320 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 321 | cv::drawContours(view_image, blob_result.filtered_blobs, -1, |
| 322 | cv::Scalar(0, 100, 0), cv::FILLED); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 323 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 324 | static constexpr double kCircleRadius = 2.0; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 325 | // Draw blob centroids |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 326 | for (auto stats : blob_result.blob_stats) { |
| 327 | cv::circle(view_image, stats.centroid, kCircleRadius, |
| 328 | cv::Scalar(0, 215, 255), cv::FILLED); |
| 329 | } |
| 330 | for (auto centroid : blob_result.filtered_centroids) { |
| 331 | cv::circle(view_image, centroid, kCircleRadius, cv::Scalar(0, 255, 0), |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 332 | cv::FILLED); |
| 333 | } |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 334 | |
| 335 | // Draw average centroid |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 336 | cv::circle(view_image, blob_result.centroid, kCircleRadius, |
| 337 | cv::Scalar(255, 255, 0), cv::FILLED); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 338 | } |
| 339 | |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 340 | void BlobDetector::ExtractBlobs(cv::Mat bgr_image, |
Milind Upadhyay | 25610d2 | 2022-02-07 15:35:26 -0800 | [diff] [blame] | 341 | BlobDetector::BlobResult *blob_result) { |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 342 | auto start = aos::monotonic_clock::now(); |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 343 | blob_result->binarized_image = ThresholdImage(bgr_image); |
Milind Upadhyay | 25610d2 | 2022-02-07 15:35:26 -0800 | [diff] [blame] | 344 | blob_result->unfiltered_blobs = FindBlobs(blob_result->binarized_image); |
| 345 | blob_result->blob_stats = ComputeStats(blob_result->unfiltered_blobs); |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 346 | FilterBlobs(blob_result); |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 347 | auto end = aos::monotonic_clock::now(); |
Jim Ostrowski | fec0c33 | 2022-02-06 23:28:26 -0800 | [diff] [blame] | 348 | VLOG(2) << "Blob detection elapsed time: " |
| 349 | << std::chrono::duration<double, std::milli>(end - start).count() |
| 350 | << " ms"; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | } // namespace vision |
| 354 | } // namespace y2022 |