代码:
# -*- coding=GBK -*-
# coding:utf-8
import cv2 as cv
import numpy as np
# 高斯金字塔和拉普拉斯金字塔的原理和python构建:https://zhuanlan.zhihu.com/p/94014493
# 高斯金字塔
def pyramid_image(image):
level = 3 # 金字塔的层数
temp = image.copy() # 拷贝图像
pyramid_images = []
for i in range(level):
dst = cv.pyrDown(temp)
pyramid_images.append(dst)
cv.namedWindow(f"Gaussian pyramid{str(i)}", 0) # 图片尺寸
cv.imshow("Gaussian pyramid" + str(i), dst)
temp = dst.copy()
return pyramid_images
# 拉普拉斯金字塔
def laplian_image(image):
pyramid_images = pyramid_image(image)
level = len(pyramid_images)
for i in range(level - 1, -1, -1):
if (i - 1) < 0:
expand = cv.pyrUp(pyramid_images[i], dstsize = image.shape[:2])
lpls = cv.subtract(image, expand)
lpls = np.power(lpls / float(np.max(lpls)), 1 / 1.5) # 伽马矫正
cv.namedWindow(f"Laplace{str(i)}", 0) # 图片尺寸
cv.imshow("Laplace" + str(i), lpls)
else:
expand = cv.pyrUp(pyramid_images[i], dstsize = pyramid_images[i - 1].shape[:2])
lpls = cv.subtract(pyramid_images[i - 1], expand)
lpls = np.power(lpls / float(np.max(lpls)), 1 / 1.5) # 伽马矫正
cv.namedWindow(f"Laplace{str(i)}", 0) # 图片尺寸
cv.imshow("Laplace" + str(i), lpls)
src = cv.imread("fengling_2^n.png")
# cv.namedWindow("fengling_2^n.png",0)#图片尺寸
cv.imshow("fengling_2^n.png", src)
laplian_image(src)
cv.waitKey(0)
cv.destroyAllWindows()
结果:
备注:
高斯金字塔和拉普拉斯金字塔原理和构建:https://zhuanlan.zhihu.com/p/94014493