OpenCV Foundation 46-Image Pyramid 02-Laplacian Pyramid

Earlier we introduced the Gaussian pyramid, which is generated by a series of downsampling of an image. Sometimes, we want to obtain a complete large-scale high-resolution image by upsampling the small image in the pyramid, then we need to use the Laplacian pyramid

We have already introduced earlier that after an image has been down-sampled and then up-sampled, it cannot be restored to its original state. In this regard, we also use the program to verify. Upsampling is not the inverse of downsampling. This is obvious, because the even-numbered rows and even-numbered columns are discarded after the Gaussian filter is used for downsampling, and some information will inevitably be lost.

In order to restore the original image with a higher resolution when upsampling, it is necessary to obtain the information lost in the sampling process, and the lost information constitutes the Laplacian pyramid.

The definition form of the Laplacian pyramid is:

Li = Gi - pyrUp(Gi + 1)

In the formula:

  • Li represents the i-th level in the Laplacian pyramid.
  • Gi represents the i-th layer in the Gaussian pyramid.

The i-th level in the Laplacian pyramid is equal to the difference between "the i-th level in the Gaussian pyramid" and "the upsampled result of the i+1-th level in the Gaussian pyramid". Figure 11-11 shows the correspondence between the Gaussian pyramid and the Laplacian pyramid.

insert image description here

Code example: Construct a Laplacian pyramid using the functions cv2.pyrDown() and cv2.pyrUp().


import cv2
O=cv2.imread("lena.png")
G0=O
G1=cv2.pyrDown(G0)
G2=cv2.pyrDown(G1)
G3=cv2.pyrDown(G2)
L0=G0-cv2.pyrUp(G1)
L1=G1-cv2.pyrUp(G2)
L2=G2-cv2.pyrUp(G3)
print("L0.shape=",L0.shape)
print("L1.shape=",L1.shape)
print("L2.shape=",L2.shape)
cv2.imshow("L0",L0)
cv2.imshow("L1",L1)
cv2.imshow("L2",L2)
cv2.waitKey()
cv2.destroyAllWindows()

After the program runs, it will output the following running results:

L0.shape= (512, 512, 3)
L1.shape= (256, 256, 3)
L2.shape= (128, 128, 3)

insert image description here

1 The figure in the middle is the 0th layer L0 of the Laplacian pyramid obtained by subtracting the "up-sampling result of image G1" from "original image G0" through the statement "L0=G0-cv2.pyrUp(G1)". The figure on the right is the first layer L1 of the Laplacian pyramid obtained
by subtracting the "up-sampling result of image G2" from "image G1" by the statement "L1=G1-cv2.pyrUp(G2)". The small picture on the left is the second layer L2 of the Laplacian pyramid obtained by subtracting the "up-sampling result of image G3" from "image G2" by the statement "L2=G2-cv2.pyrUp(G3)".


How to restore the original image?

The role of the Laplacian pyramid is to restore high-resolution images. Figure 11-13 demonstrates how to restore a high-resolution image through a Laplacian pyramid. Among them, the right figure is a simplification of the left figure.

insert image description here

The meanings of the symbols in Figure 11-13 are as follows:

  1. G0, G1, G2, and G3 are the 0th, 1st, 2nd, and 3rd layers of the Gaussian pyramid, respectively.
  2. L0, L1, and L2 are the 0th, 1st, and 2nd layers of the Laplacian pyramid, respectively.
  3. The downward arrow indicates the downsampling operation (corresponding to the cv2.pyrDown() function).
  4. The arrow to the upper right indicates the upsampling operation (corresponding to the cv2.pyrUp() function).
  5. The plus sign "+" indicates an addition operation.
  6. The minus sign "-" indicates a subtraction operation.

In Figure 11-13, the described operations and relationships are:

  1. Downsampling (composition of Gaussian pyramid)
G1=cv2.pyrDown(G0)
G2=cv2.pyrDown(G1)
G3=cv2.pyrDown(G2)
  1. Laplace Pyramid
L0=G0-cv2.pyrUp(G1)
L1=G1-cv2.pyrUp(G2)
L2=G2-cv2.pyrUp(G3)
  1. Upsampling to restore high-resolution images
G0=L0+cv2.pyrUp(G1)
G1=L1+cv2.pyrUp(G2)
G2=L2+cv2.pyrUp(G3)

The above relationship is derived by mathematical operation. For example, it is known that L0=G0cv2.pyrUp(G1), and the expression G0=L0+cv2.pyrUp(G1) is obtained by moving cv2.pyrUp(G1) on the right side of the expression to the left side. In addition, both G1 and G2 can be obtained through the construction expressions of the Laplacian pyramid. As mentioned earlier, the purpose of constructing the Laplacian pyramid is to restore high-resolution images.

Code example:

import cv2
import numpy as np
O=cv2.imread("lena.png")
#==============生成高斯金字塔===================
G0=O
G1=cv2.pyrDown(G0)
G2=cv2.pyrDown(G1)
G3=cv2.pyrDown(G2)
#============生成拉普拉斯金字塔===================
L0=G0-cv2.pyrUp(G1) #拉普拉斯金字塔第 0 层
L1=G1-cv2.pyrUp(G2) #拉普拉斯金字塔第 1 层
L2=G2-cv2.pyrUp(G3) #拉普拉斯金字塔第 2 层
#=================复原 G0======================
RG0=L0+cv2.pyrUp(G1) #通过拉普拉斯图像复原的原始图像 G0


cv2.imshow("L0",L0)
cv2.imshow("L1",L1)
cv2.imshow("L2",L2)
cv2.imshow("RG0",RG0)
cv2.waitKey()
cv2.destroyAllWindows()

insert image description here

Guess you like

Origin blog.csdn.net/hai411741962/article/details/132163769