| 1 | #include "cvd/connected_components.h" |
| 2 | #include <algorithm> |
| 3 | #include <climits> |
| 4 | #include <iterator> |
| 5 | |
| 6 | using namespace std; |
| 7 | namespace CVD |
| 8 | { |
| 9 | static unsigned int root_of(const vector<unsigned int>& parents, unsigned int n) |
| 10 | { |
| 11 | while(n != parents[n]) |
| 12 | n = parents[n]; |
| 13 | return n; |
| 14 | } |
| 15 | |
| 16 | struct CompareFistIntLessThan |
| 17 | { |
| 18 | bool operator()(const pair<int, int>& p1, const pair<int, int>& p2) |
| 19 | { |
| 20 | return p1.first < p2.first; |
| 21 | } |
| 22 | }; |
| 23 | |
| 24 | void connected_components(const vector<ImageRef>& v, vector<vector<ImageRef>>& r) |
| 25 | { |
| 26 | int current_label = -1; |
| 27 | |
| 28 | //This stores the initial labelling |
| 29 | //of each pixel. |
| 30 | vector<vector<ImageRef>> segments; |
| 31 | |
| 32 | r.clear(); |
| 33 | if(v.size() == 0) |
| 34 | return; |
| 35 | |
| 36 | //This stores the connectivity graph, where |
| 37 | //each element stores its parent. A node storing |
| 38 | //its own parent is a root node. |
| 39 | vector<unsigned int> parents; |
| 40 | |
| 41 | //This stores the horizontal position and label of the points in the row |
| 42 | //above. |
| 43 | //There are several choices, map<int, int> can map a position to a label, |
| 44 | //as can a vector<int> (using a dense representation). An ordered vector<int, int> |
| 45 | //is equivalent to a map, but much faster, since allocation is much easier, and |
| 46 | //points are always entered in order, so insertion is constant time. |
| 47 | |
| 48 | //vector<int, int> is slightly slower than vector<int> on some images, but |
| 49 | //faster on others, especially those where objects are sparse, but rows are |
| 50 | //not. |
| 51 | vector<pair<int, int>> row_above, row_curent; |
| 52 | vector<pair<int, int>>::iterator begin; |
| 53 | int prev_row = INT_MIN, prev_x = INT_MIN; |
| 54 | |
| 55 | for(unsigned int i = 0; i < v.size(); i++) |
| 56 | { |
| 57 | ImageRef pos = v[i]; |
| 58 | |
| 59 | if(pos.y != prev_row) |
| 60 | { |
| 61 | prev_x = INT_MIN; |
| 62 | row_above.swap(row_curent); |
| 63 | row_curent.clear(); |
| 64 | |
| 65 | if(pos.y - 1 > prev_row) |
| 66 | { |
| 67 | row_above.clear(); |
| 68 | } |
| 69 | |
| 70 | //Put in a sentinal value to avoid some tests. |
| 71 | row_above.push_back(make_pair(INT_MAX, -1)); |
| 72 | |
| 73 | begin = row_above.begin(); |
| 74 | } |
| 75 | |
| 76 | //Look to see if there is a point above (4-way connectivity) |
| 77 | //8 way would look above above-left and above-right |
| 78 | int above_label = -1; |
| 79 | |
| 80 | //Check for contiguous regions first. |
| 81 | if(begin->first == pos.x) |
| 82 | { |
| 83 | above_label = begin->second; |
| 84 | begin++; |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | vector<pair<int, int>>::iterator above; |
| 89 | above = lower_bound(begin, row_above.end(), make_pair(pos.x, 0), CompareFistIntLessThan()); |
| 90 | |
| 91 | if(above->first == pos.x) |
| 92 | { |
| 93 | above_label = above->second; |
| 94 | |
| 95 | //There is no point searching from the beginning next time. We may |
| 96 | //as well search from one past the current point. |
| 97 | begin = above + 1; |
| 98 | } |
| 99 | else |
| 100 | begin = above; |
| 101 | } |
| 102 | |
| 103 | //If there is nothing above or to the left, |
| 104 | //then create a new label and corresponding segment. |
| 105 | if(above_label == -1 && prev_x != pos.x - 1) |
| 106 | { |
| 107 | parents.resize(parents.size() + 1); |
| 108 | current_label = static_cast<int>(parents.size()) - 1; |
| 109 | parents[current_label] = current_label; |
| 110 | |
| 111 | segments.resize(segments.size() + 1); |
| 112 | } |
| 113 | else if(above_label != -1 && above_label != current_label) |
| 114 | { |
| 115 | //Parent is different, so take its label. |
| 116 | //update the chain to the left if it exists. |
| 117 | if(prev_x == pos.x - 1) |
| 118 | current_label = parents[current_label] = root_of(parents, above_label); |
| 119 | else |
| 120 | current_label = root_of(parents, above_label); |
| 121 | } |
| 122 | |
| 123 | segments[current_label].push_back(pos); |
| 124 | |
| 125 | row_curent.push_back(make_pair(pos.x, current_label)); |
| 126 | prev_row = v[i].y; |
| 127 | prev_x = v[i].x; |
| 128 | } |
| 129 | |
| 130 | //Concatenate all the connected segments. |
| 131 | vector<unsigned int> components; |
| 132 | |
| 133 | for(unsigned int i = 0; i < parents.size(); i++) |
| 134 | { |
| 135 | //Flatten |
| 136 | parents[i] = root_of(parents, i); |
| 137 | |
| 138 | //Record the roots |
| 139 | if(i == parents[i]) |
| 140 | components.push_back(i); |
| 141 | else |
| 142 | { |
| 143 | //Or append points on to the root segment. |
| 144 | copy(segments[i].begin(), segments[i].end(), back_inserter<vector<ImageRef>>(segments[parents[i]])); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | r.clear(); |
| 149 | r.resize(components.size()); |
| 150 | |
| 151 | //Save all the root segments |
| 152 | for(unsigned int i = 0; i < components.size(); i++) |
| 153 | r[i].swap(segments[components[i]]); |
| 154 | } |
| 155 | } |