blob: a7354425f0cbaa31e701556a3d31f677fbdeb4b2 [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 {
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 Schuh6691f192017-01-14 17:01:02 -080047 int size() const { return ranges_.size(); }
48
Brian Silverman93897f32019-03-10 16:19:19 -070049 // Returns the total number of included pixels.
Parker Schuh6691f192017-01-14 17:01:02 -080050 int npixels();
Brian Silverman93897f32019-03-10 16:19:19 -070051 // 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 Schuh6691f192017-01-14 17:01:02 -080056 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 Schuh0ff777c2017-02-19 15:01:13 -080075 int height() const { return min_y_ + ranges_.size(); }
76
Parker Schuh6691f192017-01-14 17:01:02 -080077 private:
78 // minimum index in y where the blob starts
79 int min_y_ = 0;
80
Brian Silverman5eec8b92019-03-10 15:14:31 -070081 // 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 Schuh6691f192017-01-14 17:01:02 -080083 std::vector<std::vector<ImageRange>> ranges_;
84
85 // Cached pixel count.
86 int npixelsc_ = -1;
87};
88
Brian Silverman5eec8b92019-03-10 15:14:31 -070089void PrintTo(const RangeImage &range, std::ostream *os);
90
Parker Schuh6691f192017-01-14 17:01:02 -080091typedef std::vector<RangeImage> BlobList;
92typedef std::vector<const RangeImage *> BlobLRef;
93
Parker Schuh6691f192017-01-14 17:01:02 -080094// Debug print range image as ranges.
95std::string ShortDebugPrint(const BlobList &blobl);
96// Debug print range image as ### for the ranges.
97void DebugPrint(const BlobList &blobl);
98
99} // namespace vision
100} // namespace aos
101
102#endif // _AOS_VISION_BLOB_RANGE_IMAGE_H_