blob: 29b1640af4eedb8fa811281a12302ec14ffc4e79 [file] [log] [blame]
Parker Schuh6691f192017-01-14 17:01:02 -08001#include "aos/vision/blob/find_blob.h"
2
Stephan Pleinescc500b92024-05-30 10:58:40 -07003#include <utility>
4#include <vector>
5
Parker Schuh6691f192017-01-14 17:01:02 -08006#include "aos/vision/blob/disjoint_set.h"
7
Stephan Pleinesf63bde82024-01-13 15:59:33 -08008namespace aos::vision {
Parker Schuh6691f192017-01-14 17:01:02 -08009
10struct BlobBuilder {
11 BlobBuilder(int i) : min_y(i) {}
12 void Add(int i, ImageRange rng) {
13 while (static_cast<int>(ranges.size()) <= i - min_y) {
14 ranges.emplace_back();
15 }
16 ranges[i - min_y].push_back(rng);
17 }
18
19 void Merge(const BlobBuilder &o) {
20 // Always will be positive because of std::swap below in
21 // MergeInBlob maintains y order.
22 int diff = o.min_y - min_y;
23 for (int j = 0; j < static_cast<int>(o.ranges.size()); j++) {
24 int i = j + diff;
25 while (static_cast<int>(ranges.size()) <= i) {
26 ranges.emplace_back();
27 }
28
29 std::vector<ImageRange> cur = ranges[i];
30 std::vector<ImageRange> prev = o.ranges[j];
31
32 // Merge sort of cur and prev.
33 ranges[i].clear();
34 int a = 0;
35 int b = 0;
36 while (a < static_cast<int>(cur.size()) &&
37 b < static_cast<int>(prev.size())) {
38 if (cur[a].st < prev[b].st) {
39 ranges[i].push_back(cur[a++]);
40 } else {
41 ranges[i].push_back(prev[b++]);
42 }
43 }
44 while (a < static_cast<int>(cur.size())) {
45 ranges[i].push_back(cur[a++]);
46 }
47 while (b < static_cast<int>(prev.size())) {
48 ranges[i].push_back(prev[b++]);
49 }
50 }
51 }
52 std::vector<std::vector<ImageRange>> ranges;
53 int min_y;
54};
55
56// Uses disjoint set class to track range images.
Parker Schuh0ff777c2017-02-19 15:01:13 -080057// Joins in the disjoint set are done at the same time as joins in the
Parker Schuh6691f192017-01-14 17:01:02 -080058// range image.
59class BlobDisjointSet {
60 public:
61 int AddToBlob(int bid, int i, ImageRange img) {
62 bid = disjoint_set.Find(bid);
63 items[bid].Add(i, img);
64 return bid;
65 }
66 int AddBlob(int i, ImageRange img) {
67 int bid = disjoint_set.Add();
68 items.emplace_back(i);
69 items[bid].Add(i, img);
70 return bid;
71 }
72 void MergeInBlob(int cbid, int pbid) {
73 cbid = disjoint_set.Find(cbid);
74 pbid = disjoint_set.Find(pbid);
75 if (cbid != pbid) {
76 if (items[cbid].min_y > items[pbid].min_y) {
77 std::swap(cbid, pbid);
78 }
79 items[cbid].Merge(items[pbid]);
80 disjoint_set.ExplicitUnion(pbid, cbid);
81 }
82 }
83 BlobList MoveBlobs() {
84 std::vector<RangeImage> blobs;
85 for (int i = 0; i < static_cast<int>(items.size()); i++) {
86 if (disjoint_set.IsRoot(i)) {
87 blobs.emplace_back(items[i].min_y, std::move(items[i].ranges));
88 }
89 }
90 return blobs;
91 }
92
93 private:
94 DisjointSet disjoint_set;
95 std::vector<BlobBuilder> items;
96};
97
Brian Silvermanb27e6b72019-03-10 16:17:42 -070098BlobList FindBlobs(const RangeImage &input_image) {
Parker Schuh6691f192017-01-14 17:01:02 -080099 BlobDisjointSet blob_set;
Brian Silvermanb27e6b72019-03-10 16:17:42 -0700100 std::vector<int> previous_ids;
101 std::vector<int> current_ids;
102 for (ImageRange input_range : input_image.ranges()[0]) {
103 previous_ids.push_back(blob_set.AddBlob(0, input_range));
Parker Schuh6691f192017-01-14 17:01:02 -0800104 }
105
Brian Silvermanb27e6b72019-03-10 16:17:42 -0700106 for (int input_row = 1; input_row < input_image.size(); input_row++) {
107 // The index of previous_ranges we're currently considering.
108 int previous_location = 0;
109 // The index of current_ranges we're currently considering.
110 int current_location = 0;
111 const std::vector<ImageRange> &previous_ranges =
112 input_image.ranges()[input_row - 1];
113 const std::vector<ImageRange> &current_ranges =
114 input_image.ranges()[input_row];
115 current_ids.clear();
Parker Schuh6691f192017-01-14 17:01:02 -0800116
Brian Silvermanb27e6b72019-03-10 16:17:42 -0700117 while (previous_location < static_cast<int>(previous_ranges.size()) &&
118 current_location < static_cast<int>(current_ranges.size())) {
119 const ImageRange previous_range = previous_ranges[previous_location];
120 const ImageRange current_range = current_ranges[current_location];
121 if (current_range.last() < previous_range.st) {
122 // If current_range ends before previous_range starts, then they don't
123 // overlap, so we might want to add current_range to a separate blob.
124 if (static_cast<int>(current_ids.size()) == current_location) {
125 // We only want to add it if we haven't already added this
126 // current_range to a blob.
127 current_ids.push_back(blob_set.AddBlob(input_row, current_range));
Parker Schuh6691f192017-01-14 17:01:02 -0800128 }
Brian Silvermanb27e6b72019-03-10 16:17:42 -0700129 current_location++;
130 } else if (previous_range.last() < current_range.st) {
131 // If previous_range ends before current_range starts, then they don't
132 // overlap. Definitely nothing to merge before current_range in the
133 // current row.
134 previous_location++;
Parker Schuh6691f192017-01-14 17:01:02 -0800135 } else {
Brian Silvermanb27e6b72019-03-10 16:17:42 -0700136 if (static_cast<int>(current_ids.size()) > current_location) {
137 // If we've already added current_range, and they still overlap, then
138 // we should merge the blobs.
139 blob_set.MergeInBlob(current_ids[current_location],
140 previous_ids[previous_location]);
Parker Schuh6691f192017-01-14 17:01:02 -0800141 } else {
Brian Silvermanb27e6b72019-03-10 16:17:42 -0700142 // If we haven't yet added current_range to a blob, then do that now.
143 current_ids.push_back(
144 blob_set.AddToBlob(previous_ids[previous_location], input_row,
145 current_ranges[current_location]));
Parker Schuh6691f192017-01-14 17:01:02 -0800146 }
Brian Silvermanb27e6b72019-03-10 16:17:42 -0700147 // Increment the furthest-left-ending range in either row. The
148 // further-right-ending one might merge with the next one in the other
149 // row, so we need to look at it again next iteration.
150 if (current_range.last() < previous_range.last()) {
151 current_location++;
Parker Schuh6691f192017-01-14 17:01:02 -0800152 } else {
Brian Silvermanb27e6b72019-03-10 16:17:42 -0700153 previous_location++;
Parker Schuh6691f192017-01-14 17:01:02 -0800154 }
155 }
156 }
Brian Silvermanb27e6b72019-03-10 16:17:42 -0700157 // Finish processing the current row. This is sometimes necessary if the
158 // previous row was fully processed first.
159 //
160 // Note that we don't care if the previous row didn't get fully iterated
161 // over.
162 while (current_location < static_cast<int>(current_ranges.size())) {
163 if (static_cast<int>(current_ids.size()) == current_location) {
164 // We only want to add it if we haven't already added this range to a
165 // blob.
166 current_ids.push_back(
167 blob_set.AddBlob(input_row, current_ranges[current_location]));
Parker Schuh6691f192017-01-14 17:01:02 -0800168 }
Brian Silvermanb27e6b72019-03-10 16:17:42 -0700169 current_location++;
Parker Schuh6691f192017-01-14 17:01:02 -0800170 }
Brian Silvermanb27e6b72019-03-10 16:17:42 -0700171
172 // Update previous_ids for the next iteration.
173 std::swap(previous_ids, current_ids);
Parker Schuh6691f192017-01-14 17:01:02 -0800174 }
175 return blob_set.MoveBlobs();
176}
177
Stephan Pleinesf63bde82024-01-13 15:59:33 -0800178} // namespace aos::vision