blob: bf5afd7f0c3caa0c048a127e947ac30533aab12c [file] [log] [blame]
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08001#include "y2022/vision/blob_detector.h"
2
3#include <opencv2/imgproc.hpp>
4
5#include "aos/network/team_number.h"
6
7DEFINE_uint64(green_delta, 50,
8 "Required difference between green pixels vs. red and blue");
9DEFINE_bool(use_outdoors, false,
10 "If true, change thresholds to handle outdoor illumination");
11
12namespace y2022 {
13namespace vision {
14
15cv::Mat BlobDetector::ThresholdImage(cv::Mat rgb_image) {
16 cv::Mat gray_image(cv::Size(rgb_image.cols, rgb_image.rows), CV_8UC1);
17 for (int row = 0; row < rgb_image.rows; row++) {
18 for (int col = 0; col < rgb_image.cols; col++) {
19 cv::Vec3b pixel = rgb_image.at<cv::Vec3b>(row, col);
20 uint8_t blue = pixel.val[0];
21 uint8_t green = pixel.val[1];
22 uint8_t red = pixel.val[2];
23 // Simple filter that looks for green pixels sufficiently brigher than
24 // red and blue
25 if ((green > blue + 30) && (green > red + 50)) {
26 gray_image.at<uint8_t>(row, col) = 255;
27 } else {
28 gray_image.at<uint8_t>(row, col) = 0;
29 }
30 }
31 }
32
33 return gray_image;
34}
35
36std::vector<std::vector<cv::Point>> BlobDetector::FindBlobs(
37 cv::Mat binarized_image) {
38 // find the contours (blob outlines)
39 std::vector<std::vector<cv::Point>> contours;
40 std::vector<cv::Vec4i> hierarchy;
41 cv::findContours(binarized_image, contours, hierarchy, cv::RETR_CCOMP,
42 cv::CHAIN_APPROX_SIMPLE);
43
44 return contours;
45}
46
47// Filter blobs to get rid of noise, too large items, etc.
48std::vector<std::vector<cv::Point>> BlobDetector::FilterBlobs(
49 std::vector<std::vector<cv::Point>> blobs) {
50 // TODO: Put in some filters
51
52 std::vector<std::vector<cv::Point>> filtered_blobs;
53 for (auto blob : blobs) {
54 // for now, let's remove all blobs that are at the bottom of the image
55 if (blob[0].y < 400) {
56 filtered_blobs.push_back(blob);
57 } else {
58 // LOG(INFO) << "Found and removed blob";
59 }
60 }
61 return filtered_blobs;
62}
63
64void BlobDetector::DrawBlobs(
65 cv::Mat view_image, std::vector<std::vector<cv::Point>> unfiltered_blobs,
66 std::vector<std::vector<cv::Point>> filtered_blobs) {
67 CHECK_GT(view_image.cols, 0);
68 if (unfiltered_blobs.size() > 0) {
69 // Draw blobs unfilled, with red color border
70 drawContours(view_image, unfiltered_blobs, -1, cv::Scalar(0, 0, 255), 0);
71 }
72
73 drawContours(view_image, filtered_blobs, -1, cv::Scalar(0, 255, 0),
74 cv::FILLED);
75}
76
77std::vector<std::vector<cv::Point>> BlobDetector::ComputeStats(
78 std::vector<std::vector<cv::Point>> blobs) {
79 // Placeholder for now for this
80 // TODO<Jim>: need to compute stats on blobs, like centroid, aspect
81 // ratio, bounding box
82 return blobs;
83}
84
85void BlobDetector::ExtractBlobs(
86 cv::Mat rgb_image, cv::Mat binarized_image, cv::Mat blob_image,
87 std::vector<std::vector<cv::Point>> &filtered_blobs,
88 std::vector<std::vector<cv::Point>> &unfiltered_blobs,
89 std::vector<std::vector<cv::Point>> &blob_stats) {
90 binarized_image = ThresholdImage(rgb_image);
91 unfiltered_blobs = FindBlobs(binarized_image);
92 filtered_blobs = FilterBlobs(unfiltered_blobs);
93 DrawBlobs(blob_image, unfiltered_blobs, filtered_blobs);
94 blob_stats = ComputeStats(filtered_blobs);
95}
96
97} // namespace vision
98} // namespace y2022