1、
附录一、快速付里哀变换与反变换程序实例
#include
2、/ COMPLEX Sub(COMPLEX c1, COMPLEX c2) { COMPLEX c; c.re=c1.re-c2.re; c.im=c1.im-c2.im; return c; } /*复数乘运算*/ COMPLEX Mul(COMPLEX c1, COMPLEX c2) { COMPLEX c; c.re=c1.re*c2.re-c1.im*c2.im; c.im=c1.re*c2.im+c2.re*c1.im; return c; } /*快速付里哀变换 TD为时域值,FD为频域值,power为2的幂数*/ voi
3、d FFT(COMPLEX * TD, COMPLEX * FD, int power)
{
int count;
int i,j,k,bfsize,p;
double angle;
COMPLEX *W,*X1,*X2,*X;
/*计算付里哀变换点数*/
count=1< 4、COMPLEX)*count);
/*计算加权系数*/
for(i=0;i 5、i++)
{
p=j*bfsize;
X2[i+p]=Add(X1[i+p],X1[i+p+bfsize/2]);
X2[i+p+bfsize/2]=Mul(Sub(X1[i+p],X1[i+p+bfsize/2]),W[i*(1< 6、
FD[j]=X1[p];
}
/*释放存储器*/
free(W);
free(X1);
free(X2);
}
/*快速付里哀反变换,利用快速付里哀变换
FD为频域值,TD为时域值,power为2的幂数*/
void IFFT(COMPLEX *FD, COMPLEX *TD, int power)
{
int i,count;
COMPLEX *x;
/*计算付里哀反变换点数*/
count=1< 7、
/*将频域点写入存储器*/
memcpy(x,FD,sizeof(COMPLEX)*count);
/*求频域点的共轭*/
for(i=0;i 8、用2N点付里哀变换实现快速余弦变换)
/*快速离散余弦变换,利用快速付里哀变换
f为时域值,F为变换域值,power为2的幂数*/
void DCT(double *f, double *F, int power)
{
int i,count;
COMPLEX *X;
double s;
/*计算离散余弦变换点数*/
count=1< 9、2);
/*将时域点写入存储器*/
for(i=0;i 10、散余弦反变换,利用快速付里哀反变换
F为变换域值,f为时域值,power为2的幂数*/
void IDCT(double *F, double *f, int power)
{
int i,count;
COMPLEX *X;
double s;
/*计算离散余弦反变换点数*/
count=1< 11、
for(i=0;i 12、Hadamard变换与反变换程序实例
/*快速沃尔什-哈达玛变换
f为时域值,F为变换域值,power为2的幂数*/
void WALh(double *f, double *W, int power)
{
int count;
int i,j,k,bfsize,p;
double *X1,*X2,*X;
/*计算快速沃尔什变换点数*/
count=1< 13、)*count);
/*将时域点写入存储器*/
memcpy(X1,f,sizeof(double)*count);
/*蝶形运算*/
for(k=0;k 14、
X=X1;
X1=X2;
X2=X;
}
/*调整系数*/
for(i=0;i 15、ALh(W,f,power);
/*调整系数*/
for(i=0;i 16、h>
#include 17、0196637,-0.0482603,-0.0485391,
0.292562,0.564406,0.292562,-0.0485391,-0.0482602,-0.0196637,
0.00569794,-0.0033276};
float g[DD]={0.00332761,0.00569794,-0.0196637,-0.0482603,0.0485391,
0.292562,-0.564406,0.292562,0.0485391,-0.0482602,0.0196637,
0.00569794,0.0033276};
float 18、hi[DD],gi[DD];
int wavelet_direction=1;
int a(int x,int xsize);
//Threshold//
int s(float x);
//Set Inverse Filter Coefficients//
void coef();
//********Wavelet Transform************************//
void wt(int xs,int ys,long xsize,long ysize);
//*****Inverse Wavelet Transform********* 19、//
void iwt(int xs,int ys,long xsize,long ysize);
float *img[SIZE],*imgx[SIZE],*imgy[SIZE];
//****for evolution agents*****//
int AgentNum,ActiveAgent;
int SolutionX[1000],SolutionY[1000];
int SearchNum,AgentLife;
int Agent[1000][2];
int isAgentAlive[1000];
int SearchTime;
20、unsigned short Image[32][32];
#define MAXAPPNAME 20
static const char AppName[] = "Image Processing";
//
// TBmpViewWindow, a Bitmap displaying window derived from TClipboardViewer to
// facilitate receiving of clipboard change notifications. Could mix it in if
// an additional base was 21、 desired.
//
class TSubWindow : public TFrameWindow {
public:
TSubWindow(TWindow* parent);
~TSubWindow();
protected:
void EvSize(UINT sizeType, TSize& size)
{Invalidate(); TFrameWindow::EvSize(sizeType, size);}
void Paint(TDC& dc, bool, TRect&);
DECLARE_RESPONSE_TABLE( 22、TSubWindow);
};
DEFINE_RESPONSE_TABLE1(TSubWindow, TFrameWindow)
EV_WM_SIZE,
END_RESPONSE_TABLE;
// pointers to different child windows.
//
TWindow* SubWinPtr = 0;
TSubWindow::TSubWindow(TWindow* parent)
: TFrameWindow(parent)
{
Attr.Style |= WS_VISIBLE | WS_POPUP | WS_OVERLAP 23、PEDWINDOW,
Attr.X = 100;
Attr.Y = 100;
Attr.W = 300;
Attr.H = 400;
}
//
// Destroy window. SubWinPtr[Type] is set to 0 to indicate that the window
// has be closed.
//
TSubWindow::~TSubWindow()
{
SubWinPtr = 0;
}
void
TSubWindow::Paint(TDC& dc, bool, TRect&)
{
}
cl 24、ass TBmpViewWindow : virtual public TWindow, public TClipboardViewer {
public:
char FileName[MAXPATH];
TDib* Dib;
TBitmap* Bitmap;
TMemoryDC * pMemDC;
TPalette* Palette;
TBrush* BkgndBrush;
long Rop;
int PixelWidth;
int PixelHeight;
WORD 25、 Colors;
bool Fit;
bool AutoClipView;
// unsigned char far *f[256],*g[256];
TBmpViewWindow();
~TBmpViewWindow();
void ShowSubWindow(TWindow* parent);
protected:
void CmFileOpen();
void CmCopy();
void CmPaste();
void CmFit();
void 26、 CmAutoClipView();
void CeCopy(TCommandEnabler& ce);
void CePaste(TCommandEnabler& ce);
void CeFit(TCommandEnabler& ce);
void CeAutoClipView(TCommandEnabler& ce);
//*** Image Processing**//
void CmWavelet();
void CmAgent();
//*********** 27、//
void Paint(TDC&, bool erase, TRect&);
void EvSize(UINT sizeType, TSize&);
void EvPaletteChanged(HWND hWndPalChg);
bool EvQueryNewPalette();
void EvSetFocus(HWND); // should use above when working
void EvDrawClipboard();
void EvD 28、estroy();
bool UpdatePalette(bool alwaysRepaint);
void AdjustScroller();
void SetCaption(const char*);
void SetupFromDib(TDib* dib);
bool LoadBitmapFile(const char*);
bool LoadBitmapResource(WORD ResId);
DECLARE_RESPONSE_TABLE(TBmpViewWindow);
29、};
DEFINE_RESPONSE_TABLE2(TBmpViewWindow, TClipboardViewer, TWindow)
EV_COMMAND(CM_FILEOPEN, CmFileOpen),
EV_COMMAND(CM_EDITCOPY, CmCopy),
EV_COMMAND(CM_EDITPASTE, CmPaste),
EV_COMMAND(CM_FIT, CmFit),
EV_COMMAND(CM_AUTOCLIPVIEW, CmAutoClipView),
EV_COMMAND_ENABLE(CM_EDITCOPY, Ce 30、Copy),
EV_COMMAND_ENABLE(CM_EDITPASTE, CePaste),
EV_COMMAND_ENABLE(CM_FIT, CeFit),
EV_COMMAND_ENABLE(CM_AUTOCLIPVIEW, CeAutoClipView),
//*****Image Processing***************//
EV_COMMAND(CM_WAVELET,CmWavelet),
EV_COMMAND(CM_AGENT,CmAgent),
//************************************* 31、//
EV_WM_SIZE,
EV_WM_PALETTECHANGED,
EV_WM_QUERYNEWPALETTE,
EV_WM_SETFOCUS,
EV_WM_DRAWCLIPBOARD,
EV_WM_DESTROY,
END_RESPONSE_TABLE;
//****agent search******//
//****小波变换菜单Wavelet*******//
//****原图象存在lena.bmp***//
//****运行后,分解图象存在lenawt.bmp******//
//****重建图象存在lenaiwt.bmp*** 32、//
void TBmpViewWindow::CmAgent()
{
char ss[5];
int i,j,k,x,y;
AgentNum=5;ActiveAgent=5;SearchNum=0;
AgentLife=2;
SearchTime=0;
for(i=0;i 33、x++)
for(y=0;y<32;y++)
Image[x][y]=(unsigned short)pMemDC->GetPixel(x,y);
// img[i][j]=f[i][j];
while(1){
MessageBox("ss","ss",MB_OK);
if(ActiveAgent<=0)break;
for( i=0;i 34、
if(Image[x][y]== 0)
{
SolutionX[SearchNum]=x;SolutionY[SearchNum]=y;
Image[x][y]=255;//***marking this point
pMemDC->SetPixel(x,y,
(unsigned char)Image[x][y]);
AdjustScroller();
SearchNum+=1;
//**judge direction**//
if((x+1)<32& 35、Image[x+1][y]==0){x=x+1;Agent[i][0]=x;}
else if((x-1)>=0 && Image[x-1][y]==0) {x=x-1;Agent[i][0]=x;}
else if((y-1)>=0&&Image[x][y-1]==0){y=y-1;Agent[i][1]=y;}
else if((y+1)<32&&Image[x][y+1]==0){y=y+1;Agent[i][1]=y;}
else {isAgentAlive[i]=0; ActiveAgent-=1;}
36、 }
else
{ //**judge direction**//
if((x+1)<32&&Image[x+1][y]==0){x=x+1;Agent[i][0]=x;}
else if((x-1)>=0 && Image[x-1][y]==0) {x=x-1;Agent[i][0]=x;}
else if((y-1)>=0&&Image[x][y-1]==0){y=y-1;Agent[i][1]=y;}
else if((y+1)<32&&Image[x][y+1]==0){y=y+1;Age 37、nt[i][1]=y;}
else {isAgentAlive[i]=0; ActiveAgent-=1;}
}
SearchTime++;
}//**end if isAgentAlive==1
}//**end for
}//**end while
sprintf(ss,"%d",SearchTime);
MessageBox(ss,"Total Search times",MB_OK);
}
}
void
TBmpViewWindow::Cm 38、Wavelet()
{
unsigned i,j,k;int xs,ys;
unsigned char buf[SIZE+1],buf1[1078];
FILE *inputfile,*mfile,*outputfile;
inputfile=fopen("lena.bmp","rb");
if(!inputfile)
{ MessageBox("Cannot open file lena.raw", GetApplication()->GetName(), MB_OK);
CloseWindow(0);
}
mfile=fopen("l 39、enawt.bmp","wb");
if(!mfile)
{ MessageBox("Cannot open file lenawt.raw", GetApplication()->GetName(), MB_OK);
CloseWindow(0);
}
outputfile=fopen("lenaiwt.bmp","wb");
if(!outputfile)
{ MessageBox("Cannot open file lenawt.raw", GetApplication()->GetName(), MB_OK);
CloseWindow(0 40、);
}
for(i=0;i<=SIZE-1;i++)
{
if((imgx[i]=(float far*)farmalloc(SIZE*sizeof(float)))==NULL)
{ MessageBox("Cannot malloc memory for displaying bitmap file", GetApplication()->GetName(), MB_OK);
CloseWindow(0);
}
if((imgy[i]=(float far*)farmalloc(SIZE*sizeof(float)))==NULL)
{ 41、 MessageBox("Cannot malloc memory for displaying bitmap file", GetApplication()->GetName(), MB_OK);
CloseWindow(0);
}
if((img[i]=(float far*)farmalloc(SIZE*sizeof(float)))==NULL)
{ MessageBox("Cannot malloc memory for displaying bitmap file", GetApplication()->GetName(), MB_OK);
Close 42、Window(0);
}
}
//read input file
fread(buf1,1,1078,inputfile);
for(i=0;i 43、SIZE);
xs=xs/2;ys=ys/2;
wt(xs,ys,SIZE,SIZE);
xs=xs/2;ys=ys/2;
wt(xs,ys,SIZE,SIZE);
//*****write the wt result file*****//
fwrite(buf1,1,1078,mfile);
for(i=0;i 44、[j]-128.0;
}
fwrite(buf,1,SIZE,mfile);
}
fclose(mfile);
//****start inverse wt....***//
iwt(xs,ys,SIZE,SIZE);
xs=2*xs;ys=2*ys;
iwt(xs,ys,SIZE,SIZE);
xs=2*xs;ys=2*ys;
iwt(xs,ys,SIZE,SIZE);
fwrite(buf1,1,1078,outputfile);
for(i=0;i 45、j++)
{
buf[j]=(unsigned char)(s(img[i][j])+128);
}
fwrite(buf,1,SIZE,outputfile);
}
fclose(outputfile);
/* LoadBitmapFile("lenawt.bmp");
SetCaption(strlwr("lenawt.bmp"));
LoadBitmapFile("lenaiwt.bmp");
SetCaption(strlwr("lenaiwt.bmp"));
*/
for(i=0;i<=SIZE-1;i 46、)
{
farfree(imgy[i]);
farfree(imgx[i]);
farfree(img[i]);
}
if(wavelet_direction==1)wavelet_direction=0;
else wavelet_direction=1;
}
//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 s(float x)
{
if( 47、x>127)return(127);
if(x<-128)return(-128);
return(x);
}
//Set Inverse Filter Coefficients//
void coef()
{
int i;
for(i=0;i 48、k,n;
float temp1,temp2;
float bufferx[SIZE],buffery[SIZE];
for(n=0;n 49、n][a(i+j,xs)];
bufferx[i/2]=temp1;
bufferx[i/2+xs/2]=temp2;
}
for(k=0;k 50、DD-1)/2]*img[a(i+j,ys)][n];
for(j=-(DD-1)/2+1;j<=(DD-1)/2+1;j++)
temp2=temp2+g[j+(DD-1)/2-1]*img[a(i+j,ys)][n];
buffery[i/2]=temp1;
buffery[i/2+ys/2]=temp2;
}
for(k=0;k
©2010-2025 宁波自信网络信息技术有限公司 版权所有
客服电话:4009-655-100 投诉/维权电话:18658249818