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