📄 middle.htm
字号:
lastrow[i]=fgetc(fp1);
//Process the other rows
for(i=0;i<254;i++)
{ fseek(fp1,i*256,0);
for(h=0;h<3;h++)
for(g=0;g<256;g++)
img[h][g]=fgetc(fp1);
//Left border
transimg[0]=img[1][0];
//Right border
transimg[255]=img[1][255];
//Process the related data
for(k=0;k<254;k++)
{ result=0;
for(m=0;m<3;m++)
for(n=0;n<3;n++)
{result+=img[m][k+n]*mask[m][n];
if(result<0)result=0;
if(result>255)result=255;}
transimg[k+1]=result;
}
for(l=0;l<256;l++)
{fseek(fp1,256*(i+1),0);
fputc(transimg[l],fp2); /*write data to file2*/
}
}
fseek(fp1,255*256,0); /*mov the pointer*/
for(i=0;i<256;i++) /*write the last row to file 2*/
fputc(lastrow[i],fp2);
fclose(fp2);
}
//////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//The subfunction--imsmooth
//The function is smoothing the original iamge
void imsmooth(FILE *fp1)
{ FILE *fp2;
float mask[3][3];
float maska[3][3]={1.0/9,1.0/9,1.0/9,1.0/9,1.0/9,1.0/9,1.0/9,1.0/9,1.0/9};
float maskb[3][3]={1.0/16,1.0/8,1.0/16,1.0/8,1.0/4,1.0/8,1.0/16,1.0/8,1.0/16};
char select;
unsigned char img[3][256],transimg[256];
unsigned char firstrow[256]={0},lastrow[256]={0}; /*firstcolumn[256],lastcolumn[256];*/
int i,k,l,m=0,n=0;
int h,g;
float result;
printf("Please choose the mask!\n\n");
printf("Press a:You choose the box mask:\n");
printf(" 1/9 1/9 1/9 \n");
printf(" 1/9 1/9 1/9 \n");
printf(" 1/9 1/9 1/9 \n");
printf("Press b:You choose the gaussian mask:\n");
printf(" 1/16 1/8 1/16 \n");
printf(" 1/8 1/4 1/8 \n");
printf(" 1/16 1/8 1/16 \n");
getchar();
onceagain:
printf("You choose the mask a or b?\n");
select=getchar();
if(select=='a')for(m=0;m<3;m++)
for(n=0;n<3;n++)mask[m][n]=maska[m][n];goto out;
if(select=='b')for(m=0;m<3;m++)
for(n=0;n<3;n++)mask[m][n]=maskb[m][n];goto out;
if(select!='a'&&select!='b')printf("\nVery sorry!\nThere are no mask you need\n");goto onceagain;
out:
if((fp2=fopen("smooth.dat","wb"))==NULL)
{
printf("Cannot open the file");
exit(0);
}
//Save and write the first row
for(i=0;i<256;i++)
{ firstrow[i]=fgetc(fp1);
fputc(firstrow[i],fp2);
}
//Save and write the last row
fseek(fp1,255*256,0);
for(i=0;i<256;i++)
lastrow[i]=fgetc(fp1);
//Process the other rows
for(i=0;i<254;i++)
{ fseek(fp1,i*256,0);
for(h=0;h<3;h++)
for(g=0;g<256;g++)
{img[h][g]=fgetc(fp1);
//printf("%d ",img[h][g]);
}
//Left border
transimg[0]=img[1][0];
//Right border
transimg[255]=img[1][255];
for(k=0;k<254;k++)
{ result=0;
for(m=0;m<3;m++)
for(n=0;n<3;n++)
{result+=img[m][k+n]*mask[m][n];
}
//if(result<0)result=0;
//if(result>255)result=255;}
transimg[k+1]=(int)result;
// printf("%d ",transimg[k+1]);
//printf("%d ",(int)(result));
}
for(l=0;l<256;l++)
{fseek(fp1,256*(i+1),0);
fputc(transimg[l],fp2); /*write data to file2*/
}
}
fseek(fp1,255*256,0); /*mov the pointer*/
for(i=0;i<256;i++) /*write the last row to file 2*/
fputc(lastrow[i],fp2);
fclose(fp2);
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
////The subfunction--middle
//The function is middle-filtering the original image
void middle(FILE *fp1)
{
FILE * fp2;
unsigned char image1[256][256];
unsigned char image2[256][256];
int i,j,k,l,r;
if((fp2=fopen("middle.dat","wb"))==NULL)
{
printf("Cannot open the file\n");
exit(0);
}
for(i=0;i<256;i++)
for(j=0;j<256;j++)
{
image1[i][j]=fgetc(fp1);
image2[i][j]=image1[i][j];
}
int n;
int m;
printf("请输入中值滤波要的阶数(必须为大于1的奇数)\n");
scanf("%d",&n);
printf("请选择是十字型的还是方型的\n");
printf("按a选择是十字型:\n");
printf("按b选择是方型型:\n");
unsigned char temp[100];
/* unsigned char *temp;
temp=new unsigned char[2*n-1];
*/
m=n/2;
// printf("%d ",m);
unsigned char re;
getchar();
switch(getchar())
{case 'a':
for(i=m;i<256-m;i++)
for(j=m;j<256-m;j++)
{ l=0;
for(k=0;k<m;k++)
temp[l++]=image1[i-k-1][j];
for(k=0;k<m;k++)
temp[l++]=image1[i][j-k-1];
for(k=0;k<m;k++)
temp[l++]=image1[i+k+1][j];
for(k=0;k<m;k++)
temp[l++]=image1[i][j+k+1];
temp[l]=image1[i][j];
re=sort(temp,(2*n-1));
image2[i][j]=re;
// printf("%d ",image2[i][j]);
}break;
case 'b':
for(i=m;i<256-m;i++)
for(j=m;j<256-m;j++)
{l=0;
for(k=-1*m;k<=m;k++)
for(r=-1*m;r<=m;r++)
temp[l++]=image1[i+k][j+r];
re=sort(temp,(n*n));
image2[i][j]=re;
// printf("%d ",image2[i][j]);
}
printf("%d ",l);break;
}
for(i=0;i<256;i++)
for(j=0;j<256;j++)
{
fputc(image2[i][j],fp2);
}
fclose(fp2);
}
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//The subfunction--sort
//The function is sorting the array by the way of bubble
unsigned char sort(unsigned char a[],int n)
{
int i;
int flag;
unsigned char result ,t;
do{
flag=0;
for(i=0;i<=n-2;i++)
if(a[i+1]>a[i])
{t=a[i];
a[i]=a[i+1];
a[i+1]=t;
flag=1;
}
}while(flag==1);
result=a[n/2];
return result;
}
//////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////</PRE><!-- CoreMail Version 3.1_snapshot Copyright (c) 2002-2006 www.mailtech.cn --></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -