Image pyramid is a kind of multi-scale representation of image, which is an effective and simple structure to interpret image with multi-resolution. A pyramid of an image is a collection of images arranged in pyramid shape with progressively reduced resolution and derived from the same original image.

The Gauss pyramid is a common image pyramid, as shown below:

The construction process of gauss pyramid is as follows:

  • Gaussian filtering is performed for layer I, and the Gaussian filtering kernel is as follows:

     

  • Delete even rows and even columns of the filtered image

After the above two steps, an image 1/4 the size of the original image is obtained, which completes a downsampling. A pyramid representation of an image can be obtained through repeated iterations.

Upsampling can be done in reverse to get an image 4 times larger.

  • Double the size of the original rows and fill the new rows with zeros
  • Gaussian filtering is performed on the enlarged image, and the filtering kernel is multiplied by 4

code

#include<opencv2/opencv.hpp>
using namespace cv;
using namespace std;
​
int main(a)
{
    Mat src = imread("G:\\opencvDemo\\lena.jpg");
    Mat dstimage;
    pyrDown(src, dstimage, Size(src.cols / 2, src.rows / 2));
    pyrUp(dstimage, dstimage, Size(dstimage.cols * 2, dstimage.rows * 2));
    imshow("srcImg", src);
    imshow("dstImg", dstimage);
    waitKey(0);
​
    return 0;
}
Copy the code
Copy the code

Zoom in on the original image and then zoom in.

The original:

Sampling image below:

Upsampling image:

OpenCV API

void cv::pyrDown    (   InputArray  src,
                        OutputArray     dst,
                        const Size &    dstsize = Size(),
                        int     borderType = BORDER_DEFAULT 
                        )SRC: input image DST: output image, which is of the same type as the input image DSTSize: output image Size. The default value is Size((src.cols+)1) /2, (src.rows+1) /2), and no matter how to set the output size must meet | dstsize. Width *2- the SRC. Cols | ≦2
    |dstsize.height*2- the SRC. Rows | ≦2BorderType: Border paddingCopy the code
void cv::pyrUp  (   InputArray  src,
                    OutputArray     dst,
                    const Size &    dstsize = Size(),
                    int     borderType = BORDER_DEFAULT 
                    )SRC: input image DST: output image, which is of the same type as the input image DSTSize: output image Size. The default value is Size((src.cols+)1) *2, (src.rows+1) *2), and no matter how to set the output size must meet | dstsize. Width - SRC. Cols *2| ≦ (dstsize. The width of the mod2)
    |dstsize.height-src.rows*2| ≦ (dstsize.height mod 2BorderType: Border paddingCopy the code

conclusion

It can be seen that the image will lose part of the image information after downsampling, and the up-sampling image and the original image have distortion.

There are many resize() and interpolation methods for image scaling, and image pyramid is mainly used for multi-scale representation of images. In the image recognition, matching and other aspects used more.