import cv2
import numpy as np
img1 = cv2.imread('salah.png')
img2 = cv2.imread('neymar.png')
def get_laplacian_pyramids_list(img,n):
#tạo các list kim tu thap laplacian
G = img.copy()
gp = [G]
for i in range(n):
G = cv2.pyrDown(G)
gp.append(G)
lp = [gp[n-1]]
for i in range(n-1,0,-1):
gauss_extend = cv2.pyrUp(gp[i],dstsize = (gp[i-1].shape[1],gp[i-1].shape[0]))
lap = cv2.subtract(gp[i-1],gauss_extend)
lp.append(lap)
return lp
lp_img1 = get_laplacian_pyramids_list(img1,6)
lp_img2 = get_laplacian_pyramids_list(img2,6)
# l = laplacian
LS = []
for lpA,lpO in zip(lp_img1,lp_img2):
rows,cols,la = lpA.shape
#ghep nua ben trai voi nua ben phai cua 2 h.a
ls = np.hstack((lpA[:,:cols//2],lpO[:,cols//2:]))
LS.append(ls)
#xây dựng lại hình ảnh
ls_ = LS[0]
#print(LS[1].shape)
for i in range(1,6):
ls_ = cv2.pyrUp(ls_,dstsize = (LS[i].shape[1],LS[i].shape[0]))
#print(ls_.shape)
#print(LS[i].shape)
ls_ = cv2.add(ls_, LS[i])
#ghep 2 hinh anh goc:
real = np.hstack((img1[:,:cols//2],img2[:,cols//2:]))
cv2.imshow('ls_',ls_)
cv2.imwrite('both.png',ls_)
cv2.waitKey(0)
cv2.destroyAllWindows()