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; |
Henry Speiser | e45e7a2 | 2022-02-04 23:17:01 -0800 | [diff] [blame] | 16 | size_t num_points; |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 17 | }; |
| 18 | |
Milind Upadhyay | 25610d2 | 2022-02-07 15:35:26 -0800 | [diff] [blame] | 19 | struct BlobResult { |
| 20 | cv::Mat binarized_image; |
| 21 | std::vector<std::vector<cv::Point>> filtered_blobs, unfiltered_blobs; |
| 22 | std::vector<BlobStats> blob_stats; |
| 23 | cv::Point centroid; |
| 24 | }; |
| 25 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 26 | BlobDetector() {} |
Milind Upadhyay | 25610d2 | 2022-02-07 15:35:26 -0800 | [diff] [blame] | 27 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 28 | // Given an image, threshold it to find "green" pixels |
| 29 | // Input: Color image |
| 30 | // Output: Grayscale (binarized) image with green pixels set to 255 |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame^] | 31 | static cv::Mat ThresholdImage(cv::Mat bgr_image); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 32 | |
| 33 | // Given binary image, extract blobs |
| 34 | static std::vector<std::vector<cv::Point>> FindBlobs(cv::Mat threshold_image); |
| 35 | |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 36 | // Extract stats for each blob |
| 37 | static std::vector<BlobStats> ComputeStats( |
| 38 | std::vector<std::vector<cv::Point>> blobs); |
| 39 | |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 40 | // Filter blobs to get rid of noise, too small/large items, and blobs that |
| 41 | // aren't in a circle. Returns a pair of filtered blobs and the average |
| 42 | // of their centroids. |
| 43 | static std::pair<std::vector<std::vector<cv::Point>>, cv::Point> FilterBlobs( |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 44 | std::vector<std::vector<cv::Point>> blobs, |
| 45 | std::vector<BlobStats> blob_stats); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 46 | |
| 47 | // Draw Blobs on image |
| 48 | // Optionally draw all blobs and filtered blobs |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 49 | static void DrawBlobs( |
| 50 | cv::Mat view_image, |
| 51 | const std::vector<std::vector<cv::Point>> &filtered_blobs, |
| 52 | const std::vector<std::vector<cv::Point>> &unfiltered_blobs, |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 53 | const std::vector<BlobStats> &blob_stats, cv::Point centroid); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 54 | |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame^] | 55 | static void ExtractBlobs(cv::Mat bgr_image, BlobResult *blob_result); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 56 | }; |
| 57 | } // namespace vision |
| 58 | } // namespace y2022 |
| 59 | |
| 60 | #endif // Y2022_BLOB_DETECTOR_H_ |