小波变换源码

wavelet.h

#if !defined(WAVELET_INCLUDE_)
#define WAVELET_INCLUDE_

class Wavelet 
{
public:
 Wavelet();
 virtual ~Wavelet();
 
public:
 bool InitDwt(int nWidth,int nHeight,int nByte,unsigned char * pImgdata);
 bool InitDwt(int nWidth,int nHeight,int nByte,int * pIntdata,int nCurDepth);
 bool WaveletDwt(int Inv);
 
 void SetSupp(int nSupp);
 void SetCurrentDepth(int nCurDepth);
 int  GetCurrentDepth();
 
 unsigned char * GetImgData(bool bOptimize = true);
 int     * GetIntData(bool bOptimize = true);
 
private:
 int  m_nWidth,m_nHeight,m_nByte;
 int  m_nCurDepth,m_nSupp;
 bool m_bInited;

 float  * m_pDwtBuf;
 int   * m_pIntBuf;
 unsigned char * m_pImgBuf;

protected:
 bool  wavelet_dwt1d (float *pfBuf, int CurLevel, int Inv=0, int Step=1);
 bool  wavelet_dwt2d(int Inv,int nStep);
 inline int    Log2 (int n);
 inline char    FloatToChar(float f);
 inline unsigned char FloatToByte(float f);
};

#endif // !defined(WAVELET_INCLUDE_)

 

#include "wavelet.h"
#include <math.h>

const float hCoef[10][20] =
{
 { .707106781187f,  .707106781187f},

 { .482962913145f,  .836516303738f,  .224143868042f, -.129409522551f },

 { .332670552950f,  .806891509311f,  .459877502118f, -.135011020010f, -.085441273882f,  .035226291882f },

 { .230377813309f,  .714846570553f,  .630880767930f, -.027983769417f,
  -.187034811719f,  .030841381836f,  .032883011667f, -.010597401785f },

 { .160102397974f,  .603829269797f,  .724308528438f,  .138428145901f, -.242294887066f,
  -.032244869585f,  .077571493840f, -.006241490213f, -.012580751999f,  .003335725285f },

 { .111540743350f,  .494623890398f,  .751133908021f,  .315250351709f, -.226264693965f,
  -.129766867567f,  .097501605587f,  .027522865530f, -.031582039318f,  .000553842201f,
   .004777257511f, -.001077301085f },

 { .077852054085f,  .396539319482f,  .729132090846f,  .469782287405f, -.143906003929f,
  -.224036184994f,  .071309219267f,  .080612609151f, -.038029936935f, -.016574541631f,
   .012550998556f,  .000429577973f, -.001801640704f,  .000353713800f },

 { .054415842243f,  .312871590914f,  .675630736297f,  .585354683654f, -.015829105256f,
  -.284015542962f,  .000472484574f,  .128747426620f, -.017369301002f, -.044088253931f,
   .013981027917f,  .008746094047f, -.004870352993f, -.000391740373f,  .000675449406f,
  -.000117476784f },

 { .038077947364f,  .243834674613f,  .604823123690f,  .657288078051f,  .133197385825f,
  -.293273783279f, -.096840783223f,  .148540749338f,  .030725681479f, -.067632829061f,
   .000250947115f,  .022361662124f, -.004723204758f, -.004281503682f,  .001847646883f,
   .000230385764f, -.000251963189f,  .000039347320f },

 { .026670057901f,  .188176800078f,  .527201188932f,  .688459039454f,  .281172343661f,
  -.249846424327f, -.195946274377f,  .127369340336f,  .093057364604f, -.071394147166f,
  -.029457536822f,  .033212674059f,  .003606553567f, -.010733175483f,  .001395351747f,
   .001992405295f, -.000685856695f, -.000116466855f,  .000093588670f, -.000013264203f }
};


Wavelet::Wavelet()
{
 m_nSupp  = 1;
 m_nCurDepth = 0;
 m_nByte  = 1;
 m_bInited = false;
 m_pDwtBuf = 0;
 m_pIntBuf = 0;
 m_pImgBuf = 0;
}

