Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 1 | #ifndef _AOS_VISION_BLOB_RANGE_IMAGE_H_ |
| 2 | #define _AOS_VISION_BLOB_RANGE_IMAGE_H_ |
| 3 | |
| 4 | #include <vector> |
| 5 | |
| 6 | #include "aos/vision/image/image_types.h" |
| 7 | |
| 8 | namespace aos { |
| 9 | namespace vision { |
| 10 | |
| 11 | struct Point { |
| 12 | int x; |
| 13 | int y; |
| 14 | }; |
| 15 | |
| 16 | struct ImageRange { |
| 17 | ImageRange(int a, int b) : st(a), ed(b) {} |
| 18 | int st; |
| 19 | int ed; |
| 20 | int last() const { return ed - 1; } |
| 21 | int calc_width() const { return ed - st; } |
| 22 | |
| 23 | bool operator<(const ImageRange &o) const { return st < o.st; } |
Brian Silverman | 5eec8b9 | 2019-03-10 15:14:31 -0700 | [diff] [blame] | 24 | bool operator==(const ImageRange &other) const { |
| 25 | return st == other.st && ed == other.ed; |
| 26 | } |
| 27 | bool operator!=(const ImageRange &other) const { return !(*this == other); } |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 28 | }; |
| 29 | |
Brian Silverman | 5eec8b9 | 2019-03-10 15:14:31 -0700 | [diff] [blame] | 30 | void PrintTo(const ImageRange &range, std::ostream *os); |
| 31 | |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 32 | // Image in pre-thresholded run-length encoded format. |
| 33 | class RangeImage { |
| 34 | public: |
| 35 | RangeImage(int min_y, std::vector<std::vector<ImageRange>> &&ranges) |
| 36 | : min_y_(min_y), ranges_(std::move(ranges)) {} |
| 37 | explicit RangeImage(int l) { ranges_.reserve(l); } |
| 38 | RangeImage() {} |
| 39 | |
Brian Silverman | 5eec8b9 | 2019-03-10 15:14:31 -0700 | [diff] [blame] | 40 | bool operator==(const RangeImage &other) const { |
| 41 | if (min_y_ != other.min_y_) { return false; } |
| 42 | if (ranges_ != other.ranges_) { return false; } |
| 43 | return true; |
| 44 | } |
| 45 | bool operator!=(const RangeImage &other) const { return !(*this == other); } |
| 46 | |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 47 | int size() const { return ranges_.size(); } |
| 48 | |
Brian Silverman | 93897f3 | 2019-03-10 16:19:19 -0700 | [diff] [blame^] | 49 | // Returns the total number of included pixels. |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 50 | int npixels(); |
Brian Silverman | 93897f3 | 2019-03-10 16:19:19 -0700 | [diff] [blame^] | 51 | // Calculates the total number of included pixels. |
| 52 | // |
| 53 | // TODO(Brian): Present a nicer API than the current duality between this and |
| 54 | // npixels(), which is annoying because npixels() has to modify the cached |
| 55 | // data so it can't be const. |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 56 | int calc_area() const; |
| 57 | |
| 58 | void Flip(ImageFormat fmt) { Flip(fmt.w, fmt.h); } |
| 59 | void Flip(int image_width, int image_height); |
| 60 | |
| 61 | std::vector<std::vector<ImageRange>>::const_iterator begin() const { |
| 62 | return ranges_.begin(); |
| 63 | } |
| 64 | |
| 65 | std::vector<std::vector<ImageRange>>::const_iterator end() const { |
| 66 | return ranges_.end(); |
| 67 | } |
| 68 | |
| 69 | const std::vector<std::vector<ImageRange>> &ranges() const { return ranges_; } |
| 70 | |
| 71 | int min_y() const { return min_y_; } |
| 72 | |
| 73 | int mini() const { return min_y_; } |
| 74 | |
Parker Schuh | 0ff777c | 2017-02-19 15:01:13 -0800 | [diff] [blame] | 75 | int height() const { return min_y_ + ranges_.size(); } |
| 76 | |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 77 | private: |
| 78 | // minimum index in y where the blob starts |
| 79 | int min_y_ = 0; |
| 80 | |
Brian Silverman | 5eec8b9 | 2019-03-10 15:14:31 -0700 | [diff] [blame] | 81 | // Each vector<ImageRange> represents all the matched ranges in a given row. |
| 82 | // Each ImageRange within that row represents a run of pixels which matches. |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 83 | std::vector<std::vector<ImageRange>> ranges_; |
| 84 | |
| 85 | // Cached pixel count. |
| 86 | int npixelsc_ = -1; |
| 87 | }; |
| 88 | |
Brian Silverman | 5eec8b9 | 2019-03-10 15:14:31 -0700 | [diff] [blame] | 89 | void PrintTo(const RangeImage &range, std::ostream *os); |
| 90 | |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 91 | typedef std::vector<RangeImage> BlobList; |
| 92 | typedef std::vector<const RangeImage *> BlobLRef; |
| 93 | |
Parker Schuh | 6691f19 | 2017-01-14 17:01:02 -0800 | [diff] [blame] | 94 | // Debug print range image as ranges. |
| 95 | std::string ShortDebugPrint(const BlobList &blobl); |
| 96 | // Debug print range image as ### for the ranges. |
| 97 | void DebugPrint(const BlobList &blobl); |
| 98 | |
| 99 | } // namespace vision |
| 100 | } // namespace aos |
| 101 | |
| 102 | #endif // _AOS_VISION_BLOB_RANGE_IMAGE_H_ |