软件编程版 #621452 层

一个小波变换的c语言程序

楼主 · 1 楼
前段时间在网上看到一个小波变换的c程序,现在再找它,竟然找不到了。作者只留下这个程序,也没说运行应实现什么效果。放这里,有兴趣的研究一下。
1)我想像中的分解(先不说重构)应是这么个状况:
对一个图像文件lenna进行处理,将图像分解过程所得数据存于另一文件,应该也能作为一幅图片打开。
而运行后(或稍作修改,只取其分解部分),得到的文件只是简单的一系列数,没有lenna。
2)程序中给的h[DD]是怎么算得的。

懂得比较多的大侠们,谁来解答我上面两点疑惑?或者大家来说说各自对小波图像分解重构的理解。。。

程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define DD 8
#define M 256
#define N 256
float h[DD]={0.1
629,0.50547,0.44
61,-0.01979,-0.13225,0.02181,0.023252,-0.00749};
float g[DD]={0.00749,0.023252,-0.02181,-0.13225,0.01979,0.44
61,-0.50547,0.1
629};
float hi[DD];
float gi[DD];

/*Edge Process*/
int a(int x, int xsize)
{
if(x<0)
x=-x;
if(x>=xsize)
x=xsize*2-x-2;
return(x);

}

/*Threshold*/

int TH(float x)
{
if(x>127)return(127);
if(x<-128)return(-128);
return (int)x;
}


void coef()
{
int i;
for(i=0;i<DD;i++)
{
hi[i]=h[DD-1-i];
gi[i]=g[DD-1-i];
}
}


/*-------------Wavelet Transform---------*/

void WT(int xs,int ys,int xsize,int ysize,float *image)
{
int i,j,k;
int n;
float temp1,temp2;
float *bufferx, *buffery;
if((bufferx=(float *)malloc(ys*sizeof(float)))==(float *)NULL)
{
printf("buffer error \n");
exit(1);
}
if((buffery=(float *)malloc(ys*sizeof(float)))==(float *)NULL)
{
printf("buffer error \n");
exit(1);
}
for(n=0;n<ys;n++)
{
for(i=0;i<xs;i+=2)
{
temp1=0;
temp2=0;
for(j=0;j<=DD-1;j++)
temp1=temp1+h[j]*(*(image+n*xsize+a(i+j,xs)));
for(j=-DD+2;j<=1;j++)
temp2=temp2+g[j+DD-2]*(*(image+n*xsize+a(i+j,xs)));

*(bufferx+i/2)=temp1;
*(bufferx+i/2+xs/2)=temp2;
}

for(k=0;k<xs;k++)
*(image+n*xsize+k)=(*(bufferx+k));
}

/*----------------Filter in the vertical------------*/
printf("horizontal has over\n");
for(n=0;n<xs;n++)
{
for(i=0;i<ys;i+=2)
{
temp1=0;
temp2=0;
for(j=0;j<=DD-1;j++)
temp1=temp1+h[j]*(*(image+a(i+j,ys)*xsize+n));
for(j=-DD+2;j<=1;j++)
temp2=temp2+g[j+DD-2]*(*(image+a(i+j,ys)*xsize+n));

*(buffery+i/2)=temp1;
*(buffery+i/2+ys/2)=temp2;
}

for(k=0;k<ys;k++)
*(image+k*xsize+n)=(*(buffery+k));
}

free(bufferx);
free(buffery);
}

/*-----------inverse Wavelaet Transform--------------*/