Wavelet::~Wavelet()
{
 if ( m_pDwtBuf )
 {
  delete m_pDwtBuf;
  m_pDwtBuf = 0;
 }
 if ( m_pIntBuf )
 {
  delete m_pIntBuf;
  m_pIntBuf = 0;
 }
 if ( m_pImgBuf )
 {
  delete m_pImgBuf;
  m_pImgBuf = 0;
 }
}

bool Wavelet::InitDwt(int nWidth,int nHeight,int nByte,unsigned char * pImgdata)
{
 if ( m_pDwtBuf )
 {
  delete m_pDwtBuf;
  m_pDwtBuf = 0;
  m_bInited = false;
 }

 m_nWidth = nWidth;
 m_nHeight = nHeight;
 m_nByte  = nByte;

 if ( ( m_nWidth != 1<<Log2(m_nWidth) ) || (m_nHeight != 1<<Log2(m_nHeight)) )
  return false;

 unsigned char * pImg = pImgdata;
 float    * pf ; 
 int    BPL  = m_nWidth*m_nByte;
 
 int i,j,k;

 if(pImg != 0)
 {
  m_pDwtBuf = new float[m_nWidth*m_nHeight*m_nByte];
  
  pf = m_pDwtBuf;

  for (  k = 0; k < m_nByte; k++ )
  {
   pImg = pImg+m_nHeight*BPL;
   for ( j = 0; j < m_nHeight; j++ )
   {
    pImg -= BPL;
    for ( i = 0; i < m_nWidth; i++ )
    {
     pf[i] = pImg[i*m_nByte+k];
    }
    pf+=m_nWidth;
   }
  }

 }
 else
 {
  return false;
 }

 m_bInited = true;

 return true;
}

bool Wavelet::InitDwt(int nWidth,int nHeight,int nByte,int * pIntdata,int nCurDepth)
{
 if ( m_pDwtBuf )
 {
  delete m_pDwtBuf;
  m_pDwtBuf = 0;
  m_bInited = false;
 }

 m_nWidth = nWidth;
 m_nHeight = nHeight;
 m_nByte  = nByte;

 if ( ( m_nWidth != 1<<Log2(m_nWidth) ) || (m_nHeight != 1<<Log2(m_nHeight)) )
  return false;

 int     * pEzw = pIntdata;
 float    * pf ; 
 int    BPL  = m_nWidth*m_nByte;
 
 int i,j,k;

 if(pEzw != 0)
 {
  m_pDwtBuf = new float[m_nWidth*m_nHeight*m_nByte];
  
  pf = m_pDwtBuf;

  for (  k = 0; k < m_nByte; k++ )
  {
   pEzw = pEzw+m_nHeight*BPL;
   for ( j = 0; j < m_nHeight; j++ )
   {
    pEzw -= BPL;
    for ( i = 0; i < m_nWidth; i++ )
    {
     pf[i] = (float)pEzw[i*m_nByte+k];
    }
    pf+=m_nWidth;
   }
  }

 }
 else
 {
  return false;
 }

 SetCurrentDepth(nCurDepth);

 m_bInited = true;

 return true;
}

bool Wavelet::WaveletDwt(int Inv)
{
 if(!m_bInited)
 {
  return false;
 }

 if(m_nCurDepth<= 0 && Inv == 1)
 {
  return false;
 }

 if (!wavelet_dwt2d(Inv, 1))
  return false;

 if (Inv)
  m_nCurDepth--;
 else
  m_nCurDepth++;

 return true;
}

void Wavelet::SetSupp(int nSupp)
{
 m_nSupp=nSupp;
}

void Wavelet::SetCurrentDepth(int nCurDepth)
{
 m_nCurDepth = nCurDepth;
}

int Wavelet::GetCurrentDepth()
{
 return m_nCurDepth;
}

