⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 waveletdoc.cpp

📁 用小波变换对图象进行灰度化处理,进一步来实现图象的预处理过程
💻 CPP
字号:
// WaveletDoc.cpp : implementation of the CWaveletDoc class
//

#include "stdafx.h"
#include "Wavelet.h"

#include "WaveletDoc.h"
#include "BmpHeader.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
///////////////
#include "conio.h"
#include "string.h"
#include "dos.h"
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include "malloc.h"
#include "time.h"
#include "process.h"
#include "float.h"

#define DD 23
const double h[DD]={-0.002,-0.003,0.006,0.006,-0.013,-0.012,0.030,0.023,-0.078,-0.035,0.307,0.542,0.307,-0.035,-0.078,0.023,0.030,-0.012,-0.013,0.006,0.006,-0.003,-0.002};
const double g[DD]={0.002,-0.003,-0.006,0.006,0.013,-0.012,-0.030,0.023,0.078,-0.035,-0.307,0.542,-0.307,-0.035,0.078,0.023,-0.030,-0.012,0.013,0.006,-0.006,-0.003,0.002};
double hi[DD],gi[DD]; 
///////////////
/////////////////////////////////////////////////////////////////////////////
// CWaveletDoc

IMPLEMENT_DYNCREATE(CWaveletDoc, CDocument)

BEGIN_MESSAGE_MAP(CWaveletDoc, CDocument)
	//{{AFX_MSG_MAP(CWaveletDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CWaveletDoc construction/destruction

CWaveletDoc::CWaveletDoc()
{
	// TODO: add one-time construction code here
	ReadOk=FALSE;

}

CWaveletDoc::~CWaveletDoc()
{
	
}

BOOL CWaveletDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CWaveletDoc serialization

void CWaveletDoc::Serialize(CArchive& ar)
{
	CString s;
	int i,j;
	//double fBmp[256][256];
	Byte** ppbImage=(Byte**)malloc(256*sizeof(Byte*));
    double** ppfImage=(double**)malloc(256*sizeof(double*));
    for(i=0;i<256;i++)
	{
		ppfImage[i]=(double*)malloc(256*sizeof(double));
		ppbImage[i]=(Byte*)malloc(256*sizeof(Byte));
	}

	int iWidth=256;
	int iHeight=256;
    int J=3;    //level numbers
//    int iHeightNew;
	////////
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
	    ar.Read(&(MyBmp.Header),sizeof(BmpHeader));
		//s.Format("%d",CmpHeader());
        //s.Format("0x%x",MyBmp.Header.HeaderSize);
		//AfxMessageBox(s);
		if(CmpHeader()==0)
		{
			ar.Read(&(MyBmp.Palettes),sizeof(DWORD)*256);
            ar.Read(&(MyBmp.Pix),sizeof(unsigned char)*256*256);
			ReadOk=TRUE;

            for(i=0;i<256;i++)
				for(j=0;j<256;j++)
				{	
					ppfImage[i][j]=(double)MyBmp.Pix[i][j];
				}
 
             for(i=0;i<256;i++)
			 for(j=0;j<256;j++)
			 {
		     	 ppbImage[i][j]=MyBmp.Pix[i][j];
			 }
/////////////////////////
        GetMultiWT(ppbImage,iWidth,iHeight,ppfImage,J);
		for(i=0;i<256;i++)
			for(j=0;j<256;j++)
			{
		     	 MyfBmp[i][j]=(Byte)ppfImage[i][j];
			}
        coef();
        GetMultiIWT(iWidth,iHeight,ppfImage,J);
        fImageTObImage(ppfImage,iWidth,iHeight,ppbImage);
		for(i=0;i<256;i++)
			 for(j=0;j<256;j++)
			 {
		     	 MyBmp.Pix[i][j]=ppbImage[i][j];
				 
			 }
	//OutPicData (iWidth,iHeight,ppbImage,EncodeImage);
//    OutData(iWidth,iHeight,ppbImage,EncodeImage);
//    GetFree(iWidth,(void**)ppbImage);
  //  GetFree(iWidth,(void**)ppfImage);
/////////////////////////
		}
		else
		{
			AfxMessageBox("It's not BMP Gray file with size 256*256!");
			return;
		}
	}
	free(ppbImage);
	free(ppfImage);
}

/////////////////////////////////////////////////////////////////////////////
// CWaveletDoc diagnostics

#ifdef _DEBUG
void CWaveletDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CWaveletDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CWaveletDoc commands

