/***************************************************************************** File: filter.c July 10 Authors: Alin Alecu Ioan Tabus (tabus@cs.tut.fi) Doina Petrescu Purpose: Fast stack and Boolean filtering Copyright 1997 Alin Alecu, Ioan Tabus, Doina Petrescu. All Rights Reserved. These programs are supplied free of charge for research purposes only, and may not 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. /****************************************************************************/ /* */ /* Filtering of Corrupted Image */ /* */ /* Inputs: */ /* - desired image */ /* - noisy image */ /* - filter file */ /* Output: */ /* - image file */ /* */ /* Requires insertion of: - desired window size */ /* - window grid format */ /* */ /* */ /****************************************************************************/ #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 NUM 258064 #define StartPR(i,j) (i==2 && j==2) #define NewLinePR(j) (j==2) #define CompGT(a,b) (a > b) int f_opt[LG]; /* Boolean filter */ double MAE; /* Mean average error */ unsigned char imxr[IR][IC], /* Pixels read from noisy image */ imdr[IR][IC], /* Pixels read from desired image */ imfr[IR][IC]; /* Filtered image pixels */ 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 */ void Filter_N0_N1(); /* Performs image filtering */ void GetWindow(); /* Function to obtain user selected window grid */ /* ******************************************************************** */ void main( argc, argv) int argc; char *argv[]; { FILE *fpx,*fpd,*fp; char name[40],namex[40],named[40],namef[40]; int i,j,nr; clock_t cl1=clock(); /* Read images from disk */ printf("\n ===================== "); printf("\n Program for filtering "); printf("\n ===================== \n"); /* Reading images */ printf("\n For computing MAE of result you'll need the desired image \n"); if(argc==5) strcpy(named,argv[1]); else { printf("\n Name of desired image: "); scanf("%s",named); } if(argc==5) strcpy(namex, argv[2]); else { printf("\n Name of noisy image: "); scanf("%s",namex); } if(argc==5) strcpy(name,argv[3]); else { printf("\n Name of vector form of Boolean filter file: "); scanf("%s",name); } if(argc==5) strcpy(namef,argv[4]); else { printf("\n The RESULT will be here: "); scanf("%s",namef); } fpd=fopen(named,"rb"); if(fpd==NULL) {printf("\n Error reading %s\n",named); return;} fpx=fopen(namex,"rb"); if(fpx==NULL) {printf("\n Error reading %s\n",namex); fclose(fpd);return;} 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>= indici[ix-1]; */ cccb >>= (winsize - indici[ix-1] - 1); nb ^=cccb; } imfr[i][j]=filtrata; if((d-filtrata)>0) MAE+=(double)(d-filtrata); else MAE-=(double)(d-filtrata); } } /* ******************************************************************** */ void dfromimd(ilin,icol,d) int *d ,ilin,icol; { *d=(int)(*(imdr[ilin]+icol)); } /* ******************************************************************** */ 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-1]; /* 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] = Index[j-1]; } Vector[j+1] = tmp; Index[j] = itmp; } }