unsigned char * Wavelet::GetImgData(bool bOptimize)
{
 
 if(!m_pImgBuf)
 {
  m_pImgBuf = new unsigned char[m_nWidth*m_nHeight*m_nByte];
 }
 unsigned char * pb = m_pImgBuf ;
 float    * pf = m_pDwtBuf;


 unsigned char * p = pb;

 int lfw = m_nWidth>>m_nCurDepth, lfh = m_nHeight>>m_nCurDepth;
 int BPL = m_nWidth*m_nByte;
 
 int i,j,k;
 for ( k = 0; k < m_nByte; k++ )
 {
  p = p+m_nHeight*BPL;
  for ( j = 0; j < m_nHeight; j++ )
  {
   p -= BPL;
   for ( i = 0; i < m_nWidth; i++ )
   {
    if ( ( j < lfh ) && ( i < lfw ) )
     p[i*m_nByte+k] = FloatToByte(pf[i]);
    else
     p[i*m_nByte+k] =(bOptimize)?(unsigned char)(FloatToChar(pf[i]) ^ 0x80):
      (unsigned char)(FloatToChar(pf[i]));
   }
   pf+=m_nWidth;
  }
 }

 return m_pImgBuf;
}

int *Wavelet::GetIntData(bool bOptimize)
{
 if(!m_pIntBuf)
 {
  m_pIntBuf = new int[m_nWidth*m_nHeight*m_nByte];
 }
 
 int     * pn = m_pIntBuf;
 float    * pf = m_pDwtBuf;
 
 int *p = pn;

 int lfw = m_nWidth>>m_nCurDepth, lfh = m_nHeight>>m_nCurDepth;
 int BPL = m_nWidth*m_nByte;
 
 int i,j,k;
 for ( k = 0; k < m_nByte; k++ )
 {
  p = p+m_nHeight*BPL;
  for ( j = 0; j < m_nHeight; j++ )
  {
   p -= BPL;
   for ( i = 0; i < m_nWidth; i++ )
   {
    p[i*m_nByte+k] = (pf[i]>255)?255:((pf[i]<-255)?-255:(int)pf[i]);
   }
   pf+=m_nWidth;
  }
 }

 return m_pIntBuf;
}

unsigned char Wavelet::FloatToByte(float f)
{
 return (f<=0)?((unsigned char)0):((f>=255)?(unsigned char)255:(unsigned char)(f+0.5));
}

char Wavelet::FloatToChar(float f)
{
 if (f>=0)
  return (f>=127.0)?(char)127:(char)(f+0.5);
 else
  return (f<=-128)?(char)-128:-(char)(-f+0.5);
}


int Wavelet::Log2(int n)
{
 int result = 0;
 while (n >>= 1) result++;
 return result;
}

bool Wavelet::wavelet_dwt2d(int Inv,int nStep)
{
 int nCurWLevel = Log2(m_nWidth)  - m_nCurDepth;
 int nCurHLevel = Log2(m_nHeight) - m_nCurDepth;
 int nCurW    = 1 << nCurWLevel;
 int nCurH    = 1 << nCurHLevel;

 int i,k;

 if (!Inv)
 {
  for (k = 0; k < m_nByte; k++ )
  {
   for (i = 0; i < nCurH; i++ )
   {
    if (!wavelet_dwt1d(m_pDwtBuf+k*m_nWidth*m_nHeight+(int)i*m_nWidth, nCurWLevel, Inv, nStep))
     return false;
   }
  }
               
        for ( k = 0; k < m_nByte; k++ )
  {
   for ( i = 0; i < nCurW; i++ )
   {
    if (!wavelet_dwt1d(m_pDwtBuf+k*m_nWidth*m_nHeight+i, nCurHLevel, Inv, m_nWidth))
     return false;
   }
  }
 }
 else
 {
  nCurW <<= 1;
  nCurH <<= 1;

  for ( k = 0; k < m_nByte; k++ )
  {
   for ( i = 0; i < nCurW; i++ )
   {
    if (!wavelet_dwt1d(m_pDwtBuf+k*m_nWidth*m_nHeight+i, nCurHLevel, Inv, m_nWidth))
     return false;
   }
  }

  for ( k = 0; k < m_nByte; k++ )
  {
   for ( i = 0; i < nCurH; i++ )
   {
    if (!wavelet_dwt1d(m_pDwtBuf+k*m_nWidth*m_nHeight+(int)i*m_nWidth, nCurWLevel, Inv, nStep))
     return false;
   }
  }
 }
 return true;
}