int CWaveletDoc::CmpHeader()
{
BmpHeader* p=&(MyBmp.Header);
if(p->Identi[0]!='B')  return 1;
if(p->Identi[1]!='M')  return 1;
if(p->DataOffset!=0x436) return 2;
if(p->HeaderSize!=40) return 3;
if(p->Width!=256) return 4;
if(p->Height!=256) return 5;
if(p->BitsPerPix!=8) return 6;
if(p->Compression!=0) return 7;
if(p->Colors!=0&&p->Colors!=256) return 8;
return 0;
}


void CWaveletDoc::GetMultiWT(Byte **ppbImage, int iWidth, int iHeight, double** ppfImage, int J)
{
	int iRow,iCol;
//    int i,j;
	int k;
    wt(0,0,iWidth,iHeight,iWidth,iHeight,ppbImage,ppfImage);
    iRow=iWidth; 
	iCol=iHeight;
    for(k=2;k<=J;k++)
     {
       iRow/=2; 
	   iCol/=2;
       fwt(0,0,iRow,iCol,iRow,iCol,ppfImage,ppfImage);
     } 

}

void CWaveletDoc::wt(int startx, int starty, int xs, int ys, long xsize, long ysize, Byte** image, double** ppfImage)
{
	int i,j,k,n;
    double temp1,temp2;
    double* bufferx;
    double* buffery;
    if((bufferx=(double*)malloc(xs*sizeof(double)))==(double*)NULL)
	{
	     AfxMessageBox("No memory!");
         return;
    }
    if((buffery=(double*)malloc(ys*sizeof(double)))==(double*)NULL)
    {               
	     AfxMessageBox("No memory!");
         return;
	}
//to filter in the horizonal direction
   for(n=starty;n<ys+starty;n++)
    {
         for(i=startx;i<xs+startx;i+=2)
		 {
              temp1=0;
              temp2=0;
              for(j=-(DD-1)/2;j<=(DD-1)/2;j++)
              {
                  temp1=temp1+h[j+(DD-1)/2-1]*(image[n][a(i+j,xs)]);
			  }
              for(j=-(DD-1)/2+1;j<=(DD-1)/2+1;j++)
			  {
				  temp2=temp2+g[j+(DD-1)/2-1]*(image[n][a(i+j,xs)]);
			  }
              *(bufferx+i/2)=temp1;
              *(bufferx+i/2+xs/2)=temp2;
		 }  //i
        for(k=startx;k<xs+startx;k++)
		{ 
			 ppfImage[n][k]=(*(bufferx+k));
		}
                 //printf("%d",n)
	}      //n
         //printf("ok2\n")
		 //to filter in the vertical direction
    for(n=startx;n<xs+startx;n++)
    {
         for(i=starty;i<ys+starty;i+=2)
		 {
              temp1=0;
              temp2=0;
              for(j=-(DD-1/2);j<=(DD-1)/2;j++)
              {
                 temp1=temp1+h[j+(DD-1)/2]*(ppfImage[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]*(ppfImage[a(i+j,ys)][n]);
			   }
              *(buffery+i/2) =temp1;
              *(buffery+i/2+ys/2)=temp2;
         }  //i
         for(k=starty;k<ys+starty;k++)
		 { 
			 ppfImage[k][n]=(*(buffery+k));
		 }
	}  //n
	
         free(bufferx);
         free(buffery);

}

void CWaveletDoc::fwt(int startx, int starty, int xs, int ys, long xsize, long ysize, double** image, double** ppfImage)
{
	
          int i,j,k,n;
          double temp1,temp2;
          double* bufferx;
          double* buffery;
          if((bufferx=(double*)malloc(xs*sizeof(double)))==(double*)NULL)
		  {                  
			   AfxMessageBox("No memory!");
               return;
		  }
          if((buffery=(double*)malloc(ys*sizeof(double)))==(double*)NULL)
          {               
			   AfxMessageBox("No memory!");
               return;
		  }
		  //to filter in the horizonal direction
          for(n=starty;n<ys+starty;n++)
		  {
                for(i=startx;i<xs+startx;i+=2)
                 {
                         temp1=0;
                         temp2=0;
                         for(j=-(DD-1)/2;j<=(DD-1)/2;j++)
						 {
                               temp1=temp1+h[j+(DD-1)/2]*(image[n][a(i+j,xs)]);
						 }
                         for(j=-(DD-1)/2+1;j<=(DD-1)/2+1;j++)
						 {
							 temp2=temp2+g[j+(DD-1)/2-1]*(image[n][a(i+j,xs)]);
						 }
                         *(bufferx+i/2)=temp1;
                         *(bufferx+i/2+xs/2)=temp2;
                }   //i
                for(k=startx;k<xs+startx;k++)
				   {
					   ppfImage[n][k]=(*(bufferx+k));
				   }
                            //printf(" %d" ,n);
		  } //n
              //printf(" ok2\n") 
		  //to filter in the vertical direction
          for(n=startx;n<xs+startx;n++)
             {
                   for (i=starty;i<ys+starty;i+=2)
				   {  
					   temp1=0;
                       temp2=0;
                         for(j=-(DD-1)/2;j<=(DD-1)/2;j++)
						 {
                           temp1=temp1+h[j+(DD-1)/2]*(ppfImage[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]*(ppfImage[a(i+j,ys)][n]);
						 }
                           *(buffery+i/2)=temp1;
                           *(buffery+i/2+ys/2)=temp2;
				   }  //i
                     for(k=starty;k<ys+starty;k++)
					 {
						 ppfImage[k][n]=(*(buffery+k));
					 }
			  }  //n
                    free(bufferx);
                    free(buffery);

}

int CWaveletDoc::a(int x, int xsize)
{
	
	if(x<0) x=-x;
    if(x>=xsize) x=xsize*2-x-2;
    return(x);

}

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

}

void CWaveletDoc::fImageTObImage(double **ppfImage, int row, int col, Byte **ppbImage)
{
	
    int i,j;
    double fTemp;
    for (i=0;i<row;i++)
        for (j=0;j<col;j++)
        {
         fTemp=ppfImage[i][j];
         if(fTemp<0)    fTemp=0;
         if(fTemp>255)  fTemp=255;
         ppbImage[i][j]=(int)fTemp;
         }

}

void CWaveletDoc::GetMultiIWT(int iWidth, int iHeight, double **ppfImage, int J)
{
	int iRow,iCol,k;
    //int i,j;
    iRow=(int)(iWidth/pow(2,J)); 
	iCol=(int)(iHeight/pow(2,J));
    for(k=1;k<=J;k++)
      {
       iRow*=2; 
	   iCol*=2;
       iwt(iRow,iCol,iRow,iCol,ppfImage);
     } 

}

void CWaveletDoc::iwt(int xs, int ys, long xsize, long ysize, double **image)
{
   	 int i,j,k,n;
     double temp1,temp2;
     double* buffer1;
     double* buffer2;
     if((buffer1=(double*)malloc(ysize*sizeof(double)))==(double*)NULL)
     {              
			printf("1: no memery\n");
            exit(1);
     }
     if((buffer2=(double*)malloc(ysize*sizeof(double)))==(double*)NULL)
     {              
		printf("2: no memery\n");
        exit(1);
	 }
	//to filter in the vertical direction
     for(n=0;n<xs;n++)
     {
         for(k=0;k<(ys/2);k++)
		{
                  *(buffer1+k*2)=image[k][n];
                  *(buffer1+k*2+1)=0;
		 }
         for(i=0;i<ys;i++)
		{
				 temp1=0;
                 for(j=-(DD-1)/2;j<=(DD-1)/2;j++)
				 {
					 temp1=temp1+hi[j+(DD-1)/2]*(*(buffer1+a(i+j,ys)));
				 }
                 *(buffer2+i)=temp1;
		}
        for(k=0;k<(ys/2);k++)
		{
                 *(buffer1+k*2)=image[k+ys/2][n];
                 *(buffer1+k*2+1)=0;
		}
        for(i=0;i<ys;i++)
		{
                 temp1=0;
                 for(j=-(DD-1)/2-1;j<=(DD-1)/2-1;j++)
				 {
					 temp1=temp1+gi[j+(DD-1)/2+1]*(*(buffer1+a(i+j,ys)));
				 }
                 temp2=temp1+(*(buffer2+i));
                 *(buffer2+i)=temp2;
		}
        for(k=0;k<ys;k++)
		{
			image[k][n]=(*(buffer2+k));   //* 2;
		}
	}  //n
    free(buffer1);
    free(buffer2); 
		  //realloc work place
    if((buffer1=(double*)malloc(xsize*sizeof(double)))==(double*)NULL)
	{
               printf("1: no memery\n");
               exit(1);
	}
    if((buffer2=(double*)malloc(xsize*sizeof(double)))==(double*)NULL)
	{
                printf("2: no memery\n");
                exit(1); 
	}
		   	//to filter in the horizonal direction
    for(n=0;n<ys;n++)
	{
		for(k=0;k<(xs/2);k++)
        {
			*(buffer1+k*2)=image[n][k];
            *(buffer1+k*2+1)=0;
		}

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

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -