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; |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame^] | 23 | // In sorted order from left to right on the circle |
| 24 | std::vector<cv::Point> filtered_centroids; |
Milind Upadhyay | 25610d2 | 2022-02-07 15:35:26 -0800 | [diff] [blame] | 25 | cv::Point centroid; |
| 26 | }; |
| 27 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 28 | BlobDetector() {} |
Milind Upadhyay | 25610d2 | 2022-02-07 15:35:26 -0800 | [diff] [blame] | 29 | |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 30 | // Given an image, threshold it to find "green" pixels |
| 31 | // Input: Color image |
| 32 | // Output: Grayscale (binarized) image with green pixels set to 255 |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 33 | static cv::Mat ThresholdImage(cv::Mat bgr_image); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 34 | |
| 35 | // Given binary image, extract blobs |
| 36 | static std::vector<std::vector<cv::Point>> FindBlobs(cv::Mat threshold_image); |
| 37 | |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 38 | // Extract stats for each blob |
| 39 | static std::vector<BlobStats> ComputeStats( |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame^] | 40 | const std::vector<std::vector<cv::Point>> &blobs); |
milind-u | 61f21e8 | 2022-01-23 18:34:11 -0800 | [diff] [blame] | 41 | |
Milind Upadhyay | e7aa40c | 2022-01-29 22:36:21 -0800 | [diff] [blame] | 42 | // Filter blobs to get rid of noise, too small/large items, and blobs that |
Milind Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame^] | 43 | // aren't in a circle. Finds the filtered blobs, centroids, and the absolute |
| 44 | // centroid. |
| 45 | static void FilterBlobs(BlobResult *blob_result); |
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 Upadhyay | f61e148 | 2022-02-11 20:42:55 -0800 | [diff] [blame^] | 49 | static void DrawBlobs(const BlobResult &blob_result, cv::Mat view_image); |
Jim Ostrowski | ff0f5e4 | 2022-01-22 01:35:31 -0800 | [diff] [blame] | 50 | |
Milind Upadhyay | ec41e13 | 2022-02-05 17:14:05 -0800 | [diff] [blame] | 51 | static void ExtractBlobs(cv::Mat bgr_image, BlobResult *blob_result); |
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_ |