请问谁有小波变换的程序?

解决方案 »

  1.   

    很多地方有。你可以参考一下Matlab中的源代码。
      

  2.   


    #define N 256
    #define FilterLth 9static double h[FilterLth] ={0.0267487,-0.016864,-0.0782234,0.266864,0.602949,0.266864,
    -0.0782234,-0.016864,0.0267487};
    static double g[FilterLth] ={0,-0.0456358,0.0287716,0.295636,-0.557543,0.295636,
    0.0287716,-0.0456358,0};static double hi[FilterLth]={0,-0.0456358,-0.0287716,0.295636,0.557543,0.295636,
    -0.0287716,-0.0456358,0};
    static double gi[FilterLth]={-0.0267487,-0.016864,0.0782234,0.266864,-0.602949,0.266864,
    0.0782234,-0.016864,-0.0267487};
    /*
    static double h[FilterLth] ={0.037828,-0.023849,-0.110624,0.377402,0.852699,0.377402,
    -0.110624,-0.023849,0.037828};
    static double g[FilterLth]={0,-0.064539,-0.040689,-0.418092,0.788486,0.418092,
    -0.040689,-0.064539,0};
    */
    class CWavelet  
    {
    public:
    BYTE *YBuf,*CbBuf,*CrBuf;
    void WaveletWTGray(PBYTE pImgData,DWORD Width,DWORD Height,int depth);
    void WaveletWTRGB(LPBITMAPINFOHEADER lpbi,PBYTE pImgData,int depth);
    void WaveletIWTGray(PBYTE pImgData,DWORD Width,DWORD Height,int depth);
    void WaveletIWTRGB(LPBITMAPINFOHEADER lpbi,PBYTE pImgData,int depth);
    CWavelet();
    virtual ~CWavelet();
    void WT(float *pfImgData,int XPos, int YPos,int SrcWidth,int SrcHeight,int depth);
    void IWT(float *pfImgData,int XPos, int YPos,int SrcWidth,int SrcHeight,int depth);
    private:
    int EdgeTran(int Pos,int Edge);
    protected:
    void RevertQuan(PBYTE pWTData,DWORD Width,DWORD Height,int depth);
    void Quantize(PBYTE pWTData,DWORD Width,DWORD Height,int depth);
    };CWavelet::CWavelet()
    {

    }CWavelet::~CWavelet()
    {}static int int_2pow(int y)
    {
    if(y==0) return 1;
    else if(y>0)
    return ((int)1<<y);
    else
    return 1/((int)1<<(-y));
    }int CWavelet::EdgeTran(int Pos,int Edge)
    {
    if(Pos<0)  Pos=Pos+Edge;   
    if(Pos>=Edge) Pos=Pos-Edge;
    return Pos;
    }void CWavelet::WT(float *pfImgData,int XPos, int YPos,int SrcWidth,int SrcHeight,int depth)
    {
    int row,colum,temp,Length;
    float low,high;

    if(depth<0) return;
    float *tempx=(float *)malloc(SrcWidth*SrcHeight*sizeof(float));
    for(row=0;row<YPos;row++)
    for(colum=0;colum<XPos;colum+=2)
    {
    low=high=0;
    for(Length=-(FilterLth-1)/2;Length<=(FilterLth-1)/2;Length++)
    {
    temp=EdgeTran(colum+Length,XPos);
    low=low+float(h[Length+(FilterLth-1)/2]*pfImgData[row*SrcWidth+temp]);
    }
    for(Length=-(FilterLth-1)/2-1;Length<=(FilterLth-1)/2-1;Length++)
    {
    temp=EdgeTran(colum+Length,XPos);
    high=high+float(g[Length+(FilterLth-1)/2+1]*pfImgData[row*SrcWidth+temp]);
    }
    tempx[row*SrcWidth+colum/2]=low;
    tempx[row*SrcWidth+colum/2+XPos/2]=high;
    } for(row=0;row<YPos;row+=2)
    for(colum=0;colum<XPos;colum++)
    {
    low=high=0;
    for(Length=-(FilterLth-1)/2;Length<=(FilterLth-1)/2;Length++)
    {
    temp=EdgeTran(row+Length,YPos);
    low+=float(h[Length+(FilterLth-1)/2]*tempx[temp*SrcWidth+colum]);
    }
    for(Length=-(FilterLth-1)/2-1;Length<=(FilterLth-1)/2-1;Length++)
    {
    temp=EdgeTran(row+Length,YPos);
    high+=float(g[Length+(FilterLth-1)/2+1]*tempx[temp*SrcWidth+colum]);
    }
    pfImgData[(row/2)*SrcWidth+colum]=low;
    pfImgData[(row/2+YPos/2)*SrcWidth+colum]=high;
    }
    delete[] tempx;
    if(depth>0)
    WT(pfImgData,XPos/2, YPos/2,SrcWidth,SrcHeight,depth-1);
    }
      

  3.   

    void CWavelet::IWT(float *pfImgData,int XPos, int YPos,int SrcWidth,int SrcHeight,int depth)
    {
    int row,colum,tempLine;
    int Length;
    float temp;
    float *bufLow,*bufHigh;
    int tw1=SrcWidth; if(SrcWidth<SrcHeight)
    tw1=SrcHeight;
    bufLow=(float *)malloc(tw1*sizeof(float));
    bufHigh=(float *)malloc(tw1*sizeof(float));
    if(depth<0) return;
    for(colum=0;colum<XPos;colum++)
    {
    for(row=0;row<YPos/2;row++)
    {
    bufLow[2*row]=pfImgData[row*SrcWidth+colum];
    bufLow[2*row+1]=0;
    }
    for(row=0;row<YPos;row++)
    {
    temp=0;
    for(Length=-FilterLth/2;Length<=FilterLth/2;Length++)
    {
    tempLine=EdgeTran(row+Length,YPos);
    temp+=float(hi[Length+FilterLth/2]*bufLow[tempLine]);
    }
    bufHigh[row]=temp;
    }
    for(row=0;row<YPos/2;row++)
    {
    bufLow[2*row]=pfImgData[(row+YPos/2)*SrcWidth+colum];
    bufLow[2*row+1]=0;
    }
    for(row=0;row<YPos;row++)
    {
    temp=0;
    for(Length=-FilterLth/2+1;Length<=FilterLth/2+1;Length++)
    {
    tempLine=EdgeTran(row+Length,YPos);
    temp+=float(gi[Length+FilterLth/2-1]*bufLow[tempLine]);
    }
    bufHigh[row]+=temp;
    }
    for(row=0;row<YPos;row++)
    pfImgData[row*SrcWidth+colum]=bufHigh[row];
    }
    for(row=0;row<YPos;row++)
    {
    for(colum=0;colum<XPos/2;colum++)
    {
    bufLow[2*colum]=pfImgData[row*SrcWidth+colum];
    bufLow[2*colum+1]=0;
    }
    for(colum=0;colum<XPos;colum++)
    {
    temp=0;
    for(Length=-FilterLth/2;Length<=FilterLth/2;Length++)
    {
    tempLine=EdgeTran(colum+Length,XPos);
    temp+=float(hi[Length+FilterLth/2]*bufLow[tempLine]);
    }
    bufHigh[colum]=temp;
    }
    for(colum=0;colum<XPos/2;colum++)
    {
    bufLow[2*colum]=pfImgData[row*SrcWidth+colum+XPos/2];
    bufLow[2*colum+1]=0;
    }
    for(colum=0;colum<XPos;colum++)
    {
    temp=0;
    for(Length=-FilterLth/2+1;Length<=FilterLth/2+1;Length++)
    {
    tempLine=EdgeTran(colum+Length,XPos);
    temp+=float(gi[Length+FilterLth/2-1]*bufLow[tempLine]);
    }
    bufHigh[colum]+=temp;
    }
    for(colum=0;colum<XPos;colum++)
    pfImgData[row*SrcWidth+colum]=bufHigh[colum]*4;
    }
    free(bufLow);
    free(bufHigh);
    if(depth>0)
    IWT(pfImgData,XPos*2, YPos*2,SrcWidth,SrcHeight,depth-1);
    }void CWavelet::WaveletWTGray(PBYTE pImgData,DWORD Width,DWORD Height,int depth)
    {
    DWORD i,j;
    float *pTemp=(float *)malloc(Width*Height*sizeof(float));
    for(i=0;i<Height;i++)
    for(j=0;j<Width;j++)
    pTemp[i*Width+j]=(float)pImgData[i*Width+j]-128;
    ///////////////////////////////////////////
    float *tempx=(float *)malloc(Width*Height*sizeof(float));
    int row,colum,temp,Length;
    float low,high;
    int XPos,YPos;
    for(int dp=0;dp<depth;dp++)
    {
    XPos=Width>>dp;
    YPos=Height>>dp;
    for(row=0;row<YPos;row++)
    for(colum=0;colum<XPos;colum+=2)
    {
    low=high=0;
    // for(Length=-(FilterLth-1)/2;Length<=(FilterLth-1)/2;Length++)
    for(Length=-4;Length<=4;Length++)
    {
    temp=EdgeTran(colum+Length,XPos);
    low=low+float(h[Length+(FilterLth-1)/2]*pTemp[row*Width+temp]);
    }
    // for(Length=-(FilterLth-1)/2-1;Length<=(FilterLth-1)/2-1;Length++)
    for(Length=-5;Length<=3;Length++)
    {
    temp=EdgeTran(colum+Length,XPos);
    high=high+float(g[Length+(FilterLth-1)/2+1]*pTemp[row*Width+temp]);
    }
    tempx[row*Width+colum/2]=low;
    tempx[row*Width+colum/2+XPos/2]=high;
    } for(row=0;row<YPos;row+=2)
    for(colum=0;colum<XPos;colum++)
    {
    low=high=0;
    // for(Length=-(FilterLth-1)/2;Length<=(FilterLth-1)/2;Length++)
    for(Length=-4;Length<=4;Length++)
    {
    temp=EdgeTran(row+Length,YPos);
    low+=float(h[Length+(FilterLth-1)/2]*tempx[temp*Width+colum]);
    }
    // for(Length=-(FilterLth-1)/2-1;Length<=(FilterLth-1)/2-1;Length++)
    for(Length=-5;Length<=3;Length++)
    {
    temp=EdgeTran(row+Length,YPos);
    high+=float(g[Length+(FilterLth-1)/2+1]*tempx[temp*Width+colum]);
    }
    pTemp[(row/2)*Width+colum]=low;
    pTemp[(row/2+YPos/2)*Width+colum]=high;
    }
    }
    free(tempx);
    ////////////////////////////////////////
    for(i=0;i<Height;i++)
    for(j=0;j<Width;j++)
    {
    int Value=(int)pTemp[i*Width+j];
    if(Value>=0) Value=((Value>>1)<<1);//Value/2*2;
    else Value=(((-Value)>>1)<<1)+1;//-Value/2*2+1;
    // if(Value<8) Value=255;
    pImgData[i*Width+j]=(BYTE)Value;
    }
    free(pTemp);
    Quantize(pImgData,Width,Height,depth);
    }
      

  4.   


    void CWavelet::WaveletIWTGray(PBYTE pImgData,DWORD Width,DWORD Height,int depth)
    {
    DWORD i,j; float *pTemp=(float *)malloc(Width*Height*sizeof(float));
    RevertQuan(pImgData,Width,Height,depth);
    for(i=0;i<Height;i++)
    for(j=0;j<Width;j++)
    {
    int Value=(int)pImgData[i*Width+j];
    if(Value&0x0001)//判断是否为奇数
    Value=-(((Value-1)>>1)<<1);//-(Value-1)/2*2;
    pTemp[i*Width+j]=(float)Value;
    }
    ////////////////////////////////////////////
    int row,colum,tempLine;
    int XPos,YPos;
    int Length;
    float temp;
    float *bufLow,*bufHigh;
    int tw1=Width; if(Width<Height)
    tw1=Height;
    bufLow=(float *)malloc(tw1*sizeof(float));
    bufHigh=(float *)malloc(tw1*sizeof(float));
    for(int dp=depth-1;dp>=0;dp--)
    {
    XPos=Width>>dp;
    YPos=Height>>dp;
    for(colum=0;colum<XPos;colum++)
    {
    for(row=0;row<YPos/2;row++)
    {
    bufLow[2*row]=pTemp[row*Width+colum];
    bufLow[2*row+1]=0;
    }
    for(row=0;row<YPos;row++)
    {
    temp=0;
    for(Length=-FilterLth/2;Length<=FilterLth/2;Length++)
    {
    tempLine=EdgeTran(row+Length,YPos);
    temp+=float(hi[Length+FilterLth/2]*bufLow[tempLine]);
    }
    bufHigh[row]=temp;
    }
    for(row=0;row<YPos/2;row++)
    {
    bufLow[2*row]=pTemp[(row+YPos/2)*Width+colum];
    bufLow[2*row+1]=0;
    }
    for(row=0;row<YPos;row++)
    {
    temp=0;
    for(Length=-FilterLth/2+1;Length<=FilterLth/2+1;Length++)
    {
    tempLine=EdgeTran(row+Length,YPos);
    temp+=float(gi[Length+FilterLth/2-1]*bufLow[tempLine]);
    }
    bufHigh[row]+=temp;
    }
    for(row=0;row<YPos;row++)
    pTemp[row*Width+colum]=bufHigh[row];
    }
    for(row=0;row<YPos;row++)
    {
    for(colum=0;colum<XPos/2;colum++)
    {
    bufLow[2*colum]=pTemp[row*Width+colum];
    bufLow[2*colum+1]=0;
    }
    for(colum=0;colum<XPos;colum++)
    {
    temp=0;
    for(Length=-FilterLth/2;Length<=FilterLth/2;Length++)
    {
    tempLine=EdgeTran(colum+Length,XPos);
    temp+=float(hi[Length+FilterLth/2]*bufLow[tempLine]);
    }
    bufHigh[colum]=temp;
    }
    for(colum=0;colum<XPos/2;colum++)
    {
    bufLow[2*colum]=pTemp[row*Width+colum+XPos/2];
    bufLow[2*colum+1]=0;
    }
    for(colum=0;colum<XPos;colum++)
    {
    temp=0;
    for(Length=-FilterLth/2+1;Length<=FilterLth/2+1;Length++)
    {
    tempLine=EdgeTran(colum+Length,XPos);
    temp+=float(gi[Length+FilterLth/2-1]*bufLow[tempLine]);
    }
    bufHigh[colum]+=temp;

    for(colum=0;colum<XPos;colum++)
    pTemp[row*Width+colum]=bufHigh[colum]*4;
    }
    }
    free(bufLow);
    free(bufHigh);
    ////////////////////////////////////////////
    for(i=0;i<Height;i++)
    for(j=0;j<Width;j++)
    {
    float Value=pTemp[i*Width+j]+128;
    if(Value>255) Value=255;
    if(Value<0) Value=0;
    pImgData[i*Width+j]=(BYTE)Value;
    }
    free(pTemp);
    }