blob: b76ffc570adf5e7270759753c02522f10735211d [file] [log] [blame]
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08001#include "y2022/vision/blob_detector.h"
2
milind-u92195982022-01-22 20:29:31 -08003#include <cmath>
4#include <string>
5
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08006#include "aos/network/team_number.h"
milind-u92195982022-01-22 20:29:31 -08007#include "opencv2/features2d.hpp"
8#include "opencv2/imgproc.hpp"
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08009
10DEFINE_uint64(green_delta, 50,
11 "Required difference between green pixels vs. red and blue");
12DEFINE_bool(use_outdoors, false,
13 "If true, change thresholds to handle outdoor illumination");
14
15namespace y2022 {
16namespace vision {
17
18cv::Mat BlobDetector::ThresholdImage(cv::Mat rgb_image) {
milind-u61f21e82022-01-23 18:34:11 -080019 cv::Mat binarized_image(cv::Size(rgb_image.cols, rgb_image.rows), CV_8UC1);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080020 for (int row = 0; row < rgb_image.rows; row++) {
21 for (int col = 0; col < rgb_image.cols; col++) {
22 cv::Vec3b pixel = rgb_image.at<cv::Vec3b>(row, col);
23 uint8_t blue = pixel.val[0];
24 uint8_t green = pixel.val[1];
25 uint8_t red = pixel.val[2];
26 // Simple filter that looks for green pixels sufficiently brigher than
27 // red and blue
milind-u92195982022-01-22 20:29:31 -080028 if ((green > blue + FLAGS_green_delta) &&
29 (green > red + FLAGS_green_delta)) {
milind-u61f21e82022-01-23 18:34:11 -080030 binarized_image.at<uint8_t>(row, col) = 255;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080031 } else {
milind-u61f21e82022-01-23 18:34:11 -080032 binarized_image.at<uint8_t>(row, col) = 0;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080033 }
34 }
35 }
36
milind-u61f21e82022-01-23 18:34:11 -080037 return binarized_image;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080038}
39
40std::vector<std::vector<cv::Point>> BlobDetector::FindBlobs(
41 cv::Mat binarized_image) {
42 // find the contours (blob outlines)
43 std::vector<std::vector<cv::Point>> contours;
44 std::vector<cv::Vec4i> hierarchy;
45 cv::findContours(binarized_image, contours, hierarchy, cv::RETR_CCOMP,
46 cv::CHAIN_APPROX_SIMPLE);
47
48 return contours;
49}
50
milind-u61f21e82022-01-23 18:34:11 -080051std::vector<BlobDetector::BlobStats> BlobDetector::ComputeStats(
52 std::vector<std::vector<cv::Point>> blobs) {
53 std::vector<BlobDetector::BlobStats> blob_stats;
54 for (auto blob : blobs) {
55 // Make the blob convex before finding bounding box
56 std::vector<cv::Point> convex_blob;
57 cv::convexHull(blob, convex_blob);
58 auto blob_size = cv::boundingRect(convex_blob).size();
59 cv::Moments moments = cv::moments(convex_blob);
60
61 const auto centroid =
62 cv::Point(moments.m10 / moments.m00, moments.m01 / moments.m00);
63 const double aspect_ratio =
64 static_cast<double>(blob_size.width) / blob_size.height;
65 const double area = moments.m00;
66 const size_t points = blob.size();
67
68 blob_stats.emplace_back(BlobStats{centroid, aspect_ratio, area, points});
69 }
70 return blob_stats;
71}
72
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080073// Filter blobs to get rid of noise, too large items, etc.
74std::vector<std::vector<cv::Point>> BlobDetector::FilterBlobs(
milind-u61f21e82022-01-23 18:34:11 -080075 std::vector<std::vector<cv::Point>> blobs,
76 std::vector<BlobDetector::BlobStats> blob_stats) {
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080077 std::vector<std::vector<cv::Point>> filtered_blobs;
milind-u92195982022-01-22 20:29:31 -080078
milind-u61f21e82022-01-23 18:34:11 -080079 auto blob_it = blobs.begin();
80 auto stats_it = blob_stats.begin();
81 while (blob_it < blobs.end() && stats_it < blob_stats.end()) {
milind-u92195982022-01-22 20:29:31 -080082 // To estimate the maximum y, we can figure out the y value of the blobs
83 // when the camera is the farthest from the target, at the field corner.
84 // We can solve for the pitch of the blob:
85 // blob_pitch = atan((height_tape - height_camera) / depth) + camera_pitch
86 // The triangle with the height of the tape above the camera and the camera
87 // depth is similar to the one with the focal length in y pixels and the y
88 // coordinate offset from the center of the image.
89 // Therefore y_offset = focal_length_y * tan(blob_pitch), and
90 // y = -(y_offset - offset_y)
milind-u61f21e82022-01-23 18:34:11 -080091 constexpr int kMaxY = 400;
92 constexpr double kTapeAspectRatio = 5.0 / 2.0;
93 constexpr double kAspectRatioThreshold = 1.5;
94 constexpr double kMinArea = 10;
milind-u92195982022-01-22 20:29:31 -080095 constexpr size_t kMinPoints = 6;
96
milind-u61f21e82022-01-23 18:34:11 -080097 // Remove all blobs that are at the bottom of the image, have a different
98 // aspect ratio than the tape, or have too little area or points
milind-u92195982022-01-22 20:29:31 -080099 // TODO(milind): modify to take into account that blobs will be on the side.
milind-u61f21e82022-01-23 18:34:11 -0800100 if ((stats_it->centroid.y <= kMaxY) &&
101 (std::abs(kTapeAspectRatio - stats_it->aspect_ratio) <
102 kAspectRatioThreshold) &&
103 (stats_it->area >= kMinArea) && (stats_it->points >= kMinPoints)) {
104 filtered_blobs.push_back(*blob_it);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800105 }
milind-u61f21e82022-01-23 18:34:11 -0800106 blob_it++;
107 stats_it++;
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800108 }
milind-u92195982022-01-22 20:29:31 -0800109
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800110 return filtered_blobs;
111}
112
113void BlobDetector::DrawBlobs(
milind-u61f21e82022-01-23 18:34:11 -0800114 cv::Mat view_image,
115 const std::vector<std::vector<cv::Point>> &unfiltered_blobs,
116 const std::vector<std::vector<cv::Point>> &filtered_blobs,
117 const std::vector<BlobStats> &blob_stats) {
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800118 CHECK_GT(view_image.cols, 0);
119 if (unfiltered_blobs.size() > 0) {
120 // Draw blobs unfilled, with red color border
milind-u92195982022-01-22 20:29:31 -0800121 cv::drawContours(view_image, unfiltered_blobs, -1, cv::Scalar(0, 0, 255),
122 0);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800123 }
124
milind-u92195982022-01-22 20:29:31 -0800125 cv::drawContours(view_image, filtered_blobs, -1, cv::Scalar(0, 255, 0),
126 cv::FILLED);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800127
milind-u92195982022-01-22 20:29:31 -0800128 // Draw blob centroids
milind-u61f21e82022-01-23 18:34:11 -0800129 for (auto stats : blob_stats) {
130 cv::circle(view_image, stats.centroid, 2, cv::Scalar(255, 0, 0),
131 cv::FILLED);
132 }
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800133}
134
135void BlobDetector::ExtractBlobs(
136 cv::Mat rgb_image, cv::Mat binarized_image, cv::Mat blob_image,
137 std::vector<std::vector<cv::Point>> &filtered_blobs,
138 std::vector<std::vector<cv::Point>> &unfiltered_blobs,
milind-u61f21e82022-01-23 18:34:11 -0800139 std::vector<BlobStats> &blob_stats) {
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800140 binarized_image = ThresholdImage(rgb_image);
141 unfiltered_blobs = FindBlobs(binarized_image);
milind-u61f21e82022-01-23 18:34:11 -0800142 blob_stats = ComputeStats(unfiltered_blobs);
143 filtered_blobs = FilterBlobs(unfiltered_blobs, blob_stats);
144 DrawBlobs(blob_image, unfiltered_blobs, filtered_blobs, blob_stats);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -0800145}
146
147} // namespace vision
148} // namespace y2022