Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 1 | #include "y2022/vision/blob_detector.h" |
| 2 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 3 | #include "aos/network/team_number.h" |
| 4 | |
| 5 | DEFINE_uint64(green_delta, 50, |
| 6 | "Required difference between green pixels vs. red and blue"); |
| 7 | DEFINE_bool(use_outdoors, false, |
| 8 | "If true, change thresholds to handle outdoor illumination"); |
| 9 | |
| 10 | namespace y2022 { |
| 11 | namespace vision { |
| 12 | |
| 13 | cv::Mat BlobDetector::ThresholdImage(cv::Mat rgb_image) { |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame^] | 14 | 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] | 15 | 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-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame^] | 24 | binarized_image.at<uint8_t>(row, col) = 255; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 25 | } else { |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame^] | 26 | binarized_image.at<uint8_t>(row, col) = 0; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame^] | 31 | return binarized_image; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | std::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-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame^] | 45 | std::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 Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 67 | // Filter blobs to get rid of noise, too large items, etc. |
| 68 | std::vector<std::vector<cv::Point>> BlobDetector::FilterBlobs( |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame^] | 69 | std::vector<std::vector<cv::Point>> blobs, |
| 70 | std::vector<BlobDetector::BlobStats> blob_stats) { |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 71 | std::vector<std::vector<cv::Point>> filtered_blobs; |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame^] | 72 | 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 Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 87 | } |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame^] | 88 | blob_it++; |
| 89 | stats_it++; |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 90 | } |
| 91 | return filtered_blobs; |
| 92 | } |
| 93 | |
| 94 | void BlobDetector::DrawBlobs( |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame^] | 95 | 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 Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 99 | 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 Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 107 | |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame^] | 108 | for (auto stats : blob_stats) { |
| 109 | cv::circle(view_image, stats.centroid, 2, cv::Scalar(255, 0, 0), |
| 110 | cv::FILLED); |
| 111 | } |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | void 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-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame^] | 118 | std::vector<BlobStats> &blob_stats) { |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 119 | binarized_image = ThresholdImage(rgb_image); |
| 120 | unfiltered_blobs = FindBlobs(binarized_image); |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame^] | 121 | blob_stats = ComputeStats(unfiltered_blobs); |
| 122 | filtered_blobs = FilterBlobs(unfiltered_blobs, blob_stats); |
| 123 | DrawBlobs(blob_image, unfiltered_blobs, filtered_blobs, blob_stats); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | } // namespace vision |
| 127 | } // namespace y2022 |