blob: 4077e2c6c31870599df0c061428ecd31ff704f18 [file] [log] [blame]
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08001#ifndef Y2022_BLOB_DETECTOR_H_
2#define Y2022_BLOB_DETECTOR_H_
3
milind-u61f21e82022-01-23 18:34:11 -08004#include <opencv2/features2d.hpp>
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08005#include <opencv2/imgproc.hpp>
6
7namespace y2022 {
8namespace vision {
9
10class BlobDetector {
11 public:
milind-u61f21e82022-01-23 18:34:11 -080012 struct BlobStats {
13 cv::Point centroid;
14 double aspect_ratio;
15 double area;
Henry Speisere45e7a22022-02-04 23:17:01 -080016 size_t num_points;
milind-u61f21e82022-01-23 18:34:11 -080017 };
18
Milind Upadhyay25610d22022-02-07 15:35:26 -080019 struct BlobResult {
20 cv::Mat binarized_image;
21 std::vector<std::vector<cv::Point>> filtered_blobs, unfiltered_blobs;
22 std::vector<BlobStats> blob_stats;
23 cv::Point centroid;
24 };
25
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080026 BlobDetector() {}
Milind Upadhyay25610d22022-02-07 15:35:26 -080027
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080028 // Given an image, threshold it to find "green" pixels
29 // Input: Color image
30 // Output: Grayscale (binarized) image with green pixels set to 255
31 static cv::Mat ThresholdImage(cv::Mat rgb_image);
32
33 // Given binary image, extract blobs
34 static std::vector<std::vector<cv::Point>> FindBlobs(cv::Mat threshold_image);
35
milind-u61f21e82022-01-23 18:34:11 -080036 // Extract stats for each blob
37 static std::vector<BlobStats> ComputeStats(
38 std::vector<std::vector<cv::Point>> blobs);
39
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -080040 // Filter blobs to get rid of noise, too small/large items, and blobs that
41 // aren't in a circle. Returns a pair of filtered blobs and the average
42 // of their centroids.
43 static std::pair<std::vector<std::vector<cv::Point>>, cv::Point> FilterBlobs(
milind-u61f21e82022-01-23 18:34:11 -080044 std::vector<std::vector<cv::Point>> blobs,
45 std::vector<BlobStats> blob_stats);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080046
47 // Draw Blobs on image
48 // Optionally draw all blobs and filtered blobs
milind-u61f21e82022-01-23 18:34:11 -080049 static void DrawBlobs(
50 cv::Mat view_image,
51 const std::vector<std::vector<cv::Point>> &filtered_blobs,
52 const std::vector<std::vector<cv::Point>> &unfiltered_blobs,
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -080053 const std::vector<BlobStats> &blob_stats, cv::Point centroid);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080054
Milind Upadhyay25610d22022-02-07 15:35:26 -080055 static void ExtractBlobs(cv::Mat rgb_image, BlobResult *blob_result);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080056};
57} // namespace vision
58} // namespace y2022
59
60#endif // Y2022_BLOB_DETECTOR_H_