blob: d962788ad0cfe10bf13855b43a3a56fe0d74d29c [file] [log] [blame]
Parker Schuh6691f192017-01-14 17:01:02 -08001#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
8namespace aos {
9namespace vision {
10
11struct Point {
12 int x;
13 int y;
14};
15
16struct 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 Silverman5eec8b92019-03-10 15:14:31 -070024 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 Schuh6691f192017-01-14 17:01:02 -080028};
29
Brian Silverman5eec8b92019-03-10 15:14:31 -070030void PrintTo(const ImageRange &range, std::ostream *os);
31
Parker Schuh6691f192017-01-14 17:01:02 -080032// Image in pre-thresholded run-length encoded format.
33class 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 Silverman5eec8b92019-03-10 15:14:31 -070040 bool operator==(const RangeImage &other) const {
Austin Schuh60e77942022-05-16 17:48:24 -070041 if (min_y_ != other.min_y_) {
42 return false;
43 }
44 if (ranges_ != other.ranges_) {
45 return false;
46 }
Brian Silverman5eec8b92019-03-10 15:14:31 -070047 return true;
48 }
49 bool operator!=(const RangeImage &other) const { return !(*this == other); }
50
Parker Schuh6691f192017-01-14 17:01:02 -080051 int size() const { return ranges_.size(); }
52
Brian Silverman93897f32019-03-10 16:19:19 -070053 // Returns the total number of included pixels.
Parker Schuh6691f192017-01-14 17:01:02 -080054 int npixels();
Brian Silverman93897f32019-03-10 16:19:19 -070055 // Calculates the total number of included pixels.
56 //
57 // TODO(Brian): Present a nicer API than the current duality between this and
58 // npixels(), which is annoying because npixels() has to modify the cached
59 // data so it can't be const.
Parker Schuh6691f192017-01-14 17:01:02 -080060 int calc_area() const;
61
62 void Flip(ImageFormat fmt) { Flip(fmt.w, fmt.h); }
63 void Flip(int image_width, int image_height);
64
65 std::vector<std::vector<ImageRange>>::const_iterator begin() const {
66 return ranges_.begin();
67 }
68
69 std::vector<std::vector<ImageRange>>::const_iterator end() const {
70 return ranges_.end();
71 }
72
73 const std::vector<std::vector<ImageRange>> &ranges() const { return ranges_; }
74
75 int min_y() const { return min_y_; }
76
77 int mini() const { return min_y_; }
78
Parker Schuh0ff777c2017-02-19 15:01:13 -080079 int height() const { return min_y_ + ranges_.size(); }
80
Parker Schuh6691f192017-01-14 17:01:02 -080081 private:
82 // minimum index in y where the blob starts
83 int min_y_ = 0;
84
Brian Silverman5eec8b92019-03-10 15:14:31 -070085 // Each vector<ImageRange> represents all the matched ranges in a given row.
86 // Each ImageRange within that row represents a run of pixels which matches.
Parker Schuh6691f192017-01-14 17:01:02 -080087 std::vector<std::vector<ImageRange>> ranges_;
88
89 // Cached pixel count.
90 int npixelsc_ = -1;
91};
92
Brian Silverman5eec8b92019-03-10 15:14:31 -070093void PrintTo(const RangeImage &range, std::ostream *os);
94
Parker Schuh6691f192017-01-14 17:01:02 -080095typedef std::vector<RangeImage> BlobList;
96typedef std::vector<const RangeImage *> BlobLRef;
97
Parker Schuh6691f192017-01-14 17:01:02 -080098// Debug print range image as ranges.
99std::string ShortDebugPrint(const BlobList &blobl);
100// Debug print range image as ### for the ranges.
101void DebugPrint(const BlobList &blobl);
102
103} // namespace vision
104} // namespace aos
105
106#endif // _AOS_VISION_BLOB_RANGE_IMAGE_H_