Opencv small exercise: Hal wavelet (Haar)

Source: Internet
Author: User

Opencv small exercise: Hal wavelet (Haar)

First, let's talk about the principle of one-dimensional haar wavelet.
For example, we have a one-dimensional image [2, 4, 6, 8, 10, 12, 14, 16].

Mean: we calculate the mean of adjacent pixels [,]. The new image resolution is half the original resolution (8/2 = 4 ).

Evaluate the difference value. The average value above stores the overall information of the image. However, we have lost a lot of details, so we need to record the details of the image at the same time, so that we can restore all the information of the image during reconstruction. Below is the formula for finding the m-th difference:

B [m] = (a [2 m]? A [2 m + 1])/2

After calculation, we obtain the result [-1,-1,-1,-1]. The new resolution is also half of the original one (8/2 = 4 ).
3. The above two steps form the results of the first decomposition [3, 7, 11, 15,-1,-1,-1,-1]. Contains the overall information and details of the image. In the next decomposition, we repeat Step 1 and break down the overall information again to obtain the second-level decomposition result [5, 13,-2,-2]. similarly, the preceding [5, 13] is the overall information, and the following [-2,-2] is the details.

Resolution Overall Information Details
4 3, 7, 11, 15 -1,-1,-1,-1
2 5, 13 -2,-2
1 9 -4

After three decomposition, we obtain an overall information and three detailed coefficients, which are one-dimensional wavelet transformations.

For two-dimensional haar wavelet, we usually break down the overall image, horizontal details, vertical details, and diagonal details at a time. First, we process rows in the row order according to the principle of one-dimensional haar wavelet decomposition, and then process the row processing results in the same order of columns. Finally, the following forms are formed.

The next step is the code time. First, let's look at the Code results:


C ++ code (Z development? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vcGVuY3aw5rG + o7pvcGVuY3YzLjCjqaO6PC9wPg0KPHByZSBjbGFzcz0 =" brush: java; ">/*************************************** * ********* Copyright: zhuchenAuthor: zhuchenDate: 2016-01-10Description: multi-level haar wavelet transform ************************************ * *************/# include # Include Using namespace std; using namespace cv; int main () {Mat img = imread ("lenna.bmp", 0); int Height = img. cols; int Width = img. rows; int depth = 3; // define the decomposition depth int depthcount = 1; Mat tmp = Mat: ones (Width, Height, CV_32FC1); Mat wavelet = Mat :: ones (Width, Height, CV_32FC1); Mat imgtmp = img. clone (); imgtmp. convertize (imgtmp, CV_32FC1); while (depthcount <= depth) {Width = img. rows/depthcount; Height = img. cols/depthcount; for (int I = 0; I <Width; I ++) {for (int j = 0; j <Height/2; j ++) {tmp. at (I, j) = (imgtmp. (I, 2 * j) + imgtmp. (I, 2 * j + 1)/2; tmp. (I, j + Height/2) = (imgtmp. (I, 2 * j)-imgtmp. (I, 2 * j + 1)/2 ;}for (int I = 0; I <Width/2; I ++) {for (int j = 0; j <Height; j ++) {wavelet. at (I, j) = (tmp. (2 * I, j) + tmp. (2 * I + 1, j)/2; wavelet. (I + Width/2, j) = (tmp. (2 * I, j)-tmp. (2 * I + 1, j)/2 ;}} imgtmp = wavelet; depthcount ++;} namedWindow ("jpg", 0); wavelet. convertize (wavelet, CV_8UC1); wavelet + = 50; // image darkness is too low, so I added 50 imshow ("jpg", wavelet); waitKey (0 ); return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.