[Filtering·4] Haar wavelet - principle

This series of articles tries to explain the principles and applications of Haar wavelets in an easy-to-understand manner. The application part mainly focuses on the double product integral and triple product integral parts used in PRT. The main references are Stollnitz et al.'s Wavelets for Computer Graphics: A Primer and Ren Ng's two classic articles on wavelet relighting.

1. 1D Hair

Haar wavelet is the simplest wavelet basis function.

We first start with an example of one-dimensional Haar wavelet decomposition:

Suppose we have a one-dimensional "image" with a resolution of 1 x 4:

[8,4,1,3]

This image can be represented by one-dimensional Haar wavelet coefficients as follows:

(1) Calculate the mean between two pixels

[8+42,1+32]=[6,2]

(2) Calculate the difference between two pixels

[8−42,1−32]=[2,−1]

In this way, we perform a Haar decomposition on the original image. With each decomposition, the resolution of the mean "image" is halved. Repeat the above two steps for each new mean part generated until there is only one number left in the mean and cannot be further decomposed:

Resolution Approximation coefficients Detail coefficients
4 [8, 4, 1, 3]
2 [6, 2] [2, -1]
1 [4] [2]

In this way, we obtain a set of Haar wavelet basis coefficients consisting of 1 mean (approximation coefficients) and 3 differences (detail coefficients):

[8,4,1,3]→[4,2,2,−1]

During this decomposition process, there is no loss of image information, and the image can be restored losslessly through the inverse operation of decomposition:

[4,2,2,−1]→[4+2,4−2,2,−1]=[6,2,2,−1]

[6,2,2,−1]→[6+2,6−2,2+(−1),2−(−1)]=[8,4,1,3]

The above reduction method is based on the inverse operation of our decomposition. Next, we look at the decomposition and reduction process just from the perspective of basis functions.

First, two functions of Haar wavelet are introduced.

 

Looking at the previous decomposition process from the perspective of basis functions

The coefficients obtained after the first decomposition are [6,2,2,−1], where the approximation coefficients are 6,2 and the detail coefficients are 2,−1. The corresponding expression is:

The coefficients obtained after the second decomposition are [4,2,2,−1], where the approximation coefficients are 4 and the detail coefficients are 2,2,−1:

2. 2D Haar

2D Haar decomposition can be viewed as 1D Haar decomposition for all rows and all columns separately. Decomposing rows and columns in different orders results in two different decomposition methods.

The method of first decomposing the rows to the finest detail and then decomposing the columns is called standard decomposition. The method of alternating row and column decomposition is called non-standard decomposition.

Standard decomposition

Non-standard decomposition

It should be noted that the basis functions generated by these two decomposition methods are different and must be distinguished.

The triple Product Wavelet Integrals we will introduce later uses the second type of non-standard decomposition.

3. Wavelet example

// verified on opencv4.2.0
# include<opencv.hpp>
# include<iostream>
 
 
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<float>(i, j) = (imgtmp.at<float>(i, 2 * j) + imgtmp.at<float>(i, 2 * j + 1)) / 2; //mean
				tmp.at<float>(i, j + width / 2) = (imgtmp.at<float>(i, 2 * j) - imgtmp.at<float>(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<float>(i, j) = (tmp.at<float>(2 * i, j) + tmp.at<float>(2 * i + 1, j)) / 2;
				wavelet.at<float>(i + height / 2, j) = (tmp.at<float>(2 * i, j) - tmp.at<float>(2 * i + 1, j)) / 2;
			}
		}
		imgtmp = wavelet;
		depthcount++;
	}
 
	normalize(wavelet, wavelet, 0, 1, cv::NORM_MINMAX);
 
	imshow("wavelet", wavelet);
 
	waitKey(0);
	return 0;
}

Reference

Wavelets for Computer Graphics: A Primer

The principle of Haar wavelet transform and opencv source code_haar wavelet transform principle_kuweicai's blog-CSDN blog 

 Haar Wavelet - Principle - Zhihu

Guess you like

Origin blog.csdn.net/u013066730/article/details/132182442