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> |
| 4 | #include <string> |
| 5 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 6 | #include "aos/network/team_number.h" |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame^] | 7 | #include "opencv2/features2d.hpp" |
| 8 | #include "opencv2/imgproc.hpp" |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 9 | |
| 10 | DEFINE_uint64(green_delta, 50, |
| 11 | "Required difference between green pixels vs. red and blue"); |
| 12 | DEFINE_bool(use_outdoors, false, |
| 13 | "If true, change thresholds to handle outdoor illumination"); |
| 14 | |
| 15 | namespace y2022 { |
| 16 | namespace vision { |
| 17 | |
| 18 | cv::Mat BlobDetector::ThresholdImage(cv::Mat rgb_image) { |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 19 | cv::Mat binarized_image(cv::Size(rgb_image.cols, rgb_image.rows), CV_8UC1); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 20 | 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-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame^] | 28 | if ((green > blue + FLAGS_green_delta) && |
| 29 | (green > red + FLAGS_green_delta)) { |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 30 | binarized_image.at<uint8_t>(row, col) = 255; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 31 | } else { |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 32 | binarized_image.at<uint8_t>(row, col) = 0; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 37 | return binarized_image; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | std::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-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 51 | std::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 Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 73 | // Filter blobs to get rid of noise, too large items, etc. |
| 74 | std::vector<std::vector<cv::Point>> BlobDetector::FilterBlobs( |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 75 | std::vector<std::vector<cv::Point>> blobs, |
| 76 | std::vector<BlobDetector::BlobStats> blob_stats) { |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 77 | std::vector<std::vector<cv::Point>> filtered_blobs; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame^] | 78 | |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 79 | auto blob_it = blobs.begin(); |
| 80 | auto stats_it = blob_stats.begin(); |
| 81 | while (blob_it < blobs.end() && stats_it < blob_stats.end()) { |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame^] | 82 | // 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-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 91 | constexpr int kMaxY = 400; |
| 92 | constexpr double kTapeAspectRatio = 5.0 / 2.0; |
| 93 | constexpr double kAspectRatioThreshold = 1.5; |
| 94 | constexpr double kMinArea = 10; |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame^] | 95 | constexpr size_t kMinPoints = 6; |
| 96 | |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 97 | // 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-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame^] | 99 | // TODO(milind): modify to take into account that blobs will be on the side. |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 100 | 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 Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 105 | } |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 106 | blob_it++; |
| 107 | stats_it++; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 108 | } |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame^] | 109 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 110 | return filtered_blobs; |
| 111 | } |
| 112 | |
| 113 | void BlobDetector::DrawBlobs( |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 114 | 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 Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 118 | CHECK_GT(view_image.cols, 0); |
| 119 | if (unfiltered_blobs.size() > 0) { |
| 120 | // Draw blobs unfilled, with red color border |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame^] | 121 | cv::drawContours(view_image, unfiltered_blobs, -1, cv::Scalar(0, 0, 255), |
| 122 | 0); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 123 | } |
| 124 | |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame^] | 125 | cv::drawContours(view_image, filtered_blobs, -1, cv::Scalar(0, 255, 0), |
| 126 | cv::FILLED); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 127 | |
milind-u | 9219598 | 2022-01-22 20:29:31 -0800 | [diff] [blame^] | 128 | // Draw blob centroids |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 129 | for (auto stats : blob_stats) { |
| 130 | cv::circle(view_image, stats.centroid, 2, cv::Scalar(255, 0, 0), |
| 131 | cv::FILLED); |
| 132 | } |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void 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-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 139 | std::vector<BlobStats> &blob_stats) { |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 140 | binarized_image = ThresholdImage(rgb_image); |
| 141 | unfiltered_blobs = FindBlobs(binarized_image); |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 142 | blob_stats = ComputeStats(unfiltered_blobs); |
| 143 | filtered_blobs = FilterBlobs(unfiltered_blobs, blob_stats); |
| 144 | DrawBlobs(blob_image, unfiltered_blobs, filtered_blobs, blob_stats); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | } // namespace vision |
| 148 | } // namespace y2022 |