41 |
41 |
//M*/
|
42 |
42 |
//
|
43 |
43 |
#include "precomp.hpp"
|
|
44 |
#include <vector>
|
44 |
45 |
|
45 |
46 |
namespace cv{
|
46 |
47 |
namespace connectedcomponents{
|
47 |
|
using std::vector;
|
|
48 |
|
|
49 |
template<typename LabelT>
|
|
50 |
struct NoOp{
|
|
51 |
NoOp(){
|
|
52 |
}
|
|
53 |
void init(const LabelT labels){
|
|
54 |
(void) labels;
|
|
55 |
}
|
|
56 |
inline
|
|
57 |
void operator()(int r, int c, LabelT l){
|
|
58 |
(void) r;
|
|
59 |
(void) c;
|
|
60 |
(void) l;
|
|
61 |
}
|
|
62 |
void finish(){}
|
|
63 |
};
|
|
64 |
template<typename LabelT>
|
|
65 |
struct CCStatsOp{
|
|
66 |
std::vector<cv::ConnectedComponentStats> &statsv;
|
|
67 |
CCStatsOp(std::vector<cv::ConnectedComponentStats> &_statsv): statsv(_statsv){
|
|
68 |
}
|
|
69 |
inline
|
|
70 |
void init(const LabelT nlabels){
|
|
71 |
statsv.clear();
|
|
72 |
cv::ConnectedComponentStats stats = cv::ConnectedComponentStats();
|
|
73 |
stats.lower_x = std::numeric_limits<LabelT>::max();
|
|
74 |
stats.lower_y = std::numeric_limits<LabelT>::max();
|
|
75 |
stats.upper_x = std::numeric_limits<LabelT>::min();
|
|
76 |
stats.upper_y = std::numeric_limits<LabelT>::min();
|
|
77 |
stats.centroid_x = 0;
|
|
78 |
stats.centroid_y = 0;
|
|
79 |
stats.integral_x = 0;
|
|
80 |
stats.integral_y = 0;
|
|
81 |
stats.area = 0;
|
|
82 |
statsv.resize(nlabels, stats);
|
|
83 |
}
|
|
84 |
void operator()(int r, int c, LabelT l){
|
|
85 |
ConnectedComponentStats &stats = statsv[l];
|
|
86 |
if(c > stats.upper_x){
|
|
87 |
stats.upper_x = c;
|
|
88 |
}else{
|
|
89 |
if(c < stats.lower_x){
|
|
90 |
stats.lower_x = c;
|
|
91 |
}
|
|
92 |
}
|
|
93 |
if(r > stats.upper_y){
|
|
94 |
stats.upper_y = r;
|
|
95 |
}else{
|
|
96 |
if(r < stats.lower_y){
|
|
97 |
stats.lower_y = r;
|
|
98 |
}
|
|
99 |
}
|
|
100 |
stats.integral_x += c;
|
|
101 |
stats.integral_y += r;
|
|
102 |
stats.area++;
|
|
103 |
}
|
|
104 |
void finish(){
|
|
105 |
for(size_t l = 0; l < statsv.size(); ++l){
|
|
106 |
ConnectedComponentStats &stats = statsv[l];
|
|
107 |
stats.lower_x = std::min(stats.lower_x, stats.upper_x);
|
|
108 |
stats.lower_y = std::min(stats.lower_y, stats.upper_y);
|
|
109 |
stats.centroid_x = stats.integral_x / double(stats.area);
|
|
110 |
stats.centroid_y = stats.integral_y / double(stats.area);
|
|
111 |
}
|
|
112 |
}
|
|
113 |
};
|
48 |
114 |
|
49 |
115 |
//Find the root of the tree of node i
|
50 |
116 |
template<typename LabelT>
|
51 |
117 |
inline static
|
52 |
|
LabelT findRoot(const vector<LabelT> &P, LabelT i){
|
|
118 |
LabelT findRoot(const LabelT *P, LabelT i){
|
53 |
119 |
LabelT root = i;
|
54 |
120 |
while(P[root] < root){
|
55 |
121 |
root = P[root];
|
... | ... | |
60 |
126 |
//Make all nodes in the path of node i point to root
|
61 |
127 |
template<typename LabelT>
|
62 |
128 |
inline static
|
63 |
|
void setRoot(vector<LabelT> &P, LabelT i, LabelT root){
|
|
129 |
void setRoot(LabelT *P, LabelT i, LabelT root){
|
64 |
130 |
while(P[i] < i){
|
65 |
131 |
LabelT j = P[i];
|
66 |
132 |
P[i] = root;
|
... | ... | |
72 |
138 |
//Find the root of the tree of the node i and compress the path in the process
|
73 |
139 |
template<typename LabelT>
|
74 |
140 |
inline static
|
75 |
|
LabelT find(vector<LabelT> &P, LabelT i){
|
|
141 |
LabelT find(LabelT *P, LabelT i){
|
76 |
142 |
LabelT root = findRoot(P, i);
|
77 |
143 |
setRoot(P, i, root);
|
78 |
144 |
return root;
|
... | ... | |
81 |
147 |
//unite the two trees containing nodes i and j and return the new root
|
82 |
148 |
template<typename LabelT>
|
83 |
149 |
inline static
|
84 |
|
LabelT set_union(vector<LabelT> &P, LabelT i, LabelT j){
|
|
150 |
LabelT set_union(LabelT *P, LabelT i, LabelT j){
|
85 |
151 |
LabelT root = findRoot(P, i);
|
86 |
152 |
if(i != j){
|
87 |
153 |
LabelT rootj = findRoot(P, j);
|
... | ... | |
97 |
163 |
//Flatten the Union Find tree and relabel the components
|
98 |
164 |
template<typename LabelT>
|
99 |
165 |
inline static
|
100 |
|
LabelT flattenL(vector<LabelT> &P){
|
|
166 |
LabelT flattenL(LabelT *P, LabelT length){
|
101 |
167 |
LabelT k = 1;
|
102 |
|
for(size_t i = 1; i < P.size(); ++i){
|
|
168 |
for(LabelT i = 1; i < length; ++i){
|
103 |
169 |
if(P[i] < i){
|
104 |
170 |
P[i] = P[P[i]];
|
105 |
171 |
}else{
|
... | ... | |
109 |
175 |
return k;
|
110 |
176 |
}
|
111 |
177 |
|
112 |
|
////Flatten the Union Find tree - inconsistent labels
|
113 |
|
//void flatten(int P[], int size){
|
114 |
|
// for(int i = 1; i < size; ++i){
|
115 |
|
// P[i] = P[P[i]];
|
116 |
|
// }
|
117 |
|
//}
|
118 |
|
const int G4[2][2] = {{-1, 0}, {0, -1}};//b, d neighborhoods
|
119 |
|
const int G8[4][2] = {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}};//a, b, c, d neighborhoods
|
120 |
178 |
//Based on "Two Strategies to Speed up Connected Components Algorithms", the SAUF (Scan array union find) variant
|
121 |
179 |
//using decision trees
|
122 |
180 |
//Kesheng Wu, et al
|
123 |
|
template<typename LabelT, typename PixelT, int connectivity = 8>
|
|
181 |
//Note: rows are encoded as position in the "rows" array to save lookup times
|
|
182 |
//reference for 4-way: {{-1, 0}, {0, -1}};//b, d neighborhoods
|
|
183 |
const int G4[2][2] = {{1, 0}, {0, -1}};//b, d neighborhoods
|
|
184 |
//reference for 8-way: {{-1, -1}, {-1, 0}, {-1, 1}, {0, -1}};//a, b, c, d neighborhoods
|
|
185 |
const int G8[4][2] = {{1, -1}, {1, 0}, {1, 1}, {0, -1}};//a, b, c, d neighborhoods
|
|
186 |
template<typename LabelT, typename PixelT, typename StatsOp = NoOp<LabelT>, int connectivity = 8>
|
124 |
187 |
struct LabelingImpl{
|
125 |
|
LabelT operator()(Mat &L, const Mat &I){
|
|
188 |
LabelT operator()(Mat &L, const Mat &I, StatsOp &sop){
|
126 |
189 |
const int rows = L.rows;
|
127 |
190 |
const int cols = L.cols;
|
128 |
|
size_t nPixels = size_t(rows) * cols;
|
129 |
|
vector<LabelT> P; P.push_back(0);
|
130 |
|
LabelT l = 1;
|
|
191 |
size_t Plength = (size_t(rows + 3 - 1)/3) * (size_t(cols + 3 - 1)/3);
|
|
192 |
if(connectivity == 4){
|
|
193 |
Plength = 4 * Plength;//a quick and dirty upper bound, an exact answer exists if you want to find it
|
|
194 |
//the 4 comes from the fact that a 3x3 block can never have more than 4 unique labels
|
|
195 |
}
|
|
196 |
LabelT *P = (LabelT *) fastMalloc(sizeof(LabelT) * Plength);
|
|
197 |
P[0] = 0;
|
|
198 |
LabelT lunique = 1;
|
131 |
199 |
//scanning phase
|
132 |
200 |
for(int r_i = 0; r_i < rows; ++r_i){
|
133 |
|
for(int c_i = 0; c_i < cols; ++c_i){
|
134 |
|
if(!I.at<PixelT>(r_i, c_i)){
|
135 |
|
L.at<LabelT>(r_i, c_i) = 0;
|
136 |
|
continue;
|
137 |
|
}
|
138 |
|
if(connectivity == 8){
|
139 |
|
const int a = 0;
|
140 |
|
const int b = 1;
|
141 |
|
const int c = 2;
|
142 |
|
const int d = 3;
|
143 |
|
|
144 |
|
bool T[4];
|
145 |
|
|
146 |
|
for(size_t i = 0; i < 4; ++i){
|
147 |
|
int gr = r_i + G8[i][0];
|
148 |
|
int gc = c_i + G8[i][1];
|
149 |
|
T[i] = false;
|
150 |
|
if(gr >= 0 && gr < rows && gc >= 0 && gc < cols){
|
151 |
|
if(I.at<PixelT>(gr, gc)){
|
152 |
|
T[i] = true;
|
153 |
|
}
|
154 |
|
}
|
|
201 |
LabelT *Lrow = (LabelT *)(L.data + L.step.p[0] * r_i);
|
|
202 |
LabelT *Lrow_prev = (LabelT *)(((char *)Lrow) - L.step.p[0]);
|
|
203 |
const PixelT *Irow = (PixelT *)(I.data + I.step.p[0] * r_i);
|
|
204 |
const PixelT *Irow_prev = (const PixelT *)(((char *)Irow) - I.step.p[0]);
|
|
205 |
LabelT *Lrows[2] = {
|
|
206 |
Lrow,
|
|
207 |
Lrow_prev
|
|
208 |
};
|
|
209 |
const PixelT *Irows[2] = {
|
|
210 |
Irow,
|
|
211 |
Irow_prev
|
|
212 |
};
|
|
213 |
if(connectivity == 8){
|
|
214 |
const int a = 0;
|
|
215 |
const int b = 1;
|
|
216 |
const int c = 2;
|
|
217 |
const int d = 3;
|
|
218 |
const bool T_a_r = (r_i - G8[a][0]) >= 0;
|
|
219 |
const bool T_b_r = (r_i - G8[b][0]) >= 0;
|
|
220 |
const bool T_c_r = (r_i - G8[c][0]) >= 0;
|
|
221 |
for(int c_i = 0; Irows[0] != Irow + cols; ++Irows[0], c_i++){
|
|
222 |
if(!*Irows[0]){
|
|
223 |
Lrow[c_i] = 0;
|
|
224 |
continue;
|
155 |
225 |
}
|
|
226 |
Irows[1] = Irow_prev + c_i;
|
|
227 |
Lrows[0] = Lrow + c_i;
|
|
228 |
Lrows[1] = Lrow_prev + c_i;
|
|
229 |
const bool T_a = T_a_r && (c_i + G8[a][1]) >= 0 && *(Irows[G8[a][0]] + G8[a][1]);
|
|
230 |
const bool T_b = T_b_r && *(Irows[G8[b][0]] + G8[b][1]);
|
|
231 |
const bool T_c = T_c_r && (c_i + G8[c][1]) < cols && *(Irows[G8[c][0]] + G8[c][1]);
|
|
232 |
const bool T_d = (c_i + G8[d][1]) >= 0 && *(Irows[G8[d][0]] + G8[d][1]);
|
156 |
233 |
|
157 |
234 |
//decision tree
|
158 |
|
if(T[b]){
|
|
235 |
if(T_b){
|
159 |
236 |
//copy(b)
|
160 |
|
L.at<LabelT>(r_i, c_i) = L.at<LabelT>(r_i + G8[b][0], c_i + G8[b][1]);
|
|
237 |
*Lrows[0] = *(Lrows[G8[b][0]] + G8[b][1]);
|
161 |
238 |
}else{//not b
|
162 |
|
if(T[c]){
|
163 |
|
if(T[a]){
|
|
239 |
if(T_c){
|
|
240 |
if(T_a){
|
164 |
241 |
//copy(c, a)
|
165 |
|
L.at<LabelT>(r_i, c_i) = set_union(P, L.at<LabelT>(r_i + G8[c][0], c_i + G8[c][1]), L.at<LabelT>(r_i + G8[a][0], c_i + G8[a][1]));
|
|
242 |
*Lrows[0] = set_union(P, *(Lrows[G8[c][0]] + G8[c][1]), *(Lrows[G8[a][0]] + G8[a][1]));
|
166 |
243 |
}else{
|
167 |
|
if(T[d]){
|
|
244 |
if(T_d){
|
168 |
245 |
//copy(c, d)
|
169 |
|
L.at<LabelT>(r_i, c_i) = set_union(P, L.at<LabelT>(r_i + G8[c][0], c_i + G8[c][1]), L.at<LabelT>(r_i + G8[d][0], c_i + G8[d][1]));
|
|
246 |
*Lrows[0] = set_union(P, *(Lrows[G8[c][0]] + G8[c][1]), *(Lrows[G8[d][0]] + G8[d][1]));
|
170 |
247 |
}else{
|
171 |
248 |
//copy(c)
|
172 |
|
L.at<LabelT>(r_i, c_i) = L.at<LabelT>(r_i + G8[c][0], c_i + G8[c][1]);
|
|
249 |
*Lrows[0] = *(Lrows[G8[c][0]] + G8[c][1]);
|
173 |
250 |
}
|
174 |
251 |
}
|
175 |
252 |
}else{//not c
|
176 |
|
if(T[a]){
|
|
253 |
if(T_a){
|
177 |
254 |
//copy(a)
|
178 |
|
L.at<LabelT>(r_i, c_i) = L.at<LabelT>(r_i + G8[a][0], c_i + G8[a][1]);
|
|
255 |
*Lrows[0] = *(Lrows[G8[a][0]] + G8[a][1]);
|
179 |
256 |
}else{
|
180 |
|
if(T[d]){
|
|
257 |
if(T_d){
|
181 |
258 |
//copy(d)
|
182 |
|
L.at<LabelT>(r_i, c_i) = L.at<LabelT>(r_i + G8[d][0], c_i + G8[d][1]);
|
|
259 |
*Lrows[0] = *(Lrows[G8[d][0]] + G8[d][1]);
|
183 |
260 |
}else{
|
184 |
261 |
//new label
|
185 |
|
L.at<LabelT>(r_i, c_i) = l;
|
186 |
|
P.push_back(l);//P[l] = l;
|
187 |
|
l = l + 1;
|
|
262 |
*Lrows[0] = lunique;
|
|
263 |
P[lunique] = lunique;
|
|
264 |
lunique = lunique + 1;
|
188 |
265 |
}
|
189 |
266 |
}
|
190 |
267 |
}
|
191 |
268 |
}
|
192 |
|
}else{
|
193 |
|
//B & D only
|
194 |
|
const int b = 0;
|
195 |
|
const int d = 1;
|
196 |
|
assert(connectivity == 4);
|
197 |
|
bool T[2];
|
198 |
|
for(size_t i = 0; i < 2; ++i){
|
199 |
|
int gr = r_i + G4[i][0];
|
200 |
|
int gc = c_i + G4[i][1];
|
201 |
|
T[i] = false;
|
202 |
|
if(gr >= 0 && gr < rows && gc >= 0 && gc < cols){
|
203 |
|
if(I.at<PixelT>(gr, gc)){
|
204 |
|
T[i] = true;
|
205 |
|
}
|
206 |
|
}
|
|
269 |
}
|
|
270 |
}else{
|
|
271 |
//B & D only
|
|
272 |
assert(connectivity == 4);
|
|
273 |
const int b = 0;
|
|
274 |
const int d = 1;
|
|
275 |
const bool T_b_r = (r_i - G4[b][0]) >= 0;
|
|
276 |
for(int c_i = 0; Irows[0] != Irow + cols; ++Irows[0], c_i++){
|
|
277 |
if(!*Irows[0]){
|
|
278 |
Lrow[c_i] = 0;
|
|
279 |
continue;
|
207 |
280 |
}
|
208 |
|
|
209 |
|
if(T[b]){
|
210 |
|
if(T[d]){
|
|
281 |
Irows[1] = Irow_prev + c_i;
|
|
282 |
Lrows[0] = Lrow + c_i;
|
|
283 |
Lrows[1] = Lrow_prev + c_i;
|
|
284 |
const bool T_b = T_b_r && *(Irows[G4[b][0]] + G4[b][1]);
|
|
285 |
const bool T_d = (c_i + G4[d][1]) >= 0 && *(Irows[G4[d][0]] + G4[d][1]);
|
|
286 |
if(T_b){
|
|
287 |
if(T_d){
|
211 |
288 |
//copy(d, b)
|
212 |
|
L.at<LabelT>(r_i, c_i) = set_union(P, L.at<LabelT>(r_i + G4[d][0], c_i + G4[d][1]), L.at<LabelT>(r_i + G4[b][0], c_i + G4[b][1]));
|
|
289 |
*Lrows[0] = set_union(P, *(Lrows[G4[d][0]] + G4[d][1]), *(Lrows[G4[b][0]] + G4[b][1]));
|
213 |
290 |
}else{
|
214 |
291 |
//copy(b)
|
215 |
|
L.at<LabelT>(r_i, c_i) = L.at<LabelT>(r_i + G4[b][0], c_i + G4[b][1]);
|
|
292 |
*Lrows[0] = *(Lrows[G4[b][0]] + G4[b][1]);
|
216 |
293 |
}
|
217 |
294 |
}else{
|
218 |
|
if(T[d]){
|
|
295 |
if(T_d){
|
219 |
296 |
//copy(d)
|
220 |
|
L.at<LabelT>(r_i, c_i) = L.at<LabelT>(r_i + G4[d][0], c_i + G4[d][1]);
|
|
297 |
*Lrows[0] = *(Lrows[G4[d][0]] + G4[d][1]);
|
221 |
298 |
}else{
|
222 |
299 |
//new label
|
223 |
|
L.at<LabelT>(r_i, c_i) = l;
|
224 |
|
P.push_back(l);//P[l] = l;
|
225 |
|
l = l + 1;
|
|
300 |
*Lrows[0] = lunique;
|
|
301 |
P[lunique] = lunique;
|
|
302 |
lunique = lunique + 1;
|
226 |
303 |
}
|
227 |
304 |
}
|
228 |
|
|
229 |
305 |
}
|
230 |
306 |
}
|
231 |
307 |
}
|
232 |
308 |
|
233 |
309 |
//analysis
|
234 |
|
LabelT nLabels = flattenL(P);
|
|
310 |
LabelT nLabels = flattenL(P, lunique);
|
|
311 |
sop.init(nLabels);
|
235 |
312 |
|
236 |
|
//assign final labels
|
237 |
|
for(size_t r = 0; r < rows; ++r){
|
238 |
|
for(size_t c = 0; c < cols; ++c){
|
239 |
|
L.at<LabelT>(r, c) = P[L.at<LabelT>(r, c)];
|
|
313 |
for(int r_i = 0; r_i < rows; ++r_i){
|
|
314 |
LabelT *Lrow_start = (LabelT *)(L.data + L.step.p[0] * r_i);
|
|
315 |
LabelT *Lrow_end = Lrow_start + cols;
|
|
316 |
LabelT *Lrow = Lrow_start;
|
|
317 |
for(int c_i = 0; Lrow != Lrow_end; ++Lrow, ++c_i){
|
|
318 |
const LabelT l = P[*Lrow];
|
|
319 |
*Lrow = l;
|
|
320 |
sop(r_i, c_i, l);
|
240 |
321 |
}
|
241 |
322 |
}
|
242 |
323 |
|
|
324 |
sop.finish();
|
|
325 |
fastFree(P);
|
|
326 |
|
243 |
327 |
return nLabels;
|
244 |
328 |
}//End function LabelingImpl operator()
|
245 |
329 |
|
... | ... | |
247 |
331 |
}//end namespace connectedcomponents
|
248 |
332 |
|
249 |
333 |
//L's type must have an appropriate depth for the number of pixels in I
|
250 |
|
uint64_t connectedComponents(Mat &L, const Mat &I, int connectivity){
|
|
334 |
template<typename StatsOp>
|
|
335 |
uint64_t connectedComponents_sub1(Mat &L, const Mat &I, int connectivity, StatsOp &sop){
|
251 |
336 |
CV_Assert(L.rows == I.rows);
|
252 |
337 |
CV_Assert(L.cols == I.cols);
|
253 |
338 |
CV_Assert(L.channels() == 1 && I.channels() == 1);
|
... | ... | |
261 |
346 |
if(lDepth == CV_8U){
|
262 |
347 |
if(iDepth == CV_8U || iDepth == CV_8S){
|
263 |
348 |
if(connectivity == 4){
|
264 |
|
return (uint64_t) LabelingImpl<uint8_t, uint8_t, 4>()(L, I);
|
|
349 |
return (uint64_t) LabelingImpl<uint8_t, uint8_t, StatsOp, 4>()(L, I, sop);
|
265 |
350 |
}else{
|
266 |
|
return (uint64_t) LabelingImpl<uint8_t, uint8_t, 8>()(L, I);
|
|
351 |
return (uint64_t) LabelingImpl<uint8_t, uint8_t, StatsOp, 8>()(L, I, sop);
|
267 |
352 |
}
|
268 |
353 |
}else if(iDepth == CV_16U || iDepth == CV_16S){
|
269 |
354 |
if(connectivity == 4){
|
270 |
|
return (uint64_t) LabelingImpl<uint8_t, uint16_t, 4>()(L, I);
|
|
355 |
return (uint64_t) LabelingImpl<uint8_t, uint16_t, StatsOp, 4>()(L, I, sop);
|
271 |
356 |
}else{
|
272 |
|
return (uint64_t) LabelingImpl<uint8_t, uint16_t, 8>()(L, I);
|
|
357 |
return (uint64_t) LabelingImpl<uint8_t, uint16_t, StatsOp, 8>()(L, I, sop);
|
273 |
358 |
}
|
274 |
359 |
}else if(iDepth == CV_32S){
|
275 |
360 |
if(connectivity == 4){
|
276 |
|
return (uint64_t) LabelingImpl<uint8_t, int32_t, 4>()(L, I);
|
|
361 |
return (uint64_t) LabelingImpl<uint8_t, int32_t, StatsOp, 4>()(L, I, sop);
|
277 |
362 |
}else{
|
278 |
|
return (uint64_t) LabelingImpl<uint8_t, int32_t, 8>()(L, I);
|
|
363 |
return (uint64_t) LabelingImpl<uint8_t, int32_t, StatsOp, 8>()(L, I, sop);
|
279 |
364 |
}
|
280 |
365 |
}else if(iDepth == CV_32F){
|
281 |
366 |
if(connectivity == 4){
|
282 |
|
return (uint64_t) LabelingImpl<uint8_t, float, 4>()(L, I);
|
|
367 |
return (uint64_t) LabelingImpl<uint8_t, float, StatsOp, 4>()(L, I, sop);
|
283 |
368 |
}else{
|
284 |
|
return (uint64_t) LabelingImpl<uint8_t, float, 8>()(L, I);
|
|
369 |
return (uint64_t) LabelingImpl<uint8_t, float, StatsOp, 8>()(L, I, sop);
|
285 |
370 |
}
|
286 |
371 |
}else if(iDepth == CV_64F){
|
287 |
372 |
if(connectivity == 4){
|
288 |
|
return (uint64_t) LabelingImpl<uint8_t, double, 4>()(L, I);
|
|
373 |
return (uint64_t) LabelingImpl<uint8_t, double, StatsOp, 4>()(L, I, sop);
|
289 |
374 |
}else{
|
290 |
|
return (uint64_t) LabelingImpl<uint8_t, double, 8>()(L, I);
|
|
375 |
return (uint64_t) LabelingImpl<uint8_t, double, StatsOp, 8>()(L, I, sop);
|
291 |
376 |
}
|
292 |
377 |
}
|
293 |
378 |
}else if(lDepth == CV_16U){
|
294 |
379 |
if(iDepth == CV_8U || iDepth == CV_8S){
|
295 |
380 |
if(connectivity == 4){
|
296 |
|
return (uint64_t) LabelingImpl<uint16_t, uint8_t, 4>()(L, I);
|
|
381 |
return (uint64_t) LabelingImpl<uint16_t, uint8_t, StatsOp, 4>()(L, I, sop);
|
297 |
382 |
}else{
|
298 |
|
return (uint64_t) LabelingImpl<uint16_t, uint8_t, 8>()(L, I);
|
|
383 |
return (uint64_t) LabelingImpl<uint16_t, uint8_t, StatsOp, 8>()(L, I, sop);
|
299 |
384 |
}
|
300 |
385 |
}else if(iDepth == CV_16U || iDepth == CV_16S){
|
301 |
386 |
if(connectivity == 4){
|
302 |
|
return (uint64_t) LabelingImpl<uint16_t, uint16_t, 4>()(L, I);
|
|
387 |
return (uint64_t) LabelingImpl<uint16_t, uint16_t, StatsOp, 4>()(L, I, sop);
|
303 |
388 |
}else{
|
304 |
|
return (uint64_t) LabelingImpl<uint16_t, uint16_t, 8>()(L, I);
|
|
389 |
return (uint64_t) LabelingImpl<uint16_t, uint16_t, StatsOp, 8>()(L, I, sop);
|
305 |
390 |
}
|
306 |
391 |
}else if(iDepth == CV_32S){
|
307 |
392 |
if(connectivity == 4){
|
308 |
|
return (uint64_t) LabelingImpl<uint16_t, int32_t, 4>()(L, I);
|
|
393 |
return (uint64_t) LabelingImpl<uint16_t, int32_t, StatsOp, 4>()(L, I, sop);
|
309 |
394 |
}else{
|
310 |
|
return (uint64_t) LabelingImpl<uint16_t, int32_t, 8>()(L, I);
|
|
395 |
return (uint64_t) LabelingImpl<uint16_t, int32_t, StatsOp, 8>()(L, I, sop);
|
311 |
396 |
}
|
312 |
397 |
}else if(iDepth == CV_32F){
|
313 |
398 |
if(connectivity == 4){
|
314 |
|
return (uint64_t) LabelingImpl<uint16_t, float, 4>()(L, I);
|
|
399 |
return (uint64_t) LabelingImpl<uint16_t, float, StatsOp, 4>()(L, I, sop);
|
315 |
400 |
}else{
|
316 |
|
return (uint64_t) LabelingImpl<uint16_t, float, 8>()(L, I);
|
|
401 |
return (uint64_t) LabelingImpl<uint16_t, float, StatsOp, 8>()(L, I, sop);
|
317 |
402 |
}
|
318 |
403 |
}else if(iDepth == CV_64F){
|
319 |
404 |
if(connectivity == 4){
|
320 |
|
return (uint64_t) LabelingImpl<uint16_t, double, 4>()(L, I);
|
|
405 |
return (uint64_t) LabelingImpl<uint16_t, double, StatsOp, 4>()(L, I, sop);
|
321 |
406 |
}else{
|
322 |
|
return (uint64_t) LabelingImpl<uint16_t, double, 8>()(L, I);
|
|
407 |
return (uint64_t) LabelingImpl<uint16_t, double, StatsOp, 8>()(L, I, sop);
|
323 |
408 |
}
|
324 |
409 |
}
|
325 |
410 |
}else if(lDepth == CV_32S){
|
|
411 |
//note that signed types don't really make sense here and not being able to use uint32_t matters for scientific projects
|
|
412 |
//OpenCV: how should we proceed? .at<T> typechecks in debug mode
|
326 |
413 |
if(iDepth == CV_8U || iDepth == CV_8S){
|
327 |
414 |
if(connectivity == 4){
|
328 |
|
return (uint64_t) LabelingImpl<int32_t, uint8_t, 4>()(L, I);
|
|
415 |
return (uint64_t) LabelingImpl<int32_t, uint8_t, StatsOp, 4>()(L, I, sop);
|
329 |
416 |
}else{
|
330 |
|
return (uint64_t) LabelingImpl<int32_t, uint8_t, 8>()(L, I);
|
|
417 |
return (uint64_t) LabelingImpl<int32_t, uint8_t, StatsOp, 8>()(L, I, sop);
|
331 |
418 |
}
|
332 |
419 |
}else if(iDepth == CV_16U || iDepth == CV_16S){
|
333 |
420 |
if(connectivity == 4){
|
334 |
|
return (uint64_t) LabelingImpl<int32_t, uint16_t, 4>()(L, I);
|
|
421 |
return (uint64_t) LabelingImpl<int32_t, uint16_t, StatsOp, 4>()(L, I, sop);
|
335 |
422 |
}else{
|
336 |
|
return (uint64_t) LabelingImpl<int32_t, uint16_t, 8>()(L, I);
|
|
423 |
return (uint64_t) LabelingImpl<int32_t, uint16_t, StatsOp, 8>()(L, I, sop);
|
337 |
424 |
}
|
338 |
425 |
}else if(iDepth == CV_32S){
|
339 |
426 |
if(connectivity == 4){
|
340 |
|
return (uint64_t) LabelingImpl<int32_t, int32_t, 4>()(L, I);
|
|
427 |
return (uint64_t) LabelingImpl<int32_t, int32_t, StatsOp, 4>()(L, I, sop);
|
341 |
428 |
}else{
|
342 |
|
return (uint64_t) LabelingImpl<int32_t, int32_t, 8>()(L, I);
|
|
429 |
return (uint64_t) LabelingImpl<int32_t, int32_t, StatsOp, 8>()(L, I, sop);
|
343 |
430 |
}
|
344 |
431 |
}else if(iDepth == CV_32F){
|
345 |
432 |
if(connectivity == 4){
|
346 |
|
return (uint64_t) LabelingImpl<int32_t, float, 4>()(L, I);
|
|
433 |
return (uint64_t) LabelingImpl<int32_t, float, StatsOp, 4>()(L, I, sop);
|
347 |
434 |
}else{
|
348 |
|
return (uint64_t) LabelingImpl<int32_t, float, 8>()(L, I);
|
|
435 |
return (uint64_t) LabelingImpl<int32_t, float, StatsOp, 8>()(L, I, sop);
|
349 |
436 |
}
|
350 |
437 |
}else if(iDepth == CV_64F){
|
351 |
438 |
if(connectivity == 4){
|
352 |
|
return (uint64_t) LabelingImpl<int32_t, double, 4>()(L, I);
|
|
439 |
return (uint64_t) LabelingImpl<int32_t, double, StatsOp, 4>()(L, I, sop);
|
353 |
440 |
}else{
|
354 |
|
return (uint64_t) LabelingImpl<int32_t, double, 8>()(L, I);
|
|
441 |
return (uint64_t) LabelingImpl<int32_t, double, StatsOp, 8>()(L, I, sop);
|
355 |
442 |
}
|
|
443 |
}else{
|
|
444 |
CV_Assert(false);
|
356 |
445 |
}
|
357 |
446 |
}
|
358 |
447 |
|
... | ... | |
360 |
449 |
return -1;
|
361 |
450 |
}
|
362 |
451 |
|
|
452 |
uint64_t connectedComponents(Mat &L, const Mat &I, int connectivity){
|
|
453 |
int lDepth = L.depth();
|
|
454 |
if(lDepth == CV_8U){
|
|
455 |
connectedcomponents::NoOp<uint8_t> sop; return connectedComponents_sub1(L, I, connectivity, sop);
|
|
456 |
}else if(lDepth == CV_16U){
|
|
457 |
connectedcomponents::NoOp<uint16_t> sop; return connectedComponents_sub1(L, I, connectivity, sop);
|
|
458 |
}else if(lDepth == CV_32S){
|
|
459 |
connectedcomponents::NoOp<uint32_t> sop; return connectedComponents_sub1(L, I, connectivity, sop);
|
|
460 |
}else{
|
|
461 |
CV_Assert(false);
|
|
462 |
return 0;
|
|
463 |
}
|
|
464 |
}
|
|
465 |
|
|
466 |
uint64_t connectedComponents(Mat &L, const Mat &I, std::vector<ConnectedComponentStats> &statsv, int connectivity){
|
|
467 |
int lDepth = L.depth();
|
|
468 |
if(lDepth == CV_8U){
|
|
469 |
connectedcomponents::CCStatsOp<uint8_t> sop(statsv); return connectedComponents_sub1(L, I, connectivity, sop);
|
|
470 |
}else if(lDepth == CV_16U){
|
|
471 |
connectedcomponents::CCStatsOp<uint16_t> sop(statsv); return connectedComponents_sub1(L, I, connectivity, sop);
|
|
472 |
}else if(lDepth == CV_32S){
|
|
473 |
connectedcomponents::CCStatsOp<uint32_t> sop(statsv); return connectedComponents_sub1(L, I, connectivity, sop);
|
|
474 |
}else{
|
|
475 |
CV_Assert(false);
|
|
476 |
return 0;
|
|
477 |
}
|
|
478 |
}
|
363 |
479 |
|
364 |
480 |
}
|
365 |
481 |
|