[OpenCV in C++] Lesson 8 - Common Operations of OpenCV Images (5): Image Morphology - The Use and Principle of Image Pyramids (Gaussian pyramid, Laplacian pyramid) and Up (Down) Sampling


Previous link: [C++'s OpenCV] Lesson 7 - Common Operations of OpenCV Images (4): Image Morphology - Principles of Image Erosion and Diffusion


1. Image Pyramid

1.1 What is an image pyramid

  • Concept: The image pyramid is a kind of multi-scale representation of images, and it is an effective but conceptually simple structure to explain images at multiple resolutions. The image pyramid of an image is a set of image resolutions that are gradually reduced in a pyramid shape (bottom-up) and originate from the same original image. It is obtained by down-sampling in steps, and the sampling is stopped until a certain termination condition is reached. We compare layer-by-layer images to a pyramid. The higher the level, the smaller the image and the lower the resolution.
  • Significance: Image pyramids have been widely used in the fields of iconography and image processing, such as: image scaling, image reconstruction, image fusion (multi-resolution tower image fusion algorithm is a more commonly used image fusion method), image enhancement technology etc. At the same time, the image pyramid is also the basis of many iconography.
  • Graphic:
    insert image description here
  • Classification:
    • Gaussian Pyramid : Through Gaussian Blur Filtering + Downsampling (Original Link: "[C++'s OpenCV] Lesson 5 - OpenCV Image Common Operations (2): OpenCV's Basic Drawing, Smoothing Filter (Blur) Processing" in the article "Image Smoothing filter processing" ) continuously reduces the size of the image to form a pyramid of the previous image, and then includes a group of images with multiple resolutions in the pyramid.

    • Laplacian Pyramid : The Laplacian Pyramid is actually a pyramid that exists to realize Gaussian Pyramid image reconstruction (that is, a pyramid based on the implementation of the Gaussian Pyramid), and the image of each layer is actually a picture of the same layer of the Gaussian Pyramid. The difference image with the image of the next layer of the Gaussian pyramid , that is, the Laplacian pyramid is the difference pyramid. Upsample the small image in the pyramid to get the full large high-resolution image
      insert image description here

2. Upsampling of images

2.1 Concept

Used to help calculate the Laplacian pyramid

2.2 Function pyrUp()

Original description: The function performs the upsampling step of the Gaussian pyramid construction, though it can actually be used to construct the Laplacian pyramid. First, it upsamples the source image by injecting even zero rows and columns and then convolves the result with the s ame kernel as in pyrDown multiplied by 4"
Original translation: This function performs the upsampling step of Gaussian pyramid construction, although it can actually be used to construct Laplacian pyramids. First, it multiplies the source by injecting even zero rows and columns The image is up-sampled, and then the result is multiplied by the kernel in pyrDown.
From the above description, we can see that the up-sampling operation of the function pyrUp() is to add 0 values ​​evenly every row and every column . After the up-sampling is completed, use the following volume The product kernel performs a Gaussian convolution blur filter operation.


void cv::pyrUp	(	
					InputArray 	src,
					OutputArray 	dst,
					const Size & 	dstsize = Size(),
					int 	borderType = BORDER_DEFAULT 
)	
  • Explanation of function parameters:
    • src: source image (upper layer source image or original image)
    • dst: the image after downsampling
    • dstsize: The size of the output image, which is a Size() object, which defaults to an object of empty size and needs to be specified.
    • borderType: image border type, generally keep the default, if you are interested, you can learn about: Selection parameter table of BorderTypes
  • Example:
#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc.hpp"

using namespace std;
using namespace cv;

int main(void)
{
    
    
	Mat srcImg = imread("/home/aelx-chen/demo.jpg");
	Mat dstImg;

	pyrUp(srcImg, dstImg, Size((srcImg.cols)*2,(srcImg.rows)*2), BORDER_DEFAULT);

	imshow("src", srcImg);
	imshow("dstImg for first time", dstImg);

	waitKey(0);
	return 0;
}

insert image description here

3. Downsampling of images

3.1 Concept

        Downsampling is "shrinking the picture", which is applied to the Gaussian pyramid (please read carefully about the introduction above!)

3.2 Function pyrDown()

Function original text: function prototype

#include <opencv2/imgproc.hpp>

void cv::pyrDown(
					InputArray 	src,
					OutputArray 	dst,
					const Size & 	dstsize = Size(),
					int 	borderType = BORDER_DEFAULT 
				)	
  • The function description in the original text: Blurs an image and downsamples it.
    Translated as: Blurs an image and downsamples it. (Personal understanding: realize the approximate picture of each layer of the Gaussian pyramid)

  • Explanation of function parameters:

    • src: source image (upper layer source image or original image)
    • dst: the image after downsampling
    • dstsize: The size of the output image, which is a Size() object, which defaults to an object of empty size and needs to be specified.
    • borderType: image border type, generally keep the default, if you are interested, you can learn about: Selection parameter table of BorderTypes
  • Example:

#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/opencv.hpp"
#include "opencv2/imgproc.hpp"

using namespace std;
using namespace cv;

int main(void)
{
    
    
	Mat srcImg = imread("/home/aelx-chen/demo.jpg");
	Mat dstImg;

	pyrDown(srcImg, dstImg, Size((srcImg.cols + 1)/2,(srcImg.rows + 1)/2), BORDER_DEFAULT);

	imshow("src", srcImg);
	imshow("dstImg for first time", dstImg);
	//可以多次迭代使用pyrDown(),将每次的结果存储进某个容器例如Vector中形成高斯金字塔,
	//其中包含每层的图片,可以每层结合不同的处理算法完成图像处理,
	//最后融合这些图片形成自己的新图片。

	waitKey(0);
	return 0;
}

insert image description here

Guess you like

Origin blog.csdn.net/weixin_43520503/article/details/129281733