#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
void main()
{
IplImage *src = 0;
src = cvLoadImage("1.jpg",CV_LOAD_IMAGE_GRAYSCALE);
cvNamedWindow("源图像",1);
cvShowImage("源图像",src);
IplImage *Wave = cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
IplImage *DstWave = cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
IplImage *SrcWave = cvCreateImage(cvGetSize(src),IPL_DEPTH_32F,1);
cvSetZero(SrcWave);
cvSetZero(Wave);
cvSetZero(DstWave);
cvConvertScale(src, SrcWave, 1, -128);//-128?无符号字符型数据转换为float型数据
cvNamedWindow("SrcWave",1);
cvShowImage("SrcWave",SrcWave);
int x = 0;
int y = 0;
int nLayer = 2;
float pSqrt2 = sqrt(2.0f);
int nWidth = (src->width >> nLayer) << nLayer;
int nHeight = (src->height >> nLayer) << nLayer;
for(int n = 0; n < nLayer; n++)
{
nWidth = nWidth >> n;
nHeight = nHeight >> n;
int nHalfW = nWidth >> 1;
int nHalfH = nHeight >> 1;
float *pSrcWaveData = 0;
float *pWaveData = 0;
//行处理
for(y = 0; y < nHeight; y++)
{
pSrcWaveData = (float *)(SrcWave->imageData + y * SrcWave->widthStep);
pWaveData = (float *)(Wave->imageData + y * Wave->widthStep);
for(x = 0; x < nHalfW - 1; x++)
{
pWaveData[x] = (pSrcWaveData[2 * x]
+ pSrcWaveData[2 * x + 2])/ 2;
pWaveData[x + nHalfW] = pSrcWaveData[2 * x + 1]
- pWaveData[x];
}
pWaveData[x] = (pSrcWaveData[2 * x] + pSrcWaveData[2 * x + 1])/ 2;
pWaveData[x + nHalfW] = pSrcWaveData[2 * x + 1] - pWaveData[x];
pWaveData[0] += (pWaveData[nHalfW] + pWaveData[nHalfW + 1]) / 4;
for(x = 1; x < nHalfW; x++)
{
pWaveData[x] += (pWaveData[nHalfW + x] + pWaveData[nHalfW + x - 1]) / 4;
}
for(x = 0; x < nHalfW ; x++)
{
pWaveData[ x] = pWaveData[ x] * pSqrt2;
pWaveData[nHalfW + x] = pWaveData[nHalfW + x] / pSqrt2;
}
}
//列处理
for(x = 0; x < nWidth; x++)
{
for(y = 0; y < nHalfH - 1; y++)
{
((float *)(DstWave->imageData + y * DstWave->widthStep))[x] = (((float *)(Wave->imageData + 2 * y * Wave->widthStep))[x] +
+ ((float *)(Wave->imageData + (2 * y + 2) * Wave->widthStep))[x]) / 2;
((float *)(DstWave->imageData + (y + nHalfH) * DstWave->widthStep))[x] = ((float *)(Wave->imageData + (2 * y + 1) * Wave->widthStep))[x]
- ((float *)(DstWave->imageData + y * DstWave->widthStep))[x];
}
((float *)(DstWave->imageData + y * DstWave->widthStep))[x] = (((float *)(Wave->imageData + 2 * y * Wave->widthStep))[x] +
+ ((float *)(Wave->imageData + (2 * y + 1) * Wave->widthStep))[x]) / 2;
((float *)(DstWave->imageData + (y + nHalfH) * DstWave->widthStep))[x] = ((float *)(Wave->imageData + (2 * y + 1) * Wave->widthStep))[x]
- ((float *)(DstWave->imageData + y * DstWave->widthStep))[x];
((float *)(DstWave->imageData + (y + nHalfH) * DstWave->widthStep))[0] += (((float *)(DstWave->imageData + (y + nHalfH) * DstWave->widthStep))[0] +
((float *)(DstWave->imageData + (y + nHalfH - 1) * DstWave->widthStep))[0]) / 4;
for(y = 1; y < nHalfH ; y++)
{
((float *)(DstWave->imageData + (y + nHalfH) * DstWave->widthStep))[x] += (((float *)(DstWave->imageData + (y + nHalfH) * DstWave->widthStep))[x] +
((float *)(DstWave->imageData + (y + nHalfH - 1) * DstWave->widthStep))[x]) / 4;
}
for(y = 1; y < nHalfH ; y++)
{
((float *)(DstWave->imageData + y * DstWave->widthStep))[x] = ((float *)(DstWave->imageData + y * DstWave->widthStep))[x] * pSqrt2;
((float *)(DstWave->imageData + (y + nHalfH) * DstWave->widthStep))[x] = ((float *)(DstWave->imageData + (y + nHalfH) * DstWave->widthStep))[x] / pSqrt2;
}
}
cvCopyImage(DstWave, SrcWave);
}
cvNamedWindow("Wave",1);
cvShowImage("Wave",DstWave);
cvWaitKey(0);
}