哈尔(Haar)小波变换的原理及opencv源代码

关于haar小波变换的原理可以参考haar小波变换

如果不想看英文版的,可以参考哈尔小波变换的原理及其实现(Haar)

这里主要给出haar小波变换的opencv实现代码。

 

 

// verified on opencv4.2.0
# include
# include


using namespace std;
using namespace cv;


int main()
{
	Mat img = imread("Lena.jpg", cv::IMREAD_GRAYSCALE);
	int width = img.cols;
	int height = img.rows;
	int depth = 2;   
	int depthcount = 1;

	Mat tmp = Mat::ones(img.size(), CV_32FC1);
	Mat wavelet = Mat::ones(img.size(), CV_32FC1);
	Mat imgtmp = img.clone();
	imshow("src", imgtmp);
	imgtmp.convertTo(imgtmp, CV_32FC1, 1.0 / 255);

	while (depthcount <= depth)
	{
		height = img.rows / depthcount;
		width = img.cols / depthcount;



		//calculate horizen
		for (int i = 0; i < height; i++) //row
		{
			for (int j = 0; j < width / 2; j++) // col
			{
				tmp.at(i, j) = (imgtmp.at(i, 2 * j) + imgtmp.at(i, 2 * j + 1)) / 2; //mean
				tmp.at(i, j + width / 2) = (imgtmp.at(i, 2 * j) - imgtmp.at(i, 2 * j + 1)) / 2; //diff
			}
		}

		// calculate vertical
		for (int i = 0; i < height / 2; i++)
		{
			for (int j = 0; j < width; j++)
			{
				wavelet.at(i, j) = (tmp.at(2 * i, j) + tmp.at(2 * i + 1, j)) / 2;
				wavelet.at(i + height / 2, j) = (tmp.at(2 * i, j) - tmp.at(2 * i + 1, j)) / 2;
			}
		}
		imgtmp = wavelet;
		depthcount++;
	}

	normalize(wavelet, wavelet, 0, 1, cv::NORM_MINMAX);

	imshow("wavelet", wavelet);

	waitKey(0);
	return 0;
}


运行结果如下:

哈尔(Haar)小波变换的原理及opencv源代码_第1张图片

 

 

你可能感兴趣的:(数字图像处理基础)