Opencv-image pyramid-top sampling-bottom sampling

Source: Internet
Author: User

This article mainly realizes the upper sampling and lower Sampling operations for the input image, and uses the pyrup and pyrdown functions to perform the upper sampling and lower sampling for the image respectively.

The image pyramid is a collection of images that are continuously sampled from the source image until the operation is stopped.

Two common images are described as follows:

1. Gaussian pyramid: used for downsample images.

2. Laplace pyramid: Used to sample and reconstruct low-resolution images in the lower layer.

This article uses the Gaussian pyramid.

1) the Gaussian pyramid chart is shown below. The higher the image, the smaller the image resolution (size.

2) The Count of each layer starts from the underlying layer, that is, (I + 1). Note: GI + 1 is smaller than the resolution of GI layer!

3) The operations for generating the I + 1 layer are as follows:

First, convolution is performed with Gaussian core and GI layer. The size of Gaussian core is 16*16;

Then, remove each even row and column.

4) it is easy to see that the processing result is 1/4 of the previous layer. The entire pyramid can be generated from the process of recursion in G0 (original input image.

5) The processing process described above is suitable for the image downsampling operation.

What if we want to increase the image size?

First, increase each dimension of the image (generally the height and width) to double the original. Set the new even rows and columns to zero.

Then, use the Gaussian Kernel used above for convolution. Remember to multiply it by 4 to approach the lost pixel value.

The two processing processes described above are image downsampling and top sampling. In this article, we use pyrup and pyrdown functions to implement them.

The specific implementation code is as follows:

# Include "opencv2/imgproc. HPP "# include" opencv2/highgui. HPP "# include <math. h> # include <stdlib. h> # include <stdio. h> using namespace CV; // global variables mat SRC, DST, TMP; const char * window_name = "pyramids Demo"; int main (void) {// function information printf ("\ n zoom in-out demo \ n"); printf ("------------------ \ n "); printf ("* [u]-> zoom in \ n"); printf ("* [d]-> zoom out \ n"); printf ("* [ESC] -> Close program \ n "); // The test image must be 2 ^ (n) src = imread (" D: \ lena.bmp "); If (! SRC. Data) {printf ("no data! -- Exiting the program \ n "); Return-1;} TMP = SRC; DST = TMP; // exchange Image Information // create an output display window namedwindow (window_name, cv_window_autosize ); imshow (window_name, DST); // cyclic operation for (;) {int C; C = waitkey (10); If (char) C = 27) // Press ESC on the keyboard to exit the program {break;} If (char) C = 'U ') // press "U" on the keyboard to perform the sampling operation {// TMP: original image, DST: output image, twice the original image, // size: the size of the DST image. The size of rows and columns is doubled. cols * 2, TMP. rows * 2); printf ("** zoom in: Image x 2 \ n");} else if (char) C = 'D ') // press "D" on the keyboard to perform the downsample operation {pyrdown (TMP, DST, size (TMP. cols/2, TMP. rows/2); printf ("** zoom out: image/2 \ n");} // create the output display window imshow (window_name, DST); TMP = DST; // update the Temp value for better recursive operations} 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.