modification of cmdlnopts

This commit is contained in:
eddyem
2015-12-08 16:16:00 +03:00
parent cb9c37fbd1
commit 4b14221751
19 changed files with 1465 additions and 179 deletions

View File

@@ -249,7 +249,7 @@ size_t *char2st(unsigned char *image, int W, int H, int W_0){
* @return allocated memory area with converted input image
*/
unsigned char *FC_filter(unsigned char *image, int W, int H){
if(W < 2 || H < 2) errx(1, "4-connect: image size too small");
if(W < 1 || H < 2) errx(1, "4-connect: image size too small");
unsigned char *ret = Malloc(W*H, 1);
int y = 0, w = W-1, h = H-1;
// top of image, y = 0
@@ -381,15 +381,6 @@ unsigned char *substim(unsigned char *im1, unsigned char *im2, int W, int H){
/*
* <=================== CONNECTED COMPONENTS LABELING ===================
*/
/*
#undef DBG
#undef EBUG
#define DBG(...)
*/
/**
* label 4-connected components on image
* (slow algorythm, but easy to parallel)
@@ -406,6 +397,13 @@ CCbox *cclabel4(unsigned char *Img, int W, int H, int W_0, size_t *Nobj){
return ret;
}
CCbox *cclabel4_1(unsigned char *Img, int W, int H, int W_0, size_t *Nobj){
unsigned char *I = FC_filter(Img, W_0, H);
#include "cclabling.h"
FREE(I);
return ret;
}
// label 8-connected components, look cclabel4
CCbox *cclabel8(unsigned char *I, int W, int H, int W_0, size_t *Nobj){
//unsigned char *I = EC_filter(Img, W_0, H);
@@ -423,6 +421,11 @@ CCbox *cclabel8_1(unsigned char *I, int W, int H, int W_0, size_t *Nobj){
return ret;
}
/*
* <=================== CONNECTED COMPONENTS LABELING ===================
*/
/*
* <=================== template ===================>
*/

View File

@@ -27,9 +27,9 @@
#include <stdlib.h>
#ifdef EBUG
#ifndef DBG
#define DBG(...) do{fprintf(stderr, __VA_ARGS__);}while(0)
#endif
#define DBG(...) do{fprintf(stderr, __VA_ARGS__);}while(0)
#else
#define DBG(...)
#endif
#define _U_ __attribute__((__unused__))
@@ -80,6 +80,7 @@ typedef struct {
CCbox *cclabel4(unsigned char *I, int W, int H, int W_0, size_t *Nobj);
CCbox *cclabel8(unsigned char *I, int W, int H, int W_0, size_t *Nobj);
CCbox *cclabel4_1(unsigned char *I, int W, int H, int W_0, size_t *Nobj);
CCbox *cclabel8_1(unsigned char *I, int W, int H, int W_0, size_t *Nobj);
#endif // __EROSION_DILATION_H__

View File

@@ -24,6 +24,7 @@ double t0 = dtime();
size_t N _U_ = 0, Ncur = 0;
size_t *labels = char2st(I, W, H, W_0);
int y;
/*
#ifdef EBUG
for(y = 0; y < H; y++){
size_t *ptr = &labels[y*W];
@@ -35,8 +36,9 @@ for(y = 0; y < H; y++){
}
FREE(labels); return ret;
#endif
printf("time for char2st: %gs\n", dtime()-t0);
t0 = dtime();
*/
//printf("time for char2st: %gs\n", dtime()-t0);
//t0 = dtime();
// Step 1: mark neighbours by strings
size_t *ptr = labels;
for(y = 0; y < H; y++){
@@ -50,8 +52,8 @@ t0 = dtime();
*ptr = Ncur;
}
}
printf("\n\ntime for step1: %gs (Ncur=%zd)\n", dtime()-t0, Ncur);
t0 = dtime();
//printf("\n\ntime for step1: %gs (Ncur=%zd)\n", dtime()-t0, Ncur);
//t0 = dtime();
DBG("Initial mark\n");
#ifdef EBUG
for(y = 0; y < H; y++){
@@ -72,11 +74,7 @@ for(y = 0; y < H; y++){
// now we should scan image again to rebuild enumeration
// THIS PROCESS AVOID PARALLELISATION???
Ncur = 0;
size_t h = H-1
#ifdef LABEL_8
,w = W-1;
#endif
;
size_t h = H-1, w = W-1;
inline void remark(size_t old, size_t *newv){
size_t new = *newv;
if(assoc[old] == new){
@@ -153,6 +151,13 @@ for(y = 0; y < H; y++){
*ptr = 0; // erase this hermit!
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 = ++Ncur;
}
@@ -174,8 +179,8 @@ for(y = 0; y < H; y++){
remark(curval, &upmark);
}
}
printf("time for step 2: %gs, found %zd objects\n", dtime()-t0, Ncur);
t0 = dtime();
//printf("time for step 2: %gs, found %zd objects\n", dtime()-t0, Ncur);
//t0 = dtime();
// Step 3: rename markers
DBG("rename markers\n");
// first correct complex assotiations in assoc
@@ -188,7 +193,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++){
@@ -202,3 +207,4 @@ for(y = 0; y < H; y++){
#endif
FREE(assoc);
FREE(labels);
printf("%6.4f\t%zd\n", dtime()-t0, Ncur);

View File

@@ -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);

View File

@@ -57,117 +57,58 @@ long throw_random_seed(){
int main(int ac, char **v){
int i,j, W = 28, H = 28, W_0;
int i,j, W = 500, H = 500, W_0;
//double midX = (W - 1.0)/ 4. - 1., midY = (H - 1.) / 4. - 1.;
//double ro = sqrt(midX*midY), ri = ro / 1.5;
bool *inp = Malloc(W * H, sizeof(bool));
printf("\n");
srand48(throw_random_seed());
for(i = 0; i < H; i++)
for(j = 0; j < W; j++)
inp[i*W+j] = (drand48() > 0.5);
/*
for(i = 0; i < H/2; i++){
for(j = 0; j < W/2; j++){
double x = j - midX, y = i - midY;
double r = sqrt(x*x + y*y);
if((r < ro && r > ri) || fabs(x) == fabs(y))
inp[i*W+j] = inp[i*W+j+W/2] = inp[(i+H/2)*W+j] = inp[(i+H/2)*W+j+W/2] = true;
}
}*/
unsigned char *b2ch = bool2char(inp, W, H, &W_0);
FREE(inp);
printf("inpt: (%dx%d)\n", W_0, H);
printC(b2ch, W_0, H);
unsigned char *eros = erosion(b2ch, W_0, H);
printf("erosion: (%dx%d)\n", W_0, H);
printC(eros, W_0, H);
unsigned char *dilat = dilation(b2ch, W_0, H);
printf("dilation: (%dx%d)\n", W_0, H);
printC(dilat, W_0, H);
unsigned char *erdilat = dilation(eros, W_0, H);
printf("\n\ndilation of erosion (openinfg: (%dx%d)\n", W_0, H);
printC(erdilat, W_0, H);
unsigned char *dileros = erosion(dilat, W_0, H);
printf("erosion of dilation (closing): (%dx%d)\n", W_0, H);
printC(dileros, W_0, H);
printf("\n\n\n image - opening of original minus original:\n");
unsigned char *immer = substim(b2ch, erdilat, W_0, H);
printC(immer, W_0, H);
FREE(eros); FREE(dilat); FREE(erdilat); FREE(dileros);
inp = char2bool(immer, W, H, W_0);
printf("\n\nAnd boolean for previous image (%dx%d):\n ",W,H);
printB(inp, W, H);
FREE(immer);
immer = FC_filter(b2ch, W_0, H);
printf("\n\n\nFilter for 4-connected areas searching:\n");
printC(immer, W_0, H);
FREE(immer);
inp[i*W+j] = (drand48() > 0.7);
unsigned char *b2ch = bool2char(inp, W, H, &W_0);//, *immer;
/*printf("image:\n");
printC(b2ch, W_0, H);*/
// immer = FC_filter(b2ch, W_0, H);
// printf("\n\n\nFilter for 4-connected areas searching:\n");
// printC(immer, W_0, H);
// FREE(immer);
size_t NL;
printf("\nmark 8-connected components:\n");
// printf("\nmark 4-connected components:\n");
printf("4new\t");
cclabel4_1(b2ch, W, H, W_0, &NL);
// printf("\nmark 4-connected components (old):\n");
printf("4old\t");
cclabel4(b2ch, W, H, W_0, &NL);
// printf("\nmark 8-connected components:\n");
printf("8new\t");
cclabel8_1(b2ch, W, H, W_0, &NL);
printf("\nmark 8-connected components (old):\n");
// printf("\nmark 8-connected components (old):\n");
printf("8old\t");
cclabel8(b2ch, W, H, W_0, &NL);
FREE(b2ch);
return 0;
W = H = 1000;
FREE(inp);
inp = Malloc(W * H, sizeof(bool));
for(i = 0; i < H; i++)
for(j = 0; j < W; j++)
inp[i*W+j] = (drand48() > 0.5);
b2ch = bool2char(inp, W, H, &W_0);
FREE(inp);
printf("\n\n\n1) 1 cc4 for 2000x2000 .. ");
fflush(stdout);
double t0 = dtime();
for(i = 0; i < 1; i++){
CCbox *b = cclabel4(b2ch, W, H, W_0, &NL);
FREE(b);
}
printf("%.3f s\n", dtime() - t0);
printf("\n2) 1 cc8 for 2000x2000 .. ");
fflush(stdout);
t0 = dtime();
for(i = 0; i < 1; i++){
CCbox *b = cclabel8(b2ch, W, H, W_0, &NL);
FREE(b);
}
printf("%.3f s\n", dtime() - t0);
printf("\n3) 1 cc8_1 for 2000x2000 .. ");
fflush(stdout);
t0 = dtime();
for(i = 0; i < 1; i++){
CCbox *b = cclabel8_1(b2ch, W, H, W_0, &NL);
FREE(b);
}
printf("%.3f s\n", dtime() - t0);
/* printf("\n\n\n\nSome tests:\n1) 10000 dilations for 2000x2000 .. ");
fflush(stdout);
double t0 = dtime();
for(i = 0; i < 10000; i++){
unsigned char *dilat = dilation(b2ch, W, H);
free(dilat);
}
printf("%.3f s\n", dtime() - t0);
printf("2) 10000 erosions for 2000x2000 .. ");
fflush(stdout);
t0 = dtime();
for(i = 0; i < 10000; i++){
unsigned char *dilat = erosion(b2ch, W, H);
free(dilat);
}
printf("%.3f s\n", dtime() - t0);
printf("3) 10000 image substitutions for 2000x2000 .. ");
fflush(stdout);
unsigned char *dilat = dilation(b2ch, W, H);
t0 = dtime();
for(i = 0; i < 10000; i++){
unsigned char *res = substim(b2ch, dilat, W, H);
free(res);
}
printf("%.3f s\n", dtime() - t0);*/
return 0;
}
/*
* bench
* names = ["4new"; "4old"; "8new"; "8old"]; for i = 1:4; nm = names(i,:); time = []; sz = []; for i = 1:52; if(name{i} == nm) time = [time speed(i)]; sz = [sz num(i)]; endif;endfor; ts = time./sz; printf("%s: time/object = %.1f +- %.1f us\n", nm, mean(ts)*1e6, std(ts)*1e6);endfor;
*
* 0.25 megapixels
* 4new: time/object = 1.9 +- 0.4 us
* 4old: time/object = 1.7 +- 0.1 us
* 8new: time/object = 3.9 +- 0.1 us
* 8old: time/object = 13.5 +- 2.1 us
*
* 1 megapixels ---> [name speed num] = textread("1megapixel", "%s %f %f");
* 4new: time/object = 5.5 +- 0.5 us
* 4old: time/object = 5.3 +- 0.0 us
* 8new: time/object = 13.3 +- 0.1 us
* 8old: time/object = 47.6 +- 2.2 us
*
* 4 megapixels ---> [name speed num] = textread("4megapixels", "%s %f %f");
* 4new: time/object = 21.3 +- 1.5 us
* 4old: time/object = 22.3 +- 2.2 us
* 8new: time/object = 57.6 +- 1.3 us
* 8old: time/object = 195.5 +- 11.7 us
*
*
*/