blob: 737363defcac70558e4121ff21bebdac61ce4529 [file] [log] [blame]
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08001#include "y2022/vision/blob_detector.h"
2
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08003#include "aos/network/team_number.h"
4
5DEFINE_uint64(green_delta, 50,
6 "Required difference between green pixels vs. red and blue");
7DEFINE_bool(use_outdoors, false,
8 "If true, change thresholds to handle outdoor illumination");
9
10namespace y2022 {
11namespace vision {
12
13cv::Mat BlobDetector::ThresholdImage(cv::Mat rgb_image) {
milind-u61f21e82022-01-23 18:34:11 -080014 cv::Mat binarized_image(cv::Size(rgb_image.cols, rgb_image.rows), CV_8UC1);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080015 for (int row = 0; row < rgb_image.rows; row++) {
16 for (int col = 0; col < rgb_image.cols; col++) {
17 cv::Vec3b pixel = rgb_image.at<cv::Vec3b>(row, col);
18 uint8_t blue = pixel.val[0];
19 uint8_t green = pixel.val[1];
20 uint8_t red = pixel.val[2];
21 // Simple filter that looks for green pixels sufficiently brigher than
22 // red and blue
23 if ((green > blue + 30) && (green > red + 50)) {
milind-u61f21e82022-01-23 18:34:11 -080024 binarized_image.at<uint8_t>(row, col) = 255;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080025 } else {
milind-u61f21e82022-01-23 18:34:11 -080026 binarized_image.at<uint8_t>(row, col) = 0;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080027 }
28 }
29 }
30
milind-u61f21e82022-01-23 18:34:11 -080031 return binarized_image;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080032}
33
34std::vector<std::vector<cv::Point>> BlobDetector::FindBlobs(
35 cv::Mat binarized_image) {
36 // find the contours (blob outlines)
37 std::vector<std::vector<cv::Point>> contours;
38 std::vector<cv::Vec4i> hierarchy;
39 cv::findContours(binarized_image, contours, hierarchy, cv::RETR_CCOMP,
40 cv::CHAIN_APPROX_SIMPLE);
41
42 return contours;
43}
44
milind-u61f21e82022-01-23 18:34:11 -080045std::vector<BlobDetector::BlobStats> BlobDetector::ComputeStats(
46 std::vector<std::vector<cv::Point>> blobs) {
47 std::vector<BlobDetector::BlobStats> blob_stats;
48 for (auto blob : blobs) {
49 // Make the blob convex before finding bounding box
50 std::vector<cv::Point> convex_blob;
51 cv::convexHull(blob, convex_blob);
52 auto blob_size = cv::boundingRect(convex_blob).size();
53 cv::Moments moments = cv::moments(convex_blob);
54
55 const auto centroid =
56 cv::Point(moments.m10 / moments.m00, moments.m01 / moments.m00);
57 const double aspect_ratio =
58 static_cast<double>(blob_size.width) / blob_size.height;
59 const double area = moments.m00;
60 const size_t points = blob.size();
61
62 blob_stats.emplace_back(BlobStats{centroid, aspect_ratio, area, points});
63 }
64 return blob_stats;
65}
66
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080067// Filter blobs to get rid of noise, too large items, etc.
68std::vector<std::vector<cv::Point>> BlobDetector::FilterBlobs(
milind-u61f21e82022-01-23 18:34:11 -080069 std::vector<std::vector<cv::Point>> blobs,
70 std::vector<BlobDetector::BlobStats> blob_stats) {
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080071 std::vector<std::vector<cv::Point>> filtered_blobs;
milind-u61f21e82022-01-23 18:34:11 -080072 auto blob_it = blobs.begin();
73 auto stats_it = blob_stats.begin();
74 while (blob_it < blobs.end() && stats_it < blob_stats.end()) {
75 constexpr int kMaxY = 400;
76 constexpr double kTapeAspectRatio = 5.0 / 2.0;
77 constexpr double kAspectRatioThreshold = 1.5;
78 constexpr double kMinArea = 10;
79 constexpr size_t kMinPoints = 2;
80 // Remove all blobs that are at the bottom of the image, have a different
81 // aspect ratio than the tape, or have too little area or points
82 if ((stats_it->centroid.y <= kMaxY) &&
83 (std::abs(kTapeAspectRatio - stats_it->aspect_ratio) <
84 kAspectRatioThreshold) &&
85 (stats_it->area >= kMinArea) && (stats_it->points >= kMinPoints)) {
86 filtered_blobs.push_back(*blob_it);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080087 }
milind-u61f21e82022-01-23 18:34:11 -080088 blob_it++;
89 stats_it++;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080090 }
91 return filtered_blobs;
92}
93
94void BlobDetector::DrawBlobs(
milind-u61f21e82022-01-23 18:34:11 -080095 cv::Mat view_image,
96 const std::vector<std::vector<cv::Point>> &unfiltered_blobs,
97 const std::vector<std::vector<cv::Point>> &filtered_blobs,
98 const std::vector<BlobStats> &blob_stats) {
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080099 CHECK_GT(view_image.cols, 0);
100 if (unfiltered_blobs.size() > 0) {
101 // Draw blobs unfilled, with red color border
102 drawContours(view_image, unfiltered_blobs, -1, cv::Scalar(0, 0, 255), 0);
103 }
104
105 drawContours(view_image, filtered_blobs, -1, cv::Scalar(0, 255, 0),
106 cv::FILLED);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800107
milind-u61f21e82022-01-23 18:34:11 -0800108 for (auto stats : blob_stats) {
109 cv::circle(view_image, stats.centroid, 2, cv::Scalar(255, 0, 0),
110 cv::FILLED);
111 }
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800112}
113
114void BlobDetector::ExtractBlobs(
115 cv::Mat rgb_image, cv::Mat binarized_image, cv::Mat blob_image,
116 std::vector<std::vector<cv::Point>> &filtered_blobs,
117 std::vector<std::vector<cv::Point>> &unfiltered_blobs,
milind-u61f21e82022-01-23 18:34:11 -0800118 std::vector<BlobStats> &blob_stats) {
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800119 binarized_image = ThresholdImage(rgb_image);
120 unfiltered_blobs = FindBlobs(binarized_image);
milind-u61f21e82022-01-23 18:34:11 -0800121 blob_stats = ComputeStats(unfiltered_blobs);
122 filtered_blobs = FilterBlobs(unfiltered_blobs, blob_stats);
123 DrawBlobs(blob_image, unfiltered_blobs, filtered_blobs, blob_stats);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800124}
125
126} // namespace vision
127} // namespace y2022