OpenCV note finishing [Gaussian Pyramid & Laplacian Pyramid]

image pyramid

It is a collection of subimages with different resolutions of the same image, which is generated by continuously down-sampling the original image, and a low-resolution approximate image is generated from a high-resolution image.

1. Gaussian Pyramid: Irreversible Sampling

downsampling

import cv2

o=cv2.imread("lena.bmp",cv2.IMREAD_GRAYSCALE)

r1=cv2.pyrDown(o) # 对原图第一次向下采样
r2=cv2.pyrDown(r1) # 对第一次采样的图像再次向下采样
r3=cv2.pyrDown(r2) # 对第二次采样的图像再次向下采样
print("o.shape=",o.shape)
print("r1.shape=",r1.shape) # 打印采样后的图像分辨率
print("r2.shape=",r2.shape)
print("r3.shape=",r3.shape)
cv2.imshow("original",o)
cv2.imshow("r1",r1)
cv2.imshow("r2",r2)
cv2.imshow("r3",r3)

cv2.waitKey()
cv2.destroyAllWindows()

insert image description here
insert image description here

upsampling

import cv2

o=cv2.imread("lenas.bmp")

r1=cv2.pyrUp(o) # 向上采样
r2=cv2.pyrUp(r1) 
r3=cv2.pyrUp(r2)
print("o.shape=",o.shape)
print("r1.shape=",r1.shape)
print("r2.shape=",r2.shape)
print("r3.shape=",r3.shape)
cv2.imshow("original",o)
cv2.imshow("r1",r1)
cv2.imshow("r2",r2)
cv2.imshow("r3",r3)

cv2.waitKey()
cv2.destroyAllWindows()

insert image description here
insert image description here
Why is Gaussian pyramid sampling irreversible?
Here, the original image is used to subtract the image that was first down-sampled and then up-sampled, and look at the result. In theory, the result of the subtraction of the two images is 0 (that is, it is pitch black), which means that the two images are one picture.

import cv2

o=cv2.imread("lena.bmp")

down=cv2.pyrDown(o) # 先向下采样
up=cv2.pyrUp(down) # 再向上采样
diff=up-o   #构造diff图像,查看up与o的差异

cv2.imshow("original",o)
cv2.imshow("up",up)
cv2.imshow("difference",diff)

cv2.waitKey()
cv2.destroyAllWindows()

insert image description here
The result of the above subtraction is not 0, which confirms that downsampling through the Gaussian pyramid is not the inverse operation of upsampling, because after Gaussian filtering, the even-numbered row and column operation is discarded, and pixel information is inevitably lost.


2. Laplacian Pyramid: Reversible Sampling

In order to solve the above problem of losing pixel details, another sampling method - Laplacian pyramid is used here.

Definition form: Li = Gi - pyrUp (Gi + 1) The
simple understanding is: first downsample the Gaussian pyramid image of the current layer, then upsample, and finally use the current image to subtract the image after twice sampling

  • Li: Indicates the i-th layer in the Laplacian pyramid.
  • Gi: Indicates the i-th layer in the Gaussian pyramid.
import cv2
import numpy as np
import matplotlib.pyplot as plt

O=cv2.imread("lena.bmp")

# 高斯金字塔并向下采样
G0=O
Gd=cv2.pyrDown(G0)

# 高斯金字塔并向上采样
Gu=cv2.pyrUp(Gd)

# 得到拉普拉斯金字塔
L0=G0-Gu 

# 通过拉普拉斯图像复原
RG0=L0+Gu


print("G0.shape=",G0.shape)
print("RG0.shape=",RG0.shape)
result=RG0-G0  #将RG0和G0做减法

#计算result的绝对值,避免求和时负负为正(PS:3+(-3)=0)
result=abs(result)  

#计算result所有元素的和
print("原始图像G0与恢复图像RG0差值的绝对值和:",np.sum(result))  


# 使用pyplot显示图像
plt.figure(figsize=(10,10),dpi=100)
plt.subplot(1,3,1)
plt.title('G0')
plt.imshow(G0),plt.axis('off')

plt.subplot(1,3,2)
plt.title('L0')
plt.imshow(L0),plt.axis('off')

plt.subplot(1,3,3)
plt.title('RG0')
plt.imshow(RG0),plt.axis('off')

Running result:
insert image description here
Bye bye. . .

Guess you like

Origin blog.csdn.net/qq_34699535/article/details/120460565