/***************************************************************************** File: coeff1.c November 9 Authors: Alin Alecu Ioan Tabus (tabus@cs.tut.fi) Doina Petrescu Bogdan Dumitrescu Purpose: Fast computation of cost coefficients Copyright 1997 Alin Alecu, Ioan Tabus, Doina Petrescu, Bogdan Dumitrescu. All Rights Reserved. Reference: @ARTICLE{TFI, author = { I. T\u{a}bu\c{s} and D. Petrescu and M. Gabbouj }, title = {A Training Framework for Stack and {Boolean} Filtering-{F}ast optimal design procedures and robustness case study}, journal = TRIP # " Special Issue on Nonlinear Image Processing", volume = "IP-5", year = "1996", month = "June", pages = "809--826", } These programs are supplied free of charge for research purposes only, and may not be sold or incorporated into any commercial product. There is ABSOLUTELY NO WARRANTY of any sort, nor any undertaking that they are fit for ANY PURPOSE WHATSOEVER. Use them at your own risk. If you do happen to find a bug, or have modifications to suggest, please report it to tabus@cs.tut.fi. The copyright notice above and this statement of conditions must remain an integral part of each and every copy made of this file. ******************************************************************************/ /****************************************************************************/ /* */ /* Computation of cost coefficients */ /* */ /* Inputs: */ /* - desired image */ /* - noisy image or images (if several images, their names should end */ /* with the suffix "_00", "_01" etc.; the number of such images will */ /* be automatically detected) */ /* Output: */ /* - costs file */ /* */ /* Allows insertion of: - desired window size */ /* - window grid format */ /* */ /* */ /****************************************************************************/ #define MERGE_SORT /* undefined: insert_sort */ #include #include #include #include #define N 21 /* Max. size of window. Eg: 5 9 10 17 19 21 */ #define LG 2097152 /* Max. no. of coefficients. Eg: 32 512 1024 131072 524288 2097152 */ #define IR 512 /* Dimensions in pixels */ #define IC 512 /* of picture */ #define StartPR(i,j) (i==2 && j==2) #define NewLinePR(j) (j==2) #define CompGT(a,b) (a > b) long C[LG]; /* Coefficients */ long C0; /* Coefficient C0 */ unsigned char imxr[IR][IC], /* Pixels read from noisy image */ imdr[IR][IC]; /* Pixels read from desired image */ unsigned int num[N]; /* Pozition of selected pixel */ /* relative to window location */ int winsize; /* Selected size of window */ int coefno; /* No. of necessary coeff. */ int sel[N], /* Selected pixels */ ih[N],iv[N], /* Common pixel indexes belonging to old window */ /* vector, corresponding to horizontal and vertical */ /* movement of window */ corh[N],corv[N], /* Common pixel indexes belonging to new window */ /* vector, corresponding to horizontal and vertical */ /* movement of window */ nohnew,novnew, /* Total no. of new pixels introduced in new window */ /* vector, corresponding to horizontal and vertical */ /* movement of window */ ihnew[N-1],ivnew[N-1]; /* New pixels introduced in new window */ /* vector, corresponding to horizontal and vertical */ /* movement of window */ clock_t clt; void Estimate_N0_N1(); /* Actual algorithm to obtain coefficients */ void GetWindow(); /* Function to obtain user selected window grid */ #ifdef MERGE_SORT void Merge( int *vec1, int *ind1, int len1, /* first vector */ int *vec2, int *ind2, int len2, /* second vector */ int *vec, int *ind ); /* merged vector */ #endif /* ******************************************************************** */ int main( argc, argv ) int argc; char *argv[]; { FILE *fpx,*fpd,*fp; char name[40], namex[40], named[40], namexn[40]; int i,j,one_image, ni; /* Read images from disk */ printf("\n ==========================================="); printf("\n Compute cost coefficients for filter design"); printf("\n ===========================================\n"); if(argc==4) strcpy(named, argv[1]); else { printf("\n Name of desired image: "); scanf("%s",named); } if(argc==4) strcpy(namex, argv[2]); else { printf("\n Name of (basic) noisy image: "); scanf("%s",namex); } if(argc==4) strcpy(name, argv[3]); else { printf("\n Name of coefficients file: "); scanf("%s",name); } fpd=fopen(named,"rb"); if(fpd==NULL) {printf("\n Error reading %s\n",named); return 0;} strcpy( namexn, namex ); fpx=fopen(namex,"rb"); if ( fpx==NULL ) /* noisy file not found, maybe it's numbered */ { strcat( namexn, "_0" ); fpx = fopen(namexn,"rb"); if ( fpx == NULL ) /* this time it's an error */ { printf("\n Error reading %s\n", namex); fclose(fpd); return 0; } one_image = 0; } else /* there will be only one noisy file */ one_image = 1; /* Selection of window grid */ GetWindow(); for(i=0;iN) ); coefno=pow(2,winsize); /* Compute no. of necessary coefficents */ printf("\nPixel 0 : 12 (by default)\n"); qx=div(12,5); num[0]=(qx.quot*IC) + qx.rem; sel[0]=12; for(i=0;i<=24;i++) chosen[i]=0; chosen[12]=1; /* Input desired pixels */ for(i=1;i24) || (chosen[x])); chosen[x]=1; qx=div(x,5); num[i]= (qx.quot*IC) + qx.rem; sel[i]=x; } scanf("%c",&ch); printf("\nIs it OK ? (y/n) : "); scanf("%c",&ch); if(ch !='y') GetWindow(); /* If all selections are OK, determine which pixels will remain and */ /* which will leave the window while it moves */ else { nohnew=0;novnew=0; for(i=0;i= vect[ix]) C[nb]-=numar; else { if (d < vect[ix -1]) C[nb]+=numar; else { C[nb]-=(2*d - vect[ix-1] - vect[ix]); } } cccb = coefno / 2; /* cccb >>= indici[ix-1]; */ cccb >>= (winsize - indici[ix-1] - 1); nb ^=cccb; } } } /* ******************************************************************** */ void dfromimd(ilin,icol,d) int *d ,ilin,icol; { *d=(int)(*(imdr[ilin]+icol)); } /* ******************************************************************** */ /* simplified version: same indices in vector and index vector */ void InsertSort(int Vector[N], int n, int Index[N], int start) { int i,j,tmp,itmp; for (i = start; i < n; i++) { tmp = Vector[i]; itmp = Index[i]; /* Shift elements down until insertion point found. */ for (j = i-1; j >= 0 && CompGT(Vector[j],tmp); j--) { Vector[j+1] = Vector[j]; Index[j+1] = Index[j]; } Vector[j+1] = tmp; Index[j+1] = itmp; } } /* ******************************************************************** */ #ifdef MERGE_SORT /* merge two increasing sequences */ void Merge( int *vec1, int *ind1, int len1, /* first vector */ int *vec2, int *ind2, int len2, /* second vector */ int *vec, int *ind ) /* merged vector */ { int k1, k2, k; int *v, *i; k1 = k2 = 0; while ( k1 < len1 && k2 < len2 ) { if ( CompGT( *vec1, *vec2 ) ) { *vec++ = *vec2++; *ind++ = *ind2++; k2++; } else { *vec++ = *vec1++; *ind++ = *ind1++; k1++; } } if ( k1 == len1 ) { v = vec2; i = ind2; k = len2 - k2; } else { v = vec1; i = ind1; k = len1 - k1; } while ( k-- ) { *vec++ = *v++; *ind++ = *i++; } } #endif