-
Notifications
You must be signed in to change notification settings - Fork 42
/
blending.py
59 lines (46 loc) · 1.51 KB
/
blending.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#!/usr/bin/python
import numpy as np
import cv2
def GaussianPyramid(img, leveln):
GP = [img]
for i in range(leveln - 1):
GP.append(cv2.pyrDown(GP[i]))
return GP
def LaplacianPyramid(img, leveln):
LP = []
for i in range(leveln - 1):
next_img = cv2.pyrDown(img)
LP.append(img - cv2.pyrUp(next_img, img.shape[1::-1]))
img = next_img
LP.append(img)
return LP
def blend_pyramid(LPA, LPB, MP):
blended = []
for i, M in enumerate(MP):
blended.append(LPA[i] * M + LPB[i] * (1.0 - M))
return blended
def reconstruct(LS):
img = LS[-1]
for lev_img in LS[-2::-1]:
img = cv2.pyrUp(img, lev_img.shape[1::-1])
img += lev_img
return img
def multi_band_blending(img1, img2, mask, leveln=6):
max_leveln = int(np.floor(np.log2(min(img1.shape[0], img1.shape[1],
img2.shape[0], img2.shape[1]))))
if leveln is None:
leveln = max_leveln
if leveln < 1 or leveln > max_leveln:
print "warning: inappropriate number of leveln"
leveln = max_leveln
# Get Gaussian pyramid and Laplacian pyramid
MP = GaussianPyramid(mask, leveln)
LPA = LaplacianPyramid(img1.astype(np.float64), leveln)
LPB = LaplacianPyramid(img2.astype(np.float64), leveln)
# Blend two Laplacian pyramidspass
blended = blend_pyramid(LPA, LPB, MP)
# Reconstruction process
result = reconstruct(blended)
result[result > 255] = 255
result[result < 0] = 0
return result