mirror of
https://github.com/eddyem/eddys_snippets.git
synced 2026-03-22 01:31:16 +03:00
modification of cmdlnopts
This commit is contained in:
@@ -23,58 +23,46 @@ double t0 = dtime();
|
||||
|
||||
CCbox *ret = NULL;
|
||||
size_t N = 0, // current label
|
||||
Ntot = 0, // number of found objects
|
||||
Nmax = W*H/4; // max number of labels
|
||||
size_t *labels = char2st(I, W, H, W_0);
|
||||
int w = W - 1;
|
||||
printf("time for char2st: %gs\n", dtime()-t0);
|
||||
int w = W - 1, h = H - 1;
|
||||
//printf("time for char2st: %gs\n", dtime()-t0);
|
||||
int y;
|
||||
size_t *assoc = Malloc(Nmax, sizeof(size_t)); // allocate memory for "remark" array
|
||||
inline void remark(size_t old, size_t *newv){
|
||||
size_t new = *newv;
|
||||
if(old == new || assoc[old] == new || assoc[new] == old){
|
||||
DBG("(%zd)\n", new);
|
||||
size_t last_assoc_idx = 0; // last index filled in assoc array
|
||||
size_t currentnum = 0; // current pixel number
|
||||
inline void remark(size_t old, size_t new){ // remark in assoc[] pixel with value old to assoc[new] or vice versa
|
||||
size_t New = assoc[new], Old = assoc[old];
|
||||
if(Old == New){
|
||||
DBG("components equal (%zd->%zd and %zd->%zd)\n", old, Old, new, New);
|
||||
return;
|
||||
}
|
||||
DBG("[cur: %zx, tot: %zd], %zx -> %zx ", N, Ntot, old, new);
|
||||
if(!assoc[old]){ // try to remark non-marked value
|
||||
assoc[old] = new;
|
||||
DBG("\n");
|
||||
return;
|
||||
DBG("N=%zd, curr=%zd, old:%zd->%zd <-> new:%zd->%zd ", N, currentnum, old, Old, new, New);
|
||||
// now we must check Old: if Old<New we should swap them
|
||||
if(Old < New){
|
||||
register size_t _tmp_ = Old; Old = New; New = _tmp_; // swap values
|
||||
_tmp_ = old; old = new; new = _tmp_;
|
||||
}
|
||||
// value was remarked -> we have to remark "new" to assoc[old]
|
||||
// and decrement all marks, that are greater than "new"
|
||||
size_t ao = assoc[old];
|
||||
DBG("(remarked to %zx) ", ao);
|
||||
DBG(" {old: %zd, new: %zd, a[o]=%zd, a[n]=%zd} ", old, new, assoc[old], assoc[new]);
|
||||
// now we must check assoc[old]: if it is < new -> change *newv, else remark assoc[old]
|
||||
if(ao < new)
|
||||
*newv = ao;
|
||||
else{
|
||||
size_t x = ao; ao = new; new = x; // swap values
|
||||
}
|
||||
int m = (old > new) ? old : new;
|
||||
int xx = m + W / 2;
|
||||
if(xx > Nmax) xx = Nmax;
|
||||
DBG(" [[xx=%d]] ", xx);
|
||||
OMP_FOR()
|
||||
for(int i = 1; i <= xx; i++){
|
||||
// decrement counters for current value (because we make merging)
|
||||
--currentnum;
|
||||
//OMP_FOR()
|
||||
for(size_t i = 1; i < last_assoc_idx; ++i){
|
||||
size_t m = assoc[i];
|
||||
if(m < new) continue;
|
||||
if(m == new){
|
||||
assoc[i] = ao;
|
||||
DBG("remark %x (%zd) to %zx ", i, m, ao);
|
||||
}else{
|
||||
DBG("assoc[%zd]=%zd ", i, m);
|
||||
if(m < Old) continue; // lower values
|
||||
if(m == Old){ // change all old markers to new
|
||||
assoc[i] = New;
|
||||
DBG("remark %zd->%zd to ->%zd ", i, m, New);
|
||||
}else{ // decrement all higher values
|
||||
DBG("decr %zd->%zd, ", i, m);
|
||||
assoc[i]--;
|
||||
DBG("decr %x: %zx, ", i, assoc[i]);
|
||||
}
|
||||
}
|
||||
DBG("\n");
|
||||
Ntot--;
|
||||
}
|
||||
t0 = dtime();
|
||||
//t0 = dtime();
|
||||
size_t *ptr = labels;
|
||||
for(y = 0; y < H; y++){ // FIXME!!!! throw out that fucking "if" checking coordinates!!!
|
||||
for(y = 0; y < H; y++){
|
||||
bool found = false;
|
||||
size_t curmark = 0; // mark of pixel to the left
|
||||
for(int x = 0; x < W; x++, ptr++){
|
||||
@@ -86,49 +74,58 @@ printf("time for char2st: %gs\n", dtime()-t0);
|
||||
found = true;
|
||||
// now check neighbours in upper string:
|
||||
if(U){
|
||||
//#ifdef LABEL_8
|
||||
#ifdef LABEL_8
|
||||
if(x && U[-1]){ // there is something in upper left corner -> use its value
|
||||
upmark = U[-1];
|
||||
}else // check point above only if there's nothing in left up
|
||||
//#endif
|
||||
#endif
|
||||
if(U[0]) upmark = U[0];
|
||||
//#ifdef LABEL_8
|
||||
#ifdef LABEL_8
|
||||
if(x < w && U[1]){ // there's something in upper right
|
||||
if(upmark){ // to the left of it was pixels
|
||||
remark(U[1], &upmark);
|
||||
remark(U[1], upmark);
|
||||
}else
|
||||
upmark = U[1];
|
||||
}
|
||||
//#endif
|
||||
#endif
|
||||
}
|
||||
if(!upmark){ // there's nothing above - set current pixel to incremented counter
|
||||
DBG("line #%d (w = %d) ", y, h);
|
||||
#ifdef LABEL_8 // check, whether pixel is not single
|
||||
size_t *D = (y < w) ? &ptr[W] : NULL;
|
||||
if( !(x && ((D && D[-1]) || ptr[-1])) // no left neighbours
|
||||
size_t *D = (y < h) ? &ptr[W] : NULL;
|
||||
if( !(x && ((D && D[-1]) /*|| ptr[-1]*/)) // no left neighbours
|
||||
&& !(x < w && ((D && D[1]) || ptr[1])) // no right neighbours
|
||||
&& !(D && D[0])){ // no neighbour down
|
||||
*ptr = 0; // erase this hermit!
|
||||
DBG("hermit!\n");
|
||||
continue;
|
||||
}
|
||||
#else
|
||||
// no neighbour down & neighbour to the right -> hermit
|
||||
if((y < h && ptr[W] == 0) && (x < w && ptr[1] == 0)){
|
||||
*ptr = 0; // erase this hermit!
|
||||
DBG("hermit!\n");
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
upmark = ++N;
|
||||
Ntot++;
|
||||
assoc[upmark] = upmark; // refresh "assoc"
|
||||
assoc[upmark] = ++currentnum; // refresh "assoc"
|
||||
DBG("assoc[%zd] = %zd\n", upmark, currentnum);
|
||||
last_assoc_idx = upmark + 1;
|
||||
}
|
||||
*ptr = upmark;
|
||||
curmark = upmark;
|
||||
//remark(curval, &upmark);
|
||||
}else{ // there was something to the left -> we must chek only U[1]
|
||||
if(U){
|
||||
if(x < w && U[1]){ // there's something in upper right
|
||||
remark(U[1], &curmark);
|
||||
remark(U[1], curmark);
|
||||
}
|
||||
}
|
||||
*ptr = curmark;
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("time for step 2: %gs, found %zd objects\n", dtime()-t0, Ntot);
|
||||
//printf("time for step 2: %gs, found %zd objects\n", dtime()-t0, currentnum);
|
||||
DBG("Initial mark\n");
|
||||
#ifdef EBUG
|
||||
for(y = 0; y < H; y++){
|
||||
@@ -141,7 +138,7 @@ for(y = 0; y < H; y++){
|
||||
}
|
||||
#endif
|
||||
|
||||
t0 = dtime();
|
||||
//t0 = dtime();
|
||||
// Step 2: rename markers
|
||||
DBG("rename markers\n");
|
||||
// first correct complex assotiations in assoc
|
||||
@@ -154,7 +151,7 @@ t0 = dtime();
|
||||
*ptr = assoc[p];
|
||||
}
|
||||
}
|
||||
printf("time for step 3: %gs\n", dtime()-t0);
|
||||
//printf("time for step 3: %gs\n", dtime()-t0);
|
||||
#ifdef EBUG
|
||||
printf("\n\n");
|
||||
for(y = 0; y < H; y++){
|
||||
@@ -168,3 +165,4 @@ for(y = 0; y < H; y++){
|
||||
#endif
|
||||
FREE(assoc);
|
||||
FREE(labels);
|
||||
printf("%6.4f\t%zd\n", dtime()-t0, currentnum);
|
||||
|
||||
Reference in New Issue
Block a user