案例来源于© Fu Xianjun. All Rights Reserved
一高斯金字塔
导包
import cv2
import numpy as np
高斯金字塔
#高斯金字塔
def pyramid_demo(image,level):
temp = image.copy()
pyramid_images = []
for i in range(level):
dst = cv2.pyrDown(temp)
pyramid_images.append(dst)
cv2.imshow("pyramid_down_"+str(i), dst)
temp = dst.copy()
return pyramid_images[level-1]
演示效果
src = cv2.imread("lena.jpg")
cv2.imshow("input image", src)
pyramid_demo(src,4)
cv2.waitKey(0)
cv2.destroyAllWindows()
二拉普拉斯金字塔¶的构建
# 拉普拉斯金字塔构建
G0 = cv2.imread("lena.bmp")
cv2.imshow("input image",G0)
G1=cv2.pyrDown(G0)
G2=cv2.pyrDown(G1)
G3=cv2.pyrDown(G2)
G4=cv2.pyrDown(G3)
L0 = cv2.subtract(G0,cv2.pyrUp(G1))
L1 = cv2.subtract(G1,cv2.pyrUp(G2))
L2 = cv2.subtract(G2,cv2.pyrUp(G3))
L3 = cv2.subtract(G3,cv2.pyrUp(G4))
cv2.imshow("G1",G1)
cv2.imshow("G2",G2)
cv2.imshow("G3",G3)
cv2.imshow("G4",G4)
cv2.waitKey(0)
cv2.destroyAllWindows()
三利用金字塔实现图像拼接
import cv2
import numpy as np
A = cv2.imread('apple.png')
A = cv2.resize(A,(256,256),interpolation=cv2.INTER_CUBIC)
B = cv2.imread('orange.png')
B = cv2.resize(B,(256,256),interpolation=cv2.INTER_CUBIC)
# 生成高斯金字塔
G = A.copy()
gpA = [G]
for i in range(5):
G = cv2.pyrDown(G)
gpA.append(G)
G = B.copy()
gpB = [G]
for i in range(5):
G = cv2.pyrDown(G)
gpB.append(G)
# 产生Laplacian金字塔
lpA = [gpA[5]]
for i in range(5,0,-1):
GE = cv2.pyrUp(gpA[i])
L = cv2.subtract(gpA[i-1],GE)
lpA.append(L)
lpB = [gpB[5]]
for i in range(5,0,-1):
GE = cv2.pyrUp(gpB[i])
L = cv2.subtract(gpB[i-1],GE)
lpB.append(L)
# 合并
LS = []
for la,lb in zip(lpA,lpB):
rows,cols,dpt = la.shape
ls = np.hstack((la[:,0:cols//2], lb[:,cols//2:]))
LS.append(ls)
# 重新构建图像
ls_ = LS[0]
for i in range(1,6):
ls_ = cv2.pyrUp(ls_)
ls_ = cv2.add(ls_, LS[i])
# 连接
real = np.hstack((A[:,:cols//2],B[:,cols//2:]))
cv2.imshow("apple",A)
cv2.imshow("orange",B)
cv2.imshow("LS",ls_)
cv2.imshow("Real",real)
cv2.waitKey()
cv2.destroyAllWindows()
结果