blob: 3e8c35c99bf7c66ea06d643dfa0cc3219b5ab344 [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
Stephan Pleinescc500b92024-05-30 10:58:40 -07004#include <iosfwd>
5#include <string>
6#include <utility>
Parker Schuh6691f192017-01-14 17:01:02 -08007#include <vector>
8
9#include "aos/vision/image/image_types.h"
10
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -080011namespace aos::vision {
Parker Schuh6691f192017-01-14 17:01:02 -080012
13struct Point {
14 int x;
15 int y;
16};
17
18struct ImageRange {
19 ImageRange(int a, int b) : st(a), ed(b) {}
20 int st;
21 int ed;
22 int last() const { return ed - 1; }
23 int calc_width() const { return ed - st; }
24
25 bool operator<(const ImageRange &o) const { return st < o.st; }
Brian Silverman5eec8b92019-03-10 15:14:31 -070026 bool operator==(const ImageRange &other) const {
27 return st == other.st && ed == other.ed;
28 }
29 bool operator!=(const ImageRange &other) const { return !(*this == other); }
Parker Schuh6691f192017-01-14 17:01:02 -080030};
31
Brian Silverman5eec8b92019-03-10 15:14:31 -070032void PrintTo(const ImageRange &range, std::ostream *os);
33
Parker Schuh6691f192017-01-14 17:01:02 -080034// Image in pre-thresholded run-length encoded format.
35class RangeImage {
36 public:
37 RangeImage(int min_y, std::vector<std::vector<ImageRange>> &&ranges)
38 : min_y_(min_y), ranges_(std::move(ranges)) {}
39 explicit RangeImage(int l) { ranges_.reserve(l); }
40 RangeImage() {}
41
Brian Silverman5eec8b92019-03-10 15:14:31 -070042 bool operator==(const RangeImage &other) const {
Austin Schuh60e77942022-05-16 17:48:24 -070043 if (min_y_ != other.min_y_) {
44 return false;
45 }
46 if (ranges_ != other.ranges_) {
47 return false;
48 }
Brian Silverman5eec8b92019-03-10 15:14:31 -070049 return true;
50 }
51 bool operator!=(const RangeImage &other) const { return !(*this == other); }
52
Parker Schuh6691f192017-01-14 17:01:02 -080053 int size() const { return ranges_.size(); }
54
Brian Silverman93897f32019-03-10 16:19:19 -070055 // Returns the total number of included pixels.
Parker Schuh6691f192017-01-14 17:01:02 -080056 int npixels();
Brian Silverman93897f32019-03-10 16:19:19 -070057 // Calculates the total number of included pixels.
58 //
59 // TODO(Brian): Present a nicer API than the current duality between this and
60 // npixels(), which is annoying because npixels() has to modify the cached
61 // data so it can't be const.
Parker Schuh6691f192017-01-14 17:01:02 -080062 int calc_area() const;
63
64 void Flip(ImageFormat fmt) { Flip(fmt.w, fmt.h); }
65 void Flip(int image_width, int image_height);
66
67 std::vector<std::vector<ImageRange>>::const_iterator begin() const {
68 return ranges_.begin();
69 }
70
71 std::vector<std::vector<ImageRange>>::const_iterator end() const {
72 return ranges_.end();
73 }
74
75 const std::vector<std::vector<ImageRange>> &ranges() const { return ranges_; }
76
77 int min_y() const { return min_y_; }
78
79 int mini() const { return min_y_; }
80
Parker Schuh0ff777c2017-02-19 15:01:13 -080081 int height() const { return min_y_ + ranges_.size(); }
82
Parker Schuh6691f192017-01-14 17:01:02 -080083 private:
84 // minimum index in y where the blob starts
85 int min_y_ = 0;
86
Brian Silverman5eec8b92019-03-10 15:14:31 -070087 // Each vector<ImageRange> represents all the matched ranges in a given row.
88 // Each ImageRange within that row represents a run of pixels which matches.
Parker Schuh6691f192017-01-14 17:01:02 -080089 std::vector<std::vector<ImageRange>> ranges_;
90
91 // Cached pixel count.
92 int npixelsc_ = -1;
93};
94
Brian Silverman5eec8b92019-03-10 15:14:31 -070095void PrintTo(const RangeImage &range, std::ostream *os);
96
Parker Schuh6691f192017-01-14 17:01:02 -080097typedef std::vector<RangeImage> BlobList;
98typedef std::vector<const RangeImage *> BlobLRef;
99
Parker Schuh6691f192017-01-14 17:01:02 -0800100// Debug print range image as ranges.
101std::string ShortDebugPrint(const BlobList &blobl);
102// Debug print range image as ### for the ranges.
103void DebugPrint(const BlobList &blobl);
104
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800105} // namespace aos::vision
Parker Schuh6691f192017-01-14 17:01:02 -0800106
107#endif // _AOS_VISION_BLOB_RANGE_IMAGE_H_