blob: 84e504dd55d086eb20f64a00c5c34a87a925ab6b [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
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080032 // Filter blobs to get rid of noise, too large items, etc.
33 static std::vector<std::vector<cv::Point>> FilterBlobs(
milind-u61f21e82022-01-23 18:34:11 -080034 std::vector<std::vector<cv::Point>> blobs,
35 std::vector<BlobStats> blob_stats);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080036
37 // Draw Blobs on image
38 // Optionally draw all blobs and filtered blobs
milind-u61f21e82022-01-23 18:34:11 -080039 static void DrawBlobs(
40 cv::Mat view_image,
41 const std::vector<std::vector<cv::Point>> &filtered_blobs,
42 const std::vector<std::vector<cv::Point>> &unfiltered_blobs,
43 const std::vector<BlobStats> &blob_stats);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080044
45 static void ExtractBlobs(
46 cv::Mat rgb_image, cv::Mat binarized_image, cv::Mat blob_image,
47 std::vector<std::vector<cv::Point>> &filtered_blobs,
48 std::vector<std::vector<cv::Point>> &unfiltered_blobs,
milind-u61f21e82022-01-23 18:34:11 -080049 std::vector<BlobStats> &blob_stats);
Jim Ostrowskiff0f5e42022-01-22 01:35:31 -080050};
51} // namespace vision
52} // namespace y2022
53
54#endif // Y2022_BLOB_DETECTOR_H_