一,高斯金字塔--图片经过高斯+下采样
"""
高斯金字塔
"""
def gauss_pyramid():img = cv2.imread('./data/img4.png')lower_reso = cv2.pyrDown(img)lower_reso2 = cv2.pyrDown(lower_reso)plt.subplot(131), plt.imshow(img)plt.title('Input Image')plt.subplot(132), plt.imshow(lower_reso)plt.title('lower_1')plt.subplot(133), plt.imshow(lower_reso2)plt.title('lower_1')plt.show()
经过两次下采样的还原回去
与原图相比更加的模糊,因为丢失了高频信息。
二,拉普拉斯金字塔
由于高频细节信息在卷积和下采样中丢失,其保留所有层所丢失的高频信息,用于图像恢复,故可以将拉普拉斯保存的高频信息与高斯金字塔的放大图片融合
"""
图像融合
"""
def blend_image():A = cv2.imread('./data/apple.png')B = cv2.imread('./data/orange.png')b, g, r = cv2.split(A)A = cv2.merge([r, g, b])b, g, r = cv2.split(B)B= cv2.merge([r, g, b])A=cv2.resize(A,(240,240))B = cv2.resize(B, (240, 240))# print(A.shape)# print(B.shape)# generate Gaussian pyramid for AG = A.copy()gpA = [G]for i in range(6):G = cv2.pyrDown(G)gpA.append(G)# generate Gaussian pyramid for BG = B.copy()gpB = [G]for i in range(6):G = cv2.pyrDown(G)gpB.append(G)# generate Laplacian Pyramid for AlpA = [gpA[5]]# print(np.array(lpA).shape)for i in range(5, 0, -1):size = (gpA[i - 1].shape[1], gpA[i - 1].shape[0])GE = cv2.pyrUp(gpA[i],dstsize=size)L = cv2.subtract(gpA[i - 1], GE)lpA.append(L)# generate Laplacian Pyramid for BlpB = [gpB[5]]for i in range(5, 0, -1):size = (gpB[i - 1].shape[1], gpB[i - 1].shape[0])GE = cv2.pyrUp(gpB[i],dstsize=size)L = cv2.subtract(gpB[i - 1], GE)lpB.append(L)# Now add left and right halves of images in each levelLS = []for la, lb in zip(lpA, lpB):rows, cols, dpt = la.shapels = np.hstack((la[:, 0:cols // 2], lb[:, cols // 2:]))LS.append(ls)# now reconstructls_ = LS[0]for i in range(1, 6):size = (LS[i].shape[1], LS[i].shape[0])ls_ = cv2.pyrUp(ls_,dstsize=size)ls_ = cv2.add(ls_, LS[i])# image with direct connecting each halfreal = np.hstack((A[:, :cols // 2], B[:, cols // 2:]))plt.subplot(121)plt.imshow(ls_)plt.subplot(122)plt.imshow(real)plt.show()
打印结果:
左图是经过拉普拉斯金字塔的融合,右图是直接融合的。