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