blob: 93bba0996db8d160e3ab0353c8eff83db4f89393 [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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -08007namespace y2022::vision {
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -08008
9class BlobDetector {
10 public:
milind-u61f21e82022-01-23 18:34:11 -080011 struct BlobStats {
12 cv::Point centroid;
Milind Upadhyay8f38ad82022-03-03 10:06:18 -080013 // Size of the rotated rect fitting around the blob
14 cv::Size size;
15 // Aspect ratio of the non-rotated bounding box
milind-u61f21e82022-01-23 18:34:11 -080016 double aspect_ratio;
17 double area;
Henry Speisere45e7a22022-02-04 23:17:01 -080018 size_t num_points;
milind-u61f21e82022-01-23 18:34:11 -080019 };
20
Milind Upadhyay25610d22022-02-07 15:35:26 -080021 struct BlobResult {
22 cv::Mat binarized_image;
23 std::vector<std::vector<cv::Point>> filtered_blobs, unfiltered_blobs;
24 std::vector<BlobStats> blob_stats;
Milind Upadhyay8f38ad82022-03-03 10:06:18 -080025 std::vector<BlobStats> filtered_stats;
Milind Upadhyay25610d22022-02-07 15:35:26 -080026 cv::Point centroid;
27 };
28
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080029 BlobDetector() {}
Milind Upadhyay25610d22022-02-07 15:35:26 -080030
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080031 // Given an image, threshold it to find "green" pixels
32 // Input: Color image
33 // Output: Grayscale (binarized) image with green pixels set to 255
Milind Upadhyayec41e132022-02-05 17:14:05 -080034 static cv::Mat ThresholdImage(cv::Mat bgr_image);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080035
36 // Given binary image, extract blobs
37 static std::vector<std::vector<cv::Point>> FindBlobs(cv::Mat threshold_image);
38
milind-u61f21e82022-01-23 18:34:11 -080039 // Extract stats for each blob
40 static std::vector<BlobStats> ComputeStats(
Milind Upadhyayf61e1482022-02-11 20:42:55 -080041 const std::vector<std::vector<cv::Point>> &blobs);
milind-u61f21e82022-01-23 18:34:11 -080042
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -080043 // Filter blobs to get rid of noise, too small/large items, and blobs that
Milind Upadhyayf61e1482022-02-11 20:42:55 -080044 // aren't in a circle. Finds the filtered blobs, centroids, and the absolute
45 // centroid.
46 static void FilterBlobs(BlobResult *blob_result);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080047
48 // Draw Blobs on image
49 // Optionally draw all blobs and filtered blobs
Milind Upadhyayf61e1482022-02-11 20:42:55 -080050 static void DrawBlobs(const BlobResult &blob_result, cv::Mat view_image);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080051
Milind Upadhyayec41e132022-02-05 17:14:05 -080052 static void ExtractBlobs(cv::Mat bgr_image, BlobResult *blob_result);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080053};
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080054} // namespace y2022::vision
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080055
56#endif // Y2022_BLOB_DETECTOR_H_