原文
高斯金字塔G0层下采样后为G1,用G0减去G1的上采样,就得到了拉普拉斯层L0.
高斯金字塔G1层上采样后与拉普拉斯金字塔L0层相加后就得到了G0层。
import numpy as np
import cv2 as cv
from matplotlib import pyplot as pltimg = cv.imread('left_01.png', cv.IMREAD_GRAYSCALE)#彩色图转灰度图
Gus0 = img#高斯层
Gus1 = cv.pyrDown(Gus0)#对图像进行模糊并下采样
Gus2 = cv.pyrDown(Gus1)
Gus3 = cv.pyrDown(Gus2)
pyrUp1=cv.pyrUp(Gus1)#对高斯图1进行上采样
if(pyrUp1.shape!=Gus0.shape):#假如上采样后的图片尺寸与高斯图像尺寸不一致。则对上采样图像进行裁剪。使它们尺寸相等pyrUp1 = pyrUp1[0:Gus0.shape[0],0:Gus0.shape[1]]
Lap0 = Gus0 - pyrUp1#拉普拉斯层
cv.imshow('Lap0',Lap0)
cv.waitKey(0)
cv.destroyAllWindows()pyrUp2=cv.pyrUp(Gus2)#对高斯图2进行上采样
if(pyrUp2.shape!=Gus1.shape):#假如上采样后的图片尺寸与高斯图像尺寸不一致。则对上采样图像进行裁剪。使它们尺寸相等pyrUp2 = pyrUp2[0:Gus1.shape[0],0:Gus1.shape[1]]
Lap1 = Gus1 - pyrUp2#拉普拉斯层
cv.imshow('Lap1',Lap1)
cv.waitKey(0)
cv.destroyAllWindows()pyrUp3=cv.pyrUp(Gus3)#对高斯图3进行上采样
if(pyrUp3.shape!=Gus2.shape):#假如上采样后的图片尺寸与高斯图像尺寸不一致。则对上采样图像进行裁剪。使它们尺寸相等pyrUp3 = pyrUp3[0:Gus2.shape[0],0:Gus2.shape[1]]
Lap2 = Gus2 - pyrUp3#拉普拉斯层
cv.imshow('Lap2',Lap2)
cv.waitKey(0)
cv.destroyAllWindows()prGus = pyrUp3
for i in range(2):#对拉普拉斯层第三层进行2次上采样prGus = cv.pyrUp(prGus)plt.figure(0)
plt.subplot(221), plt.imshow(img, 'gray'), plt.title('Org/Gus0'), plt.axis('off')
plt.subplot(222), plt.imshow(Gus1, 'gray'), plt.title('Gus1'), plt.axis('off') # 为了效果明显 我们选用第1层高斯
plt.subplot(223), plt.imshow(pyrUp1, 'gray'), plt.title('pyrUp1'), plt.axis('off') # 如果我们直接上采样
plt.subplot(224), plt.imshow(Lap2, 'gray'), plt.title('LAP2'), plt.axis('off')
plt.show()plt.figure(1)
# rep = Lap0 + cv.pyrUp(Lap1 + cv.pyrUp(Lap2 + pyrUp3))
rep = cv.pyrUp((Lap2+pyrUp3))#拉普拉斯层加上上采样层,就能恢复原来的原始图像
if(rep.shape!=Lap1.shape):#调整尺寸,使上采样的图尺寸与拉普拉斯层的尺寸一致rep = rep[0:Lap1.shape[0],0:Lap1.shape[1]]rep = cv.pyrUp((Lap1+rep))
if(rep.shape!=Lap0.shape):#调整尺寸,使上采样的图尺寸与拉普拉斯层的尺寸一致rep = rep[0:Lap0.shape[0],0:Lap0.shape[1]]
rep = Lap0+rep
plt.subplot(121), plt.imshow(img, 'gray'), plt.title('Org/Gus0'), plt.axis('off')
plt.subplot(122), plt.imshow(rep, 'gray'), plt.title('LapToRestore'), plt.axis('off')
plt.show()