blob: d0a2b8552c2b98e8b7626691339d881588fcaa47 [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;
Milind Upadhyayf61e1482022-02-11 20:42:55 -080023 // In sorted order from left to right on the circle
24 std::vector<cv::Point> filtered_centroids;
Milind Upadhyay25610d22022-02-07 15:35:26 -080025 cv::Point centroid;
26 };
27
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080028 BlobDetector() {}
Milind Upadhyay25610d22022-02-07 15:35:26 -080029
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080030 // Given an image, threshold it to find "green" pixels
31 // Input: Color image
32 // Output: Grayscale (binarized) image with green pixels set to 255
Milind Upadhyayec41e132022-02-05 17:14:05 -080033 static cv::Mat ThresholdImage(cv::Mat bgr_image);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080034
35 // Given binary image, extract blobs
36 static std::vector<std::vector<cv::Point>> FindBlobs(cv::Mat threshold_image);
37
milind-u61f21e82022-01-23 18:34:11 -080038 // Extract stats for each blob
39 static std::vector<BlobStats> ComputeStats(
Milind Upadhyayf61e1482022-02-11 20:42:55 -080040 const std::vector<std::vector<cv::Point>> &blobs);
milind-u61f21e82022-01-23 18:34:11 -080041
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -080042 // Filter blobs to get rid of noise, too small/large items, and blobs that
Milind Upadhyayf61e1482022-02-11 20:42:55 -080043 // aren't in a circle. Finds the filtered blobs, centroids, and the absolute
44 // centroid.
45 static void FilterBlobs(BlobResult *blob_result);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080046
47 // Draw Blobs on image
48 // Optionally draw all blobs and filtered blobs
Milind Upadhyayf61e1482022-02-11 20:42:55 -080049 static void DrawBlobs(const BlobResult &blob_result, cv::Mat view_image);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080050
Milind Upadhyayec41e132022-02-05 17:14:05 -080051 static void ExtractBlobs(cv::Mat bgr_image, BlobResult *blob_result);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080052};
53} // namespace vision
54} // namespace y2022
55
56#endif // Y2022_BLOB_DETECTOR_H_