图像金字塔操作,又分为高斯金字塔和拉普拉斯金字塔,高斯金字塔简单的理解为图像的downsample和upsample.
拉普拉斯金字塔是图像A - up(down(A))的操作,看上去全是边缘。
#!/usr/bin/env python
import cv2
img = cv2.imread('frame.jpg')
cv2.imshow('src', img)
lower_reso= cv2.pyrDown(img)
print lower_reso.shape
cv2.imshow('downsample', lower_reso)
higher_reso = cv2.pyrUp(lower_reso)
print higher_reso.shape
cv2.imshow('upsample', higher_reso)
cv2.imshow('laplace', img - higher_reso)
cv2.waitKey(0)
两张图像直接拼接和高斯laplace金字塔融合比较:
#!/usr/bin/env python
import cv2
import numpy as np
import sys
A = cv2.imread('frame.jpg')
B = cv2.imread('book.jpg')
#generate gaussian pyramid for A
G=A.copy()
gpA=[G]
for i in xrange(6):
G=cv2.pyrDown(G)
gpA.append(G)
#generate gaussian pyramid for B
G=B.copy()
gpB=[G]
for i in xrange(6):
G=cv2.pyrDown(G)
gpB.append(G)
#generate laplace pyramid for A
lpA = [gpA[5]]
for i in xrange(5, 0, -1):
GE = cv2.pyrUp(gpA[i])
L = cv2.subtract(gpA[i-1], GE)
lpA.append(L)
#generate laplace pyramid for B
lpB = [gpB[5]]
for i in xrange(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 xrange(1,6):
ls_ = cv2.pyrUp(ls_)
ls_ = cv2.add(ls_, LS[i])
real = np.hstack((A[:,:cols/2],B[:,cols/2:]))
cv2.imshow('real', real)
cv2.imshow('lap blend', ls_)
cv2.waitKey(0)