bool Wavelet::wavelet_dwt1d(float *pfBuf, int CurLevel, int Inv, int Step)
{
 float s = (float) sqrt(2);
 float* h = (float*) hCoef[m_nSupp-1];

 if(CurLevel < 0)
 {
  return false;
 }
  
 int CurN = 1<<CurLevel;
 int iMask1 = CurN - 1, iMask2 = CurN/2 - 1;
   
 if (Inv) CurN <<= 1;
 if (m_nSupp<1 || m_nSupp>10 || CurN<2*m_nSupp) return false;

 float *ptemp = new float[CurN];
 if (!ptemp) return false;

 float s1, s2;
 int Index1, Index2;
 int i,j;
 if (!Inv)
 { 
  Index1 = 0, Index2 = 2*m_nSupp-1;
  for (  i = 0; i < CurN/2; i++ )
  { 
   s1 = s2 = 0;
   float t = -1;
            for ( j = 0; j < 2*m_nSupp; j++, t = -t )
   {
    s1 += h[j]*pfBuf[(Index1 & iMask1) * Step];
    s2 += t*h[j]*pfBuf[(Index2 & iMask1) * Step];         
    Index1++, Index2--;
   }
   ptemp[i] = s1/s;
   ptemp[i+CurN/2] = s2/s;           
   Index1 -= 2*m_nSupp;
   Index2 +=2*m_nSupp;
   Index1 += 2;
   Index2 += 2;
  }
 }
 else
 {
  Index1 = CurN/2;
  Index2 = CurN/2-m_nSupp+1;
  for (  i = 0; i < CurN/2; i++ )
  {
   s1 = s2 = 0;
   int Index3 = 0;
   for (  j = 0; j < m_nSupp; j++ )
   {
     s1 += h[Index3]*pfBuf[(Index1 & CurN/2-1) * Step]
       +h[Index3+1]*pfBuf[((Index2 & CurN/2-1) + CurN/2) * Step];
     s2 += h[Index3+1]*pfBuf[(Index1 & CurN/2-1) * Step]
       -h[Index3]*pfBuf[((Index2 & CurN/2-1) + CurN/2) * Step];
  
     Index3 += 2;
     Index1--;
     Index2++;
   }
   ptemp[2*i] = s1*s;
   ptemp[2*i+1] = s2*s;

   Index1 += m_nSupp;
   Index2 -= m_nSupp;
   Index1++;
   Index2++;
  }
 }
 
 for (  i = 0; i < CurN; i++ )
  pfBuf[i*Step] = ptemp[i];

 delete[] ptemp;
 return true;
}

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

cwt-0.1.tar.gz cwt.pdf cwtlib-1.5.tar.gz ecg-vc8.rar ecg.rar fwt_filters.rar MassSpecWavelet-manual.pdf Publ_24-27_May_2005_Rus.tar.gz toolbox.tar.Z VC实现的二维小波变换源代码.rar VC编写,具有小波变换,复原,图像融合等算法.经过调试,可以正常运行.rar wavelet.0.3.tar.gz 图像处理中的小波变换C++源代码,含有使用文档和例子,非常全面!.rar 图像小波变换的vc++代码,包括小波变换WaveletTransform.rar 基于Gabor小波变换的特征提取和人工智能的人脸检测系统源代码.rar 基于小波变换的图像融合技术研究.exe 基于小波变换的车型特征提取及车型分类PPT(程啸岚).rar(5.08M).rar 小波人脸表情.rar 小波代码.rar 小波变换的代码.是学习小波变换的好的例程!-.tar 小波变换C++源代码(1).rar 小波变换C++源代码.asp.rar 小波变换C++源代码.rar 小波变换C++程序.rar 小波变换的数字水印算法,附有论文和源程序.rar 小波变换程序源代码.rar 小波变换算法.zip(121.27K).zip 小波时频画图工具包,能较好地画出小波变换地时频图.rar 小波器-代码.r.. . 关于常用图像处理的v.. . 采用多种小波基的离散.. . 利用小波变换进行边缘.. . 一个.rar 常用的小波变换程序.rar 本程序实现二维灰度图像的小波变换和逆变换wavelet.rar 脊变换.zip 这是我以前收集的一个毕业论文(包括源程序),是关于图像小波变换实现及EZW编码理论研究,供大家交流.rar 连续小波变换下载.rar 连续小波程序入门.rar
©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值