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