一、原理
reduce=高斯模糊+降采样
expand=扩大+卷积
PyrDown:降采样
PyrUp:还原
二、高斯金字塔
import cv2
import numpy as np
from matplotlib import pyplot as pltdef pyramid(image):level = 3temp = image.copy()pyramid_image = []for i in range(level):dst = cv2.pyrDown(temp)pyramid_image.append(dst)cv2.imshow("pyramid_down_"+str(i),dst)temp = dst.copy()return pyramid_imagesrc = cv2.imread(r"G:\Juptyer_workspace\study\opencv\opencv3\cat.png")
cv2.imshow("image",src)
cv2.namedWindow("image",cv2.WINDOW_AUTOSIZE)
pyramid(src)
cv2.waitKey(0)
cv2.destroyAllWindows()
效果图如下:
三、拉普拉斯金字塔
拉普拉斯金字塔对图像大小有要求,必须是2的次方倍。
import cv2
import numpy as np
from matplotlib import pyplot as pltdef pyramid(image):level = 3temp = image.copy()pyramid_image = []for i in range(level):dst = cv2.pyrDown(temp)pyramid_image.append(dst)cv2.imshow("pyramid_down_"+str(i),dst)temp = dst.copy()return pyramid_imagedef lapalian(image):pyramid_images = pyramid(image)#pyramid_images = pyramid_images.resize(512*512)level = len(pyramid_images)for i in range(level-1,-1,-1):if (i-1) <0:expand = cv2.pyrUp(pyramid_images[i],dstsize=image.shape[:2])lpls = cv2.subtract(image,expand)cv2.imshow("lapalian_down_"+str(i),lpls)else:expand = cv2.pyrUp(pyramid_images[i],dstsize=pyramid_images[i-1].shape[:2])lpls = cv2.subtract(pyramid_images[i-1],expand)cv2.imshow("lapalian_down_"+str(i),lpls)src = cv2.imread(r"G:\Juptyer_workspace\study\opencv\opencv3\999.jpg")
cv2.imshow("image",src)
cv2.namedWindow("image",cv2.WINDOW_AUTOSIZE)
lapalian(src)
cv2.waitKey(0)
cv2.destroyAllWindows()
效果图如下: