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