代码实战:图像融合
我将肯巴.沃克和约翰.沃尔融合在了一起。
具体见注释。
import numpy as np
import cv2
import scipy.ndimage as ndimage
img = cv2.imread("1.png")
img1 = cv2.imread("2.png")
img=cv2.resize(img,(192,192))
img1=cv2.resize(img1,(192,192))
#降采样次数
step=3
#高斯金字塔计算
girl1=img.copy()
gp1=[girl1]
for i in range(step):
girl1=cv2.pyrDown(girl1)
gp1.append(girl1)
girl2=img1.copy()
gp2=[girl2]
for i in range(step):
girl2=cv2.pyrDown(girl2)
gp2.append(girl2)
#拉普拉斯金字塔计算
lp1=[gp1[step]]
for i in range(step):
GE=cv2.pyrUp(gp1[step-i])
L=cv2.subtract(gp1[step-i-1],GE)
lp1.append(L)
lp2=[gp2[step]]
for i in range(step):
GE=cv2.pyrUp(gp2[step-i])
L = cv2.subtract(gp2[step - i - 1], GE)
lp2.append(L)
#合并
merges=[]
for i in range(step+1):
w,h,d=lp1[i].shape
merge=np.hstack((lp1[i][:,0:w//2-10//2**i],lp2[i][:,w//2-10//2**i:]))
merges.append(merge)
#将合并的图像进行拉普拉斯金字塔法拼接
merge=merges[0]
for i in range(step):
merge=cv2.pyrUp(merge)
merge=cv2.add(merge,merges[i+1])
cv2.imshow("0",img)
cv2.imshow("1",img1)
cv2.imshow("2",merge)
cv2.waitKey(0)
拉普拉斯金字塔就是记录高斯金字塔每一级下采样后再上采样与下采样前的差异,目的是为了能够完整的恢复出每一层级的下采样前图像。下面的公式就是前面的差异记录过程:
step=3
step=5
好像效果变好了?