Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 1 | #ifndef Y2022_BLOB_DETECTOR_H_ |
| 2 | #define Y2022_BLOB_DETECTOR_H_ |
| 3 | |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 4 | #include <opencv2/features2d.hpp> |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 5 | #include <opencv2/imgproc.hpp> |
| 6 | |
| 7 | namespace y2022 { |
| 8 | namespace vision { |
| 9 | |
| 10 | class BlobDetector { |
| 11 | public: |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 12 | struct BlobStats { |
| 13 | cv::Point centroid; |
| 14 | double aspect_ratio; |
| 15 | double area; |
| 16 | size_t points; |
| 17 | }; |
| 18 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 19 | 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-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 28 | // Extract stats for each blob |
| 29 | static std::vector<BlobStats> ComputeStats( |
| 30 | std::vector<std::vector<cv::Point>> blobs); |
| 31 | |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame^] | 32 | // 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-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 36 | std::vector<std::vector<cv::Point>> blobs, |
| 37 | std::vector<BlobStats> blob_stats); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 38 | |
| 39 | // Draw Blobs on image |
| 40 | // Optionally draw all blobs and filtered blobs |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 41 | 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 Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame^] | 45 | const std::vector<BlobStats> &blob_stats, cv::Point centroid); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 46 | |
| 47 | static void ExtractBlobs( |
| 48 | cv::Mat rgb_image, cv::Mat binarized_image, cv::Mat blob_image, |
| 49 | std::vector<std::vector<cv::Point>> &filtered_blobs, |
| 50 | std::vector<std::vector<cv::Point>> &unfiltered_blobs, |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame^] | 51 | std::vector<BlobStats> &blob_stats, cv::Point ¢roid); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 52 | }; |
| 53 | } // namespace vision |
| 54 | } // namespace y2022 |
| 55 | |
| 56 | #endif // Y2022_BLOB_DETECTOR_H_ |