blob: a60316a3b6e1b45e8b0d4c748fb7991a3aef5ed9 [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;
Milind Upadhyay8f38ad82022-03-03 10:06:18 -080014 // Size of the rotated rect fitting around the blob
15 cv::Size size;
16 // Aspect ratio of the non-rotated bounding box
milind-u61f21e82022-01-23 18:34:11 -080017 double aspect_ratio;
18 double area;
Henry Speisere45e7a22022-02-04 23:17:01 -080019 size_t num_points;
milind-u61f21e82022-01-23 18:34:11 -080020 };
21
Milind Upadhyay25610d22022-02-07 15:35:26 -080022 struct BlobResult {
23 cv::Mat binarized_image;
24 std::vector<std::vector<cv::Point>> filtered_blobs, unfiltered_blobs;
25 std::vector<BlobStats> blob_stats;
Milind Upadhyay8f38ad82022-03-03 10:06:18 -080026 std::vector<BlobStats> filtered_stats;
Milind Upadhyay25610d22022-02-07 15:35:26 -080027 cv::Point centroid;
28 };
29
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080030 BlobDetector() {}
Milind Upadhyay25610d22022-02-07 15:35:26 -080031
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080032 // Given an image, threshold it to find "green" pixels
33 // Input: Color image
34 // Output: Grayscale (binarized) image with green pixels set to 255
Milind Upadhyayec41e132022-02-05 17:14:05 -080035 static cv::Mat ThresholdImage(cv::Mat bgr_image);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080036
37 // Given binary image, extract blobs
38 static std::vector<std::vector<cv::Point>> FindBlobs(cv::Mat threshold_image);
39
milind-u61f21e82022-01-23 18:34:11 -080040 // Extract stats for each blob
41 static std::vector<BlobStats> ComputeStats(
Milind Upadhyayf61e1482022-02-11 20:42:55 -080042 const std::vector<std::vector<cv::Point>> &blobs);
milind-u61f21e82022-01-23 18:34:11 -080043
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -080044 // Filter blobs to get rid of noise, too small/large items, and blobs that
Milind Upadhyayf61e1482022-02-11 20:42:55 -080045 // aren't in a circle. Finds the filtered blobs, centroids, and the absolute
46 // centroid.
47 static void FilterBlobs(BlobResult *blob_result);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080048
49 // Draw Blobs on image
50 // Optionally draw all blobs and filtered blobs
Milind Upadhyayf61e1482022-02-11 20:42:55 -080051 static void DrawBlobs(const BlobResult &blob_result, cv::Mat view_image);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080052
Milind Upadhyayec41e132022-02-05 17:14:05 -080053 static void ExtractBlobs(cv::Mat bgr_image, BlobResult *blob_result);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080054};
55} // namespace vision
56} // namespace y2022
57
58#endif // Y2022_BLOB_DETECTOR_H_