blob: 10e801fcbcac2b4c524676a7adb4647c89e0b37d [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
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -08008namespace aos::vision {
Parker Schuh6691f192017-01-14 17:01:02 -08009
10struct Point {
11 int x;
12 int y;
13};
14
15struct ImageRange {
16 ImageRange(int a, int b) : st(a), ed(b) {}
17 int st;
18 int ed;
19 int last() const { return ed - 1; }
20 int calc_width() const { return ed - st; }
21
22 bool operator<(const ImageRange &o) const { return st < o.st; }
Brian Silverman5eec8b92019-03-10 15:14:31 -070023 bool operator==(const ImageRange &other) const {
24 return st == other.st && ed == other.ed;
25 }
26 bool operator!=(const ImageRange &other) const { return !(*this == other); }
Parker Schuh6691f192017-01-14 17:01:02 -080027};
28
Brian Silverman5eec8b92019-03-10 15:14:31 -070029void PrintTo(const ImageRange &range, std::ostream *os);
30
Parker Schuh6691f192017-01-14 17:01:02 -080031// Image in pre-thresholded run-length encoded format.
32class RangeImage {
33 public:
34 RangeImage(int min_y, std::vector<std::vector<ImageRange>> &&ranges)
35 : min_y_(min_y), ranges_(std::move(ranges)) {}
36 explicit RangeImage(int l) { ranges_.reserve(l); }
37 RangeImage() {}
38
Brian Silverman5eec8b92019-03-10 15:14:31 -070039 bool operator==(const RangeImage &other) const {
Austin Schuh60e77942022-05-16 17:48:24 -070040 if (min_y_ != other.min_y_) {
41 return false;
42 }
43 if (ranges_ != other.ranges_) {
44 return false;
45 }
Brian Silverman5eec8b92019-03-10 15:14:31 -070046 return true;
47 }
48 bool operator!=(const RangeImage &other) const { return !(*this == other); }
49
Parker Schuh6691f192017-01-14 17:01:02 -080050 int size() const { return ranges_.size(); }
51
Brian Silverman93897f32019-03-10 16:19:19 -070052 // Returns the total number of included pixels.
Parker Schuh6691f192017-01-14 17:01:02 -080053 int npixels();
Brian Silverman93897f32019-03-10 16:19:19 -070054 // Calculates the total number of included pixels.
55 //
56 // TODO(Brian): Present a nicer API than the current duality between this and
57 // npixels(), which is annoying because npixels() has to modify the cached
58 // data so it can't be const.
Parker Schuh6691f192017-01-14 17:01:02 -080059 int calc_area() const;
60
61 void Flip(ImageFormat fmt) { Flip(fmt.w, fmt.h); }
62 void Flip(int image_width, int image_height);
63
64 std::vector<std::vector<ImageRange>>::const_iterator begin() const {
65 return ranges_.begin();
66 }
67
68 std::vector<std::vector<ImageRange>>::const_iterator end() const {
69 return ranges_.end();
70 }
71
72 const std::vector<std::vector<ImageRange>> &ranges() const { return ranges_; }
73
74 int min_y() const { return min_y_; }
75
76 int mini() const { return min_y_; }
77
Parker Schuh0ff777c2017-02-19 15:01:13 -080078 int height() const { return min_y_ + ranges_.size(); }
79
Parker Schuh6691f192017-01-14 17:01:02 -080080 private:
81 // minimum index in y where the blob starts
82 int min_y_ = 0;
83
Brian Silverman5eec8b92019-03-10 15:14:31 -070084 // Each vector<ImageRange> represents all the matched ranges in a given row.
85 // Each ImageRange within that row represents a run of pixels which matches.
Parker Schuh6691f192017-01-14 17:01:02 -080086 std::vector<std::vector<ImageRange>> ranges_;
87
88 // Cached pixel count.
89 int npixelsc_ = -1;
90};
91
Brian Silverman5eec8b92019-03-10 15:14:31 -070092void PrintTo(const RangeImage &range, std::ostream *os);
93
Parker Schuh6691f192017-01-14 17:01:02 -080094typedef std::vector<RangeImage> BlobList;
95typedef std::vector<const RangeImage *> BlobLRef;
96
Parker Schuh6691f192017-01-14 17:01:02 -080097// Debug print range image as ranges.
98std::string ShortDebugPrint(const BlobList &blobl);
99// Debug print range image as ### for the ranges.
100void DebugPrint(const BlobList &blobl);
101
Stephan Pleinesd99b1ee2024-02-02 20:56:44 -0800102} // namespace aos::vision
Parker Schuh6691f192017-01-14 17:01:02 -0800103
104#endif // _AOS_VISION_BLOB_RANGE_IMAGE_H_