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