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" |
milind-u | db98afa | 2022-03-01 19:54:57 -0800 | [diff] [blame] | 11 | #include "y2022/vision/geometry.h" |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 12 | |
Yash Chainani | 6acad6f | 2022-02-03 10:52:53 -0800 | [diff] [blame] | 13 | DEFINE_uint64(red_delta, 100, |
| 14 | "Required difference between green pixels vs. red"); |
| 15 | DEFINE_uint64(blue_delta, 50, |
| 16 | "Required difference between green pixels vs. blue"); |
| 17 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 18 | DEFINE_bool(use_outdoors, false, |
| 19 | "If true, change thresholds to handle outdoor illumination"); |
Yash Chainani | 6acad6f | 2022-02-03 10:52:53 -0800 | [diff] [blame] | 20 | DEFINE_uint64(outdoors_red_delta, 100, |
| 21 | "Difference between green pixels vs. red, when outdoors"); |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 22 | DEFINE_uint64(outdoors_blue_delta, 1, |
Yash Chainani | 6acad6f | 2022-02-03 10:52:53 -0800 | [diff] [blame] | 23 | "Difference between green pixels vs. blue, when outdoors"); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 24 | |
| 25 | namespace y2022 { |
| 26 | namespace vision { |
| 27 | |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 28 | cv::Mat BlobDetector::ThresholdImage(cv::Mat bgr_image) { |
Yash Chainani | 6acad6f | 2022-02-03 10:52:53 -0800 | [diff] [blame] | 29 | size_t red_delta = FLAGS_red_delta; |
| 30 | size_t blue_delta = FLAGS_blue_delta; |
| 31 | |
| 32 | if (FLAGS_use_outdoors) { |
| 33 | red_delta = FLAGS_outdoors_red_delta; |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 34 | blue_delta = FLAGS_outdoors_blue_delta; |
Yash Chainani | 6acad6f | 2022-02-03 10:52:53 -0800 | [diff] [blame] | 35 | } |
| 36 | |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 37 | cv::Mat binarized_image(cv::Size(bgr_image.cols, bgr_image.rows), CV_8UC1); |
| 38 | for (int row = 0; row < bgr_image.rows; row++) { |
| 39 | for (int col = 0; col < bgr_image.cols; col++) { |
| 40 | cv::Vec3b pixel = bgr_image.at<cv::Vec3b>(row, col); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 41 | uint8_t blue = pixel.val[0]; |
| 42 | uint8_t green = pixel.val[1]; |
| 43 | uint8_t red = pixel.val[2]; |
| 44 | // Simple filter that looks for green pixels sufficiently brigher than |
| 45 | // red and blue |
Yash Chainani | 6acad6f | 2022-02-03 10:52:53 -0800 | [diff] [blame] | 46 | if ((green > blue + blue_delta) && (green > red + red_delta)) { |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 47 | binarized_image.at<uint8_t>(row, col) = 255; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 48 | } else { |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 49 | binarized_image.at<uint8_t>(row, col) = 0; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 50 | } |
| 51 | } |
| 52 | } |
| 53 | |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 54 | return binarized_image; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | std::vector<std::vector<cv::Point>> BlobDetector::FindBlobs( |
| 58 | cv::Mat binarized_image) { |
| 59 | // find the contours (blob outlines) |
| 60 | std::vector<std::vector<cv::Point>> contours; |
| 61 | std::vector<cv::Vec4i> hierarchy; |
| 62 | cv::findContours(binarized_image, contours, hierarchy, cv::RETR_CCOMP, |
| 63 | cv::CHAIN_APPROX_SIMPLE); |
| 64 | |
| 65 | return contours; |
| 66 | } |
| 67 | |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 68 | std::vector<BlobDetector::BlobStats> BlobDetector::ComputeStats( |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 69 | const std::vector<std::vector<cv::Point>> &blobs) { |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 70 | std::vector<BlobDetector::BlobStats> blob_stats; |
| 71 | for (auto blob : blobs) { |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 72 | auto blob_size = cv::boundingRect(blob).size(); |
| 73 | cv::Moments moments = cv::moments(blob); |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 74 | |
| 75 | const auto centroid = |
| 76 | cv::Point(moments.m10 / moments.m00, moments.m01 / moments.m00); |
| 77 | const double aspect_ratio = |
| 78 | static_cast<double>(blob_size.width) / blob_size.height; |
| 79 | const double area = moments.m00; |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame] | 80 | const size_t num_points = blob.size(); |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 81 | |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame] | 82 | blob_stats.emplace_back( |
| 83 | BlobStats{centroid, aspect_ratio, area, num_points}); |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 84 | } |
| 85 | return blob_stats; |
| 86 | } |
| 87 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 88 | void BlobDetector::FilterBlobs(BlobResult *blob_result) { |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 89 | std::vector<std::vector<cv::Point>> filtered_blobs; |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 90 | std::vector<BlobStats> filtered_stats; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 91 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 92 | auto blob_it = blob_result->unfiltered_blobs.begin(); |
| 93 | auto stats_it = blob_result->blob_stats.begin(); |
| 94 | while (blob_it < blob_result->unfiltered_blobs.end() && |
| 95 | stats_it < blob_result->blob_stats.end()) { |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 96 | constexpr double kTapeAspectRatio = 5.0 / 2.0; |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 97 | constexpr double kAspectRatioThreshold = 1.6; |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 98 | constexpr double kMinArea = 10; |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 99 | constexpr size_t kMinNumPoints = 6; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 100 | |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 101 | // 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] | 102 | // aspect ratio than the tape, or have too little area or points. |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 103 | if ((std::abs(1.0 - kTapeAspectRatio / stats_it->aspect_ratio) < |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 104 | kAspectRatioThreshold) && |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 105 | (stats_it->area >= kMinArea) && |
| 106 | (stats_it->num_points >= kMinNumPoints)) { |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 107 | filtered_blobs.push_back(*blob_it); |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 108 | filtered_stats.push_back(*stats_it); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 109 | } |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 110 | blob_it++; |
| 111 | stats_it++; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 112 | } |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 113 | |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 114 | // Threshold for mean distance from a blob centroid to a circle. |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 115 | constexpr double kCircleDistanceThreshold = 10.0; |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 116 | // 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] | 117 | constexpr double kDegToRad = M_PI / 180.0; |
| 118 | constexpr double kMinBlobAngle = 50.0 * kDegToRad; |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 119 | constexpr double kMaxBlobAngle = M_PI - kMinBlobAngle; |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 120 | std::vector<std::vector<cv::Point>> blob_circle; |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 121 | Circle circle; |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 122 | std::vector<cv::Point2d> centroids; |
| 123 | |
| 124 | // If we see more than this number of blobs after filtering based on |
| 125 | // 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] | 126 | constexpr size_t kMinFilteredBlobs = 3; |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 127 | constexpr size_t kMaxFilteredBlobs = 50; |
Milind Upadhyay | 2b4404c | 2022-02-04 21:20:57 -0800 | [diff] [blame] | 128 | if (filtered_blobs.size() >= kMinFilteredBlobs && |
| 129 | filtered_blobs.size() <= kMaxFilteredBlobs) { |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 130 | constexpr size_t kRansacIterations = 15; |
| 131 | for (size_t i = 0; i < kRansacIterations; i++) { |
| 132 | // Pick 3 random blobs and see how many fit on their circle |
| 133 | const size_t j = std::rand() % filtered_blobs.size(); |
| 134 | const size_t k = std::rand() % filtered_blobs.size(); |
| 135 | const size_t l = std::rand() % filtered_blobs.size(); |
| 136 | |
| 137 | // Restart if the random indices clash |
| 138 | if ((j == k) || (j == l) || (k == l)) { |
| 139 | i--; |
| 140 | continue; |
| 141 | } |
| 142 | |
| 143 | std::vector<std::vector<cv::Point>> current_blobs{ |
| 144 | filtered_blobs[j], filtered_blobs[k], filtered_blobs[l]}; |
| 145 | std::vector<cv::Point2d> current_centroids{filtered_stats[j].centroid, |
| 146 | filtered_stats[k].centroid, |
| 147 | filtered_stats[l].centroid}; |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 148 | const std::optional<Circle> current_circle = |
| 149 | Circle::Fit(current_centroids); |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 150 | |
| 151 | // Make sure that a circle could be created from the points |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 152 | if (!current_circle) { |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 153 | continue; |
| 154 | } |
| 155 | |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 156 | // Only try to fit points to this circle if all of these are between |
| 157 | // certain angles. |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 158 | if (current_circle->InAngleRange(current_centroids[0], kMinBlobAngle, |
| 159 | kMaxBlobAngle) && |
| 160 | current_circle->InAngleRange(current_centroids[1], kMinBlobAngle, |
| 161 | kMaxBlobAngle) && |
| 162 | current_circle->InAngleRange(current_centroids[2], kMinBlobAngle, |
| 163 | kMaxBlobAngle)) { |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 164 | for (size_t m = 0; m < filtered_blobs.size(); m++) { |
| 165 | // Add this blob to the list if it is close to the circle, is on the |
| 166 | // top half, and isn't one of the other blobs |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 167 | if ((m != j) && (m != k) && (m != l) && |
| 168 | current_circle->InAngleRange(filtered_stats[m].centroid, |
| 169 | kMinBlobAngle, kMaxBlobAngle) && |
| 170 | (current_circle->DistanceTo(filtered_stats[m].centroid) < |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 171 | kCircleDistanceThreshold)) { |
| 172 | current_blobs.emplace_back(filtered_blobs[m]); |
| 173 | current_centroids.emplace_back(filtered_stats[m].centroid); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (current_blobs.size() > blob_circle.size()) { |
| 178 | blob_circle = current_blobs; |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 179 | circle = *current_circle; |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 180 | centroids = current_centroids; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | cv::Point avg_centroid(-1, -1); |
| 187 | if (centroids.size() > 0) { |
| 188 | for (auto centroid : centroids) { |
| 189 | avg_centroid.x += centroid.x; |
| 190 | avg_centroid.y += centroid.y; |
| 191 | } |
| 192 | avg_centroid.x /= centroids.size(); |
| 193 | avg_centroid.y /= centroids.size(); |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 194 | |
| 195 | for (auto centroid : centroids) { |
| 196 | blob_result->filtered_centroids.emplace_back( |
| 197 | static_cast<int>(centroid.x), static_cast<int>(centroid.y)); |
| 198 | } |
| 199 | |
| 200 | // Sort the filtered centroids to make them go from left to right |
| 201 | std::sort(blob_result->filtered_centroids.begin(), |
| 202 | blob_result->filtered_centroids.end(), |
| 203 | [&circle](cv::Point p, cv::Point q) { |
| 204 | // If the angle is greater, it is more left and should be |
| 205 | // considered "less" for sorting |
| 206 | return circle.AngleOf(p) > circle.AngleOf(q); |
| 207 | }); |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 210 | blob_result->filtered_blobs = blob_circle; |
| 211 | blob_result->centroid = avg_centroid; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 212 | } |
| 213 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 214 | void BlobDetector::DrawBlobs(const BlobResult &blob_result, |
| 215 | cv::Mat view_image) { |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 216 | CHECK_GT(view_image.cols, 0); |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 217 | if (blob_result.unfiltered_blobs.size() > 0) { |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 218 | // Draw blobs unfilled, with red color border |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 219 | cv::drawContours(view_image, blob_result.unfiltered_blobs, -1, |
| 220 | cv::Scalar(0, 0, 255), 0); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 221 | } |
| 222 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 223 | cv::drawContours(view_image, blob_result.filtered_blobs, -1, |
| 224 | cv::Scalar(0, 100, 0), cv::FILLED); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 225 | |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 226 | static constexpr double kCircleRadius = 2.0; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame] | 227 | // Draw blob centroids |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 228 | for (auto stats : blob_result.blob_stats) { |
| 229 | cv::circle(view_image, stats.centroid, kCircleRadius, |
| 230 | cv::Scalar(0, 215, 255), cv::FILLED); |
| 231 | } |
| 232 | for (auto centroid : blob_result.filtered_centroids) { |
| 233 | cv::circle(view_image, centroid, kCircleRadius, cv::Scalar(0, 255, 0), |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 234 | cv::FILLED); |
| 235 | } |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 236 | |
| 237 | // Draw average centroid |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 238 | cv::circle(view_image, blob_result.centroid, kCircleRadius, |
| 239 | cv::Scalar(255, 255, 0), cv::FILLED); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 242 | void BlobDetector::ExtractBlobs(cv::Mat bgr_image, |
Milind Upadhyay | 25610d2 | 2022-02-07 15:35:26 -0800 | [diff] [blame] | 243 | BlobDetector::BlobResult *blob_result) { |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 244 | auto start = aos::monotonic_clock::now(); |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 245 | blob_result->binarized_image = ThresholdImage(bgr_image); |
Milind Upadhyay | 25610d2 | 2022-02-07 15:35:26 -0800 | [diff] [blame] | 246 | blob_result->unfiltered_blobs = FindBlobs(blob_result->binarized_image); |
| 247 | blob_result->blob_stats = ComputeStats(blob_result->unfiltered_blobs); |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame] | 248 | FilterBlobs(blob_result); |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 249 | auto end = aos::monotonic_clock::now(); |
Jim Ostrowski | fec0c33 | 2022-02-06 23:28:26 -0800 | [diff] [blame] | 250 | VLOG(2) << "Blob detection elapsed time: " |
| 251 | << std::chrono::duration<double, std::milli>(end - start).count() |
| 252 | << " ms"; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | } // namespace vision |
| 256 | } // namespace y2022 |