高斯金字塔与拉普拉斯金字塔(python实现)

一、高斯金字塔:
高斯金子塔的思路非常简单,就是将原始图像当作金子塔的最底层,然后进行按图像长宽各减少二分之一,面积减少四分之一,进行下采样。在进行下采样之前需要进行高斯滤波。

import numpy as np
import cv2
import matplotlib.pyplot as plt
def gaussian(original_image,down_times=5):
    temp = original_image.copy()
    gaussian_pyramid = [temp]
    for i in range(down_times):
        temp = cv2.pyrDown(temp)
        gaussian_pyramid.append(temp)
    return gaussian_pyramid
if __name__ == "__main__":
    a = cv2.imread("DR_ck_0001.tiff", -1)
    gaussian_pyramid = gaussian(a, down_times=5)
    plt.subplot(2, 3, 1), plt.imshow(a, cmap='gray')
    plt.subplot(2, 3, 2), plt.imshow(gaussian_pyramid[2], cmap='gray')
    plt.subplot(2, 3, 3), plt.imshow(gaussian_pyramid[4], cmap='gray')
    plt.show()
    print(gaussian_pyramid[0].shape, gaussian_pyramid[1].shape, gaussian_pyramid[5].shape)

二、拉普拉斯金字塔
拉普拉斯金子塔可以理解残差金子塔,它是用来存储下采样后的图像于原始图像的差异的。这里需要进行一下介绍,假如原始图像为 G0,金字塔中任意一层图像为 Gi,下采样后得到 Gi+1 = Down(Gi), 由于图像下采样之后尺寸发生了变化,若将下采样后的图进行上采用得到 Up(Down(Gi)), 但是下采样后的变化是不可逆的即不可能恢复成原始图像。因此,有部分信息丢失不能完全恢复成原始图像。
拉普拉斯金字塔就是记录金字塔每一层下采样后再上采用回来与该层之间的差异的。
可以记为:Li = Gi - Up(Down(Gi))
这里还需要做一些叙述:
下面是拉普拉斯金子塔第i层额数学定义:
高斯金字塔与拉普拉斯金字塔(python实现)_第1张图片

import numpy as np
import cv2
import matplotlib.pyplot as plt
def gaussian(original_image,down_times):
    temp = original_image.copy()
    gaussian_pyramid = [temp]
    for i in range(down_times):
        temp = cv2.pyrDown(temp)
        gaussian_pyramid.append(temp)
    return gaussian_pyramid
def laplacian(gaussian_pyramid, up_times):
    laplacian_pyramid = [gaussian_pyramid[-1]]
    for i in range(up_times, 0, -1):
        print(i)
        temp_pyrUp = cv2.pyrUp(gaussian_pyramid[i])
        temp_lap = cv2.subtract(gaussian_pyramid[i-1], temp_pyrUp)
        laplacian_pyramid.append(temp_lap)
    return laplacian_pyramid
if __name__ == "__main__":
    a = cv2.imread("DR_ck_0001.tiff", -1)
    gaussian_pyramid = gaussian(a, down_times=5)
    laplacian_pyramid = laplacian(gaussian_pyramid, up_times=5)
    plt.subplot(2, 3, 1), plt.imshow(a, cmap='gray')
    plt.subplot(2, 3, 2), plt.imshow(gaussian_pyramid[2], cmap='gray')
    plt.subplot(2, 3, 3), plt.imshow(gaussian_pyramid[4], cmap='gray')
    plt.subplot(2, 3, 4), plt.imshow(laplacian_pyramid[0], cmap='gray')
    plt.subplot(2, 3, 5), plt.imshow(laplacian_pyramid[1], cmap='gray')
    plt.subplot(2, 3, 6), plt.imshow(laplacian_pyramid[5], cmap='gray')
    plt.show()
    print(gaussian_pyramid[0].shape, len(gaussian_pyramid), len(laplacian_pyramid))

你可能感兴趣的