void IWT(int xs,int ys,int xsize,int ysize,float *image)
{
int i,j,k;
int n;
float temp1,temp2;
float *buffer1, *buffer2;
if((buffer1=(float *)malloc(ysize*sizeof(float)))==(float *)NULL)
{
printf("buffer error \n");
exit(1);
}
if((buffer2=(float *)malloc(ysize*sizeof(float)))==(float *)NULL)
{
printf("buffer error \n");
exit(1);
}

/*-----------Filter in the verticl diretion--------------*/
for(n=0;n<xs;n++)
{
for(k=0;k<(ys/2);k++)
{
*(buffer1+k*2)=*(image+k*xsize+n);
*(buffer1+k*2+1)=0;
}
for(i=0;i<ys;i++)
{
temp1=0;
for(j=-(DD-1);j<=0;j++)
temp1=temp1+hi[j+DD-1]*(*(buffer1+a(i+j,ys)));
*(buffer2+i)=temp1;
}
for(k=0;k<(ys/2);k++)
{
*(buffer1+k*2)=*(image+(k+ys/2)*xsize+n);
*(buffer1+k*2+1)=0;
}
for(i=0;i<ys;i++)
{
temp1=0;
for(j=-1;j<=DD-2;j++)
temp1=temp1+gi[j+1]*(*(buffer1+a(i+j,ys)));

temp2=temp1+(*(buffer2+i));
*(buffer2+i)=temp2;
}
for(k=0;k<ys;k++)
*(image+k*xsize+n)=(*(buffer2+k));
}
free(buffer1);
free(buffer2);

/*-----------Filter in the horizontal diretion--------------*/

if((buffer1=(float *)malloc(xsize*sizeof(float)))==(float *)NULL)
{
printf("buffer error \n");
exit(1);
}
if((buffer2=(float *)malloc(xsize*sizeof(float)))==(float *)NULL)
{
printf("buffer error \n");
exit(1);
}

for(n=0;n<ys;n++)
{
for(k=0;k<(xs/2);k++)
{
*(buffer1+k*2)=*(image+n*xsize+k);
*(buffer1+k*2+1)=0;
}
for(i=0;i<xs;i++)
{
temp1=0;
for(j=-(DD-1);j<=0;j++)
temp1=temp1+hi[j+DD-1]*(*(buffer1+a(i+j,xs)));
*(buffer2+i)=temp1;
}
for(k=0;k<(xs/2);k++)
{
*(buffer1+k*2)=*(image+n*xsize+k+xs/2);
*(buffer1+k*2+1)=0;
}
for(i=0;i<xs;i++)
{
temp1=0;
for(j=-1;j<=DD-2;j++)
temp1=temp1+gi[j+1]*(*(buffer1+a(i+j,xs)));

temp2=temp1+*(buffer2+i);
(*(buffer2+i))=temp2;
}
for(k=0;k<xs;k++)
(*(image+n*xsize+k))=(*(buffer2+k))*4;
}
free(buffer1);
free(buffer2);

}

void main()
{
int xs,ys,i,j;
int inVal;
FILE *inputfile,*outputfile,*mfile;
float *image;
int row=M,col=N;

printf("input medium file name:\n");
if((mfile=fopen("level-1.txt","wb"))==NULL)
{
printf("cant open med.dat\n");
exit(1);
}
inputfile=fopen("lenna.txt","rb");
if(inputfile==NULL)
{
printf("input file error\n");
exit(3);
}
if((outputfile=fopen("output.txt","wb"))==NULL)
{
printf("output file error\n");
exit(1);
}
if((image=(float *)malloc(row*col*sizeof(float)))==(float *)NULL)
{
printf("memory error\n");
exit(1);
}

for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
fscanf(inputfile,"%d\n",&inVal);
*(image+i*col+j)=(float)inVal-128;
}
}



xs=col;
ys=row;
coef();
WT(xs,ys,col,row,image);
printf("1 level wt has finished xs,ys%d,%d\n",xs,ys);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
inVal=128+TH(*(image+i*col+j));
fprintf(mfile,"%d\n",inVal);
}
}
fclose(mfile);


if((mfile=fopen("level-2.txt","wb"))==NULL)
{
printf("cant open med.dat\n");
exit(1);
}
xs=xs/2;
ys=ys/2;
WT(xs,ys,col,row,image);
printf("2 level wt has finished xs,ys%d,%d\n",xs,ys);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
inVal=128+TH(*(image+i*col+j));
fprintf(mfile,"%d\n",inVal);
}
}
fclose(mfile);

if((mfile=fopen("level-3.txt","wb"))==NULL)
{
printf("cant open med.dat\n");
exit(1);
}

xs=xs/2;
ys=ys/2;
WT(xs,ys,col,row,image);
printf("3 level wt has finished xs,ys%d,%d\n",xs,ys);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
inVal=128+TH(*(image+i*col+j));
fprintf(mfile,"%d\n",inVal);
}
}
fclose(mfile);

IWT(xs,ys,col,row,image);
printf("iwt 3 finished ,xs=%d,ys=%d\n",xs,ys);
xs=xs*2;
ys=ys*2;
IWT(xs,ys,col,row,image);
printf("iwt 2 finished ,xs=%d,ys=%d\n",xs,ys);
xs=xs*2;
ys=ys*2;
IWT(xs,ys,col,row,image);
printf("IWT 1 finished ,xs=%d,ys=%d\n",xs,ys);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
inVal=128+TH(*(image+i*col+j));
fprintf(outputfile,"%d\n",inVal);
}
}
fclose(inputfile);
fclose(outputfile);
free(image);
}
2 楼
第一个回帖