均值和差值形式的小波变换和恢复,多层小波变换和恢复效果不好
#include <stdlib.h>
#include <cv.h>
#include <highgui.h>
// 二维离散小波变换(单通道浮点图像)
void dwt(float *pMatrix, int height, int width, int nLayer);
// 二维离散小波恢复(单通道浮点图像)
void idwt(float *pMatrix, int height, int width, int nLayer);
int main()
{
// 小波变换层数
int nLayer = 1;
// 输入彩色图像
IplImage *pSrc = cvLoadImage("12.bmp", CV_LOAD_IMAGE_GRAYSCALE);
//cvSaveImage("222.bmp",pSrc);
// 计算小波图象大小
int height = pSrc->height;
int width = pSrc->width;
// 创建小波图象
cvNamedWindow("1",1);
cvShowImage("1",pSrc);
//cvWaitKey(0);
IplImage *pWavelet = cvCreateImage(cvGetSize(pSrc), IPL_DEPTH_32F, 1);
IplImage *image = cvCreateImage(cvGetSize(pSrc), IPL_DEPTH_8U, 1);
if (pWavelet)
{
float *pMatrix = new float[height*width];
int temp = 0;
int temp1 = 0;
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
pMatrix[i*width + j] = 0.0;
temp1 = (unsigned char)(*(pSrc->imageData+i*pSrc->widthStep+j));
pMatrix[i*width + j] = (float)(temp1);
}
}
{
// 二维离散小波变换
dwt(pMatrix,height,width, nLayer);
// 二维离散小波恢复
idwt(pMatrix,height,width, nLayer);
}
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
temp = (int)(pMatrix[i*width + j]);
if(temp < 0)
temp = 0;
if(temp > 255)
temp = 255;
*(image->imageData+i*image->widthStep+j) = (unsigned char)temp;
}
}
delete pMatrix;
#endif
cvNamedWindow("123",1);
cvShowImage("123",image);
cvWaitKey(0);
cvReleaseImage(&pWavelet);
}
// 显示图像pSrc
// ...
cvReleaseImage(&pSrc);
return 0;
}
//
void dwt(float *pMatrix, int height, int width, int nLayer)
{
int i, x, y, n;
float fValue = 0;
float fRadius = sqrt(2.0f);
int nWidth = width;
int nHeight = height;
int nHalfW = nWidth / 2;
int nHalfH = nHeight / 2;
float **pData = new float*[height];
float *pRow = new float[width];
float *pColumn = new float[height];
for (i = 0; i < height; i++)
{
pData[i] = &pMatrix[i*width];
}
// 多层小波变换
for (n = 0; n < nLayer; n++, nWidth /= 2, nHeight /= 2, nHalfW /= 2, nHalfH /= 2)
{
// 水平变换
for (y = 0; y < nHeight; y++)
{
memcpy(pRow, pData[y], sizeof(float) * nWidth);
for (i = 0; i < nHalfW; i++)
{
x = i * 2;
pData[y][i] = (pRow[x] + pRow[x + 1]) / 2.0f;
pData[y][nHalfW + i] = (pRow[x] - pRow[x + 1]) / 2.0f;
}
}
// 垂直变换
for (x = 0; x < nWidth; x++)
{
for (i = 0; i < nHalfH; i++)
{
y = i * 2;
pColumn[i] = (pData[y][x] + pData[y + 1][x]) / 2.0f;
pColumn[nHalfH + i] = (pData[y][x] - pData[y + 1][x]) / 2.0f;
}
for (i = 0; i < nHeight; i++)
{
pData[i][x] = pColumn[i];
}
}
}
delete[] pData;
delete[] pRow;
delete[] pColumn;
}
//
void idwt(float *pMatrix, int height, int width, int nLayer)
{
int i, x, y, n;
float fValue = 0;
float fRadius = sqrt(2.0f);
int nWidth = width >> (nLayer - 1);
int nHeight = height >> (nLayer - 1);
int nHalfW = nWidth / 2;
int nHalfH = nHeight / 2;
float **pData = new float*[height];
float *pRow = new float[width];
float *pColumn = new float[height];
for (i = 0; i < height; i++)
{
pData[i] = &pMatrix[i*width];
}
// 多层小波恢复
for (n = 0; n < nLayer; n++, nWidth *= 2, nHeight *= 2, nHalfW *= 2, nHalfH *= 2)
{
// 垂直恢复
for (x = 0; x < nWidth; x++)
{
for (i = 0; i < nHalfH; i++)
{
y = i * 2;
pColumn[y] = pData[i][x] + pData[nHalfH + i][x];
pColumn[y + 1] = pData[i][x] - pData[nHalfH + i][x];
}
for (i = 0; i < nHeight; i++)
{
pData[i][x] = pColumn[i];
}
}
// 水平恢复
for (y = 0; y < nHeight; y++)
{
for (i = 0; i < nHalfW; i++)
{
x = i * 2;
pRow[x] = pData[y][i] + pData[y][nHalfW + i];
pRow[x + 1] = pData[y][i] - pData[y][nHalfW + i];
}
memcpy(pData[y], pRow, sizeof(float) * nWidth);
}
}
delete[] pData;
delete[] pRow;
delete[] pColumn;
}