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