asked on
ASKER
unsigned char *I ;
ReadTiffFile() ;
//it gets data of tiff file in memory pointed by I.
//Also finds Image length, and width and stores same in Il, Iw respectively.
// now start applying Median Filter on Image Data
void MedianFilter()
{
unsigned long il,iw,i,j;
il=Il;
iw=Iw;
// form an Array named array to store resulting pixels seperately. array must be of same size as I.
array= (unsigned char **)malloc(il*sizeof(unsigned char *));
if(array==NULL)
{
printf("Malloc Failed !") ;
exit(0);
}
for(i=0;i<il;i++)
{
array[i]=(unsigned char *)malloc(iw*sizeof(unsigned char));
}
// now start Median Filter
unsigned long r,c ;
for(r=0;r<il;r++)
{
for(c=0;c<iw;c++)
{
//MedianFilter each pixel (r, c)
// form an array A[] with its neibours, excluding pixel itself.
int i,j,k=0,n ;
unsigned char median, A[9];
if(r==0 && c==0) //1. First Cell
{
//form an array of 3 neighbours
n=3;
A[0]= *(I+0*iw+1); // *(I[0][1]) ;
A[1]= *(I+1*iw+0); // *I[1][0];
A[2]= *(I+1*iw+1); // *I[1][1];
}
else if (r==0 && c>0 && c<iw) //2. first row
{
// form an array of 5 neighbours
n=5;
A[k++]= *(I+0*iw+c-1); //*I[0][c-1] ;
A[k++]= *(I+0*iw+c+1); //*I[0][c+1];
for(int t=-1;t<2;t++)
A[k++]= *(I+1*iw+c+t); //*I[1][c+t];
}
else if(r>0 && r<il && c==0) //3.first col
{
//form an array of 5 neighbours
n=5;
A[k++]= *(I+(r-1)*iw+0); //*I[r-1][0] ;
A[k++]= *(I+(r+1)*iw+0); //*I[r+1][0];
for(t=-1;t<2;t++)
A[k++]=*(I+(r+t)*iw+1); //*I[r+t][1];
}
else if(c==iw-1 && r==0) //4. Top Right Cell
{
n=3;
A[k++]= *(I+0*iw+c-1);
A[k++]= *(I+1*iw+c-1);
A[k++]= *(I+1*iw*c);
}
else if(r==il-1 && c==0) // 5.Bottom Left Cell
{
n=3 ;
A[k++]=*(I+(r-1)*iw+0);
A[k++]=*(I+(r-1)*iw+1);
A[k++]=*(I+r*iw+1);
}
else if(r==il-1 && c==iw-1) //6.Last Cell
{
n=3;
A[k++]=*(I+r*iw+c-1);
A[k++]=*(I+(r-1)*iw+c-1);
A[k++]=*(I+(r-1)*iw+c);
}
else if(r==il-1 && c>0 && c<iw-1) //7. last row
{
n=5;
A[k++]=*(I+r*iw+c-1) ;
A[k++]=*(I+r*iw+c+1);
for(t=-1;t<2;t++)
A[k++]=*(I+(r-1)*iw+c+t);
}
else if(c==iw-1 && r>0 && r<il-1) //8.last col
{
n=5;
A[k++]=*(I+(r-1)*iw+c);
A[k++]=*(I+(r+1)*iw+c);
for(t=-1;t<2;t++)
A[k++]=*(I+(r+t)*iw+c-1);
}
else if(c>0 && c<iw-1 && r>0 && r<il-1) //9 anywhere in between
{
n=8;
// all 8 elements are added here.
A[0]=*(I+(r-1)*iw+(c-1) );
A[1]=*(I+(r-1)*iw+c);
A[2]=*(I+(r-1)*iw+(c+1));
A[3]=*(I+(r)*iw+(c-1));
A[4]=*(I+(r)*iw+(c+1));
A[5]=*(I+(r+1)*iw+(c-1));
A[6]=*(I+(r+1)*iw+(c));
A[7]=*(I+(r+1)*iw+(c+1));
}
else
{
//ERROR
printf(" Error ");
exit(0);
}
//find Median of n elemets of A[]
median=FindMedian(A,n);
//store this median in array [][]
array[r][c]=median;
}
}
}//end of MedianFilter()
unsigned char FindMedian(unsigned char A[], int n)
{
// Find Median of n elements.
int tmp,i,j;
//sort using Bubble Sort
for(i=0;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(A[j] > A[j+1])
{
tmp=A[j];
A[j]=A[j+1];
A[j+1]=tmp;
}
}
}
/*
printf("\n For %d elements, sorted values are ",n);
for(i=0;i<n;i++)
printf(" %d ", A[i]) ;
printf(" Returning %d", A[n/2]);
*/
return A[n/2];
}//end of Median()
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
void MedFil(const unsigned long il, const unsigned long iw)
{
printf("\nMed Fill running for il,iw=%lu, %lu", il,iw) ;
unsigned long r,c ;
arr = (unsigned char **)malloc(il*sizeof(unsigned char *));
if(arr==NULL)
DisplayError("Malloc Failed !") ;
for(c=0;c<il;c++)
{
arr[c]=(unsigned char *)malloc(iw*sizeof(unsigned char));
}
unsigned char tmp;
for(r=0;r<il;r++)
{
for(c=0;c<iw;c++)
{
tmp=*(I+r*iw+c);
arr[r][c]=tmp;
}
}
}
ASKER
ASKER
void MedFil(const unsigned long il, const unsigned long iw)
{
printf("\nMed Fill running for il,iw=%lu, %lu", il,iw) ;
unsigned long r,c ;
arr = (unsigned char **)malloc(il*sizeof(unsigned char *));
if(arr==NULL)
DisplayError("Malloc Failed !") ;
for(c=0;c<il;c++)
{
arr[c]=(unsigned char *)malloc(iw*sizeof(unsigned char));
}
unsigned char tmp;
for(r=0;r<il;r++)
{
for(c=0;c<iw;c++)
{
tmp=*(I+r*iw+c);
arr[r][c]=tmp;
}
}
//print in tmp file.
ofstream f("tmp.txt");
for(r=0;r<il;r++)
{
printf("\n%lu", r);
for(c=0;c<iw;c++)
{
f<<int(arr[r][c])<< " ";
}
f<<endl;
}
}
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
ASKER
C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.
TRUSTED BY
Note that you have to copy the image in the process, or at least keep enough source pixels in a buffer to cover the neighborhood.