blob: a0663fdaf3ea6a251c153b3cf1233ef3d2801afa [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;
16 size_t points;
17 };
18
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080019 BlobDetector() {}
20 // Given an image, threshold it to find "green" pixels
21 // Input: Color image
22 // Output: Grayscale (binarized) image with green pixels set to 255
23 static cv::Mat ThresholdImage(cv::Mat rgb_image);
24
25 // Given binary image, extract blobs
26 static std::vector<std::vector<cv::Point>> FindBlobs(cv::Mat threshold_image);
27
milind-u61f21e82022-01-23 18:34:11 -080028 // Extract stats for each blob
29 static std::vector<BlobStats> ComputeStats(
30 std::vector<std::vector<cv::Point>> blobs);
31
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -080032 // Filter blobs to get rid of noise, too small/large items, and blobs that
33 // aren't in a circle. Returns a pair of filtered blobs and the average
34 // of their centroids.
35 static std::pair<std::vector<std::vector<cv::Point>>, cv::Point> FilterBlobs(
milind-u61f21e82022-01-23 18:34:11 -080036 std::vector<std::vector<cv::Point>> blobs,
37 std::vector<BlobStats> blob_stats);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080038
39 // Draw Blobs on image
40 // Optionally draw all blobs and filtered blobs
milind-u61f21e82022-01-23 18:34:11 -080041 static void DrawBlobs(
42 cv::Mat view_image,
43 const std::vector<std::vector<cv::Point>> &filtered_blobs,
44 const std::vector<std::vector<cv::Point>> &unfiltered_blobs,
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -080045 const std::vector<BlobStats> &blob_stats, cv::Point centroid);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080046
47 static void ExtractBlobs(
Milind Upadhyay2b4404c2022-02-04 21:20:57 -080048 cv::Mat rgb_image, cv::Mat &binarized_image, cv::Mat blob_image,
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080049 std::vector<std::vector<cv::Point>> &filtered_blobs,
50 std::vector<std::vector<cv::Point>> &unfiltered_blobs,
Milind Upadhyaye7aa40c2022-01-29 22:36:21 -080051 std::vector<BlobStats> &blob_stats, cv::Point &centroid);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080052};
53} // namespace vision
54} // namespace y2022
55
56#endif // Y2022_BLOB_DETECTOR_H_