Skip to content

Instantly share code, notes, and snippets.

@uugan
Created April 23, 2013 13:15
Show Gist options
  • Save uugan/5443451 to your computer and use it in GitHub Desktop.
Save uugan/5443451 to your computer and use it in GitHub Desktop.
/* Smoothing image by homogeneous domain 5x5 */
float Smoothby5x5(unsigned char *inbuf, unsigned char* outbuf, int height, int width){
int start = GetTickCount();
int cnt=0;
int s[9],sum[9];
int a,b,c,d,e,f,g,h,ii,jj,k,l,n,o,p,q,r,ss,t,u,v,w,x,y;
double* out = new double[height*width];
for(int i=2; i<height - 2;i++)
for(int j=2;j<width-2;j++){
/*Finding all letters*/
a=inbuf[(i-2)*width+j-2]; b=inbuf[(i-2)*width+j-1];
c=inbuf[(i-2)*width+j]; d=inbuf[(i-2)*width+j+1];
e=inbuf[(i-2)*width+j+2]; f=inbuf[(i-1)*width+j-2];
g=inbuf[(i-1)*width+j-1]; h=inbuf[(i-1)*width+j];
ii=inbuf[(i-1)*width+j+1]; jj=inbuf[(i-1)*width+j+2];
k=inbuf[i*width+j-2]; l=inbuf[i*width+j-1];
n=inbuf[i*width+j+1]; o=inbuf[i*width+j+2];
p=inbuf[(i+1)*width+j-2]; q=inbuf[(i+1)*width+j-1];
r=inbuf[(i+1)*width+j]; ss=inbuf[(i+1)*width+j+1];
t=inbuf[(i+1)*width+j+2]; u=inbuf[(i+2)*width+j-2];
v=inbuf[(i+2)*width+j-1]; w=inbuf[(i+2)*width+j];
x=inbuf[(i+2)*width+j+1]; y=inbuf[(i+2)*width+j+2];
sum[0] = g+h+ii+l+n+q+r+ss;
s[0]=abs(8*inbuf[i*width+j]-sum[0]);
sum[1] = f+g+k+l+q+p;
s[1]=abs(6*inbuf[i*width+j]-sum[1]);
sum[2] =a+b+f+g+h+l;
s[2]=abs(6*inbuf[i*width+j]-sum[2]);
sum[3] = b+c+d+g+h+ii;
s[3]=abs(6*inbuf[i*width+j]-sum[3]);
sum[4]=d+e+h+ii+jj+n;
s[4]=abs(6*inbuf[i*width+j]-sum[4]);
sum[5]=ii+jj+n+o+ss+t;
s[5]=abs(6*inbuf[i*width+j]-sum[5]);
sum[6]=n+r+ss+t+x+y;
s[6]=abs(6*inbuf[i*width+j]-sum[6]);
sum[7] = q+r+ss+v+w+x;
s[7]=abs(6*inbuf[i*width+j]-sum[7]);
sum[8]=l+p+q+r+u+v;
s[8]=abs(6*inbuf[i*width+j]-sum[8]);
int min = s[0],indx=0;
for(int pp=0;pp<9;pp++){
if(min>s[pp]){
min = s[pp];
indx=pp;
}
}
if(indx==0)
out[i*width+j] = (sum[indx]+inbuf[i*width+j])/9;
else
out[i*width+j] = (sum[indx]+inbuf[i*width+j])/7;
} /*Finding max, min values */
double max=-999,min=999;
for(int i=2;i<height-2;i++)
for(int j=2;j<width-2;j++){
if(out[i*width+j]>max)max=out[i*width+j];
if(out[i*width+j]<min)min=out[i*width+j];
}
/* double array to unsigned char */
for(int i=2;i<height-2;i++)
for(int j=2;j<width-2;j++)
outbuf[i*width+j]=(unsigned char)(out[i*width+j]-min)/(max-min)*255 ;
return (float)(GetTickCount()-start);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment