二维小波分解与重构程序实例
// *********(基于Windows的二维小波分解与重构BC++4。5)**********//
#include <owl/owlpch.h>
#include <owl/applicat.h>
#include <owl/dc.h>
#include <owl/menu.h>
#include <owl/framewin.h>
#include <owl/scroller.h>
#include <owl/opensave.h>
#include <owl/clipview.h>
#include <string.h>
#include <alloc.h>
#include <dir.h>
#include <math.h>
#include "stdio.h"
#include "stdlib.h"
#include "bmpview.h"
#define PI 3.1415926
#define SIZE 256
#define DD 13
float h[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 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 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*********************//
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;
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 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(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_OVERLAPPEDWINDOW,
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&)
{
}
class 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 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 CmAutoClipView();
void CeCopy(TCommandEnabler& ce);
void CePaste(TCommandEnabler& ce);
void CeFit(TCommandEnabler& ce);
void CeAutoClipView(TCommandEnabler& ce);
//*** Image Processing**//
void CmWavelet();
void CmAgent();
//**********************//
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 EvDestroy();
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);
};
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, CeCopy),
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),
//***************************************//
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***********//
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<AgentNum;i++){Agent[i][0]=5*i+1;Agent[i][1]=5*i+1;isAgentAlive[i]=1;}
// MessageBox("ss","ss",MB_OK);
if(pMemDC)
{
for(x=0;x<32;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<AgentNum;i++)
{
if(isAgentAlive[i]==1)
{
x=Agent[i][0];y=Agent[i][1];
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&&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;}
}
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;Agent[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::CmWavelet()
{
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("lenawt.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);
}
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)
{ 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 d