图像处理 离散haar小波变换

编程环境:windows下结合opencv库

//离散Haar小波变换
/*
dst深度为IPL_DEPTH_32F
nLayer为变换尺度
*/
void HaarWavelet(IplImage* src, IplImage* dst, int nLayer);
//离散Haar小波变换
/*
dst深度为IPL_DEPTH_32F
nLayer为变换尺度
*/
void HaarWavelet(IplImage* src, IplImage* dst, int nLayer)
{
	if (!dst)
	{
		return;
	}
	if (((dst->width >> nLayer) << nLayer != dst->width) 
		|| ((dst->height >> nLayer) << nLayer != dst->height)
		|| (dst->depth != IPL_DEPTH_32F)
		|| (src->nChannels != 1)
		|| (dst->nChannels != 1)
		|| (nLayer <= 0))
	{
		return;
	}

	int x, y;
	int nWidth = dst->width;
	int nHeight = dst->height;
	int nHalfWidth = nWidth / 2;
	int nHalfHeight = nHeight / 2;
	//图像数据的起始地址
	float* *pfData = (float**)(malloc(sizeof(float*) * nHeight));
	//保存计算过程中用到的行列数据
	float* pfRow = (float*)(malloc(sizeof(float) * nWidth));
	float* pfColumn = (float*)(malloc(sizeof(float) * nHeight));
	CvMat tmp;
	//预先填充dst
	cvZero(dst);
	cvGetSubRect(dst, &tmp, cvRect(0, 0, src->width, src->height));
	cvScale(src, &tmp, 1.0, 0);
	//保存图像数据每行的起始地址
	for (y = 0; y < nHeight; y++)
	{
		pfData[y] = (float*)(dst->imageData + y * dst->widthStep);
	}

	while (nLayer > 0)
	{
		//行变换
		for (y = 0; y < nHeight; y++)
		{
			for (x = 0; x < nHalfWidth; x++)
			{
				pfRow[x] = (pfData[y][2 * x] + pfData[y][2 * x + 1]) / 2.0;
				pfRow[x + nHalfWidth] = (pfData[y][2 * x] - pfData[y][2 * x + 1]) / 2.0;
			}
			for (x = 0; x < nWidth; x++)
			{
				pfData[y][x] = pfRow[x];
			}
		}
		//列变换
		for (x = 0; x < nWidth; x++)
		{
	
			for (y = 0; y < nHalfHeight; y++)
			{
				pfColumn[y] = (pfData[2 * y][x] + pfData[2 * y + 1][x]) / 2;
				pfColumn[y + nHalfHeight] = (pfData[2 * y][x] - pfData[2 * y + 1][x]) / 2;
			}
			for (y = 0; y < nHeight; y++)
			{
				pfData[y][x] = pfColumn[y];
			}
		}
		//一层变换后尺度的缩小
		nLayer--;
		nWidth = nHalfWidth;
		nHeight = nHalfHeight;
		nHalfWidth = nHalfWidth / 2;
		nHalfHeight = nHalfHeight / 2;
	}

	//结果缩放以便与显示
	double min,max;
	cvMinMaxLoc(dst, &min, &max, NULL, NULL, NULL);
    cvScale(dst, dst, 1.0/(max-min), 1.0*(-min)/(max-min));
}


你可能感兴趣的:(图像处理)