由第一个图可知,图像金字塔这无非就是对图像进行放大和缩小罢了
1,高斯金字塔
向下采样方法(缩小),越采样越小,即从金字塔底部向上采样
cv2.pyrDown(img)
向上采样方法(放大),越采样越大,即从金字塔顶部向下采样
cv2.pyrUp(img)
import cv2
import numpy as npdef show_photo(name,picture):cv2.imshow(name,picture)cv2.waitKey(0)cv2.destroyAllWindows()#原图
img = cv2.imread('E:\Jupyter_workspace\study\data/test2.png')
show_photo('img',img)
print(img.shape)#结果为:(260, 200, 3)#放大
up = cv2.pyrUp(img)
show_photo('up',up)
print(up.shape)#结果为:(520, 400, 3)#缩小
down = cv2.pyrDown(img)
show_photo('down',down)
print(down.shape)#结果为:(130, 100, 3)#先放大再缩小
up = cv2.pyrUp(img)
up_down = cv2.pyrDown(up)
show_photo('up_down',up_down)
print(up_down.shape))#结果为:(260, 200, 3)#先缩小再放大
down = cv2.pyrDown(img)
down_up = cv2.pyrUp(down)
show_photo('down_up',down_up)
print(down_up.shape)#结果为:(260, 200, 3)#将原图、先放大再缩小、先缩小再放大进行比较
res = np.hstack((img,up_down,down_up))#vstack纵向比较
show_photo('img up_down down_up',res)
效果如下:
原图、先放大再缩小、先缩小再放大进行比较
虽然先放大再缩小或者先缩小再放大都和原图大小一样,但是实则清晰度已经变了,已经丢了很多数据了
2,拉普拉斯金字塔
Gi为原图像数据,即img
PyrUp(PyrDown(Gi))对原图像先up再down
然后用原图像Gi减去PyrUp(PyrDown(Gi))
首先,G0为原图先进行低通滤波和缩小尺寸操作得到G1,G1再进行放大尺寸得到E1,然后,G0-E1得到LP1,即与上述公式效果一致
cv2.pyrDown(img)
缩小
cv2.pyrUp(img)
放大
import cv2
import numpy as npdef show_photo(name,picture):cv2.imshow(name,picture)cv2.waitKey(0)cv2.destroyAllWindows()img = cv2.imread('E:\Jupyter_workspace\study\data/test2.png')
down = cv2.pyrDown(img)
down_up = cv2.pyrUp(down)#先缩小再放大
lpls = img-down_up
show_photo('lpls',lpls)