/
E27_project2.py
192 lines (133 loc) · 3.99 KB
/
E27_project2.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
"""
Rachel Diamond and Julie Harris
Project 2: Image blending
OLD VERSION--> merged with short.py in file E27_project02.py
"""
import cv2
import numpy
NUM_PYR = 3 #number of iterartions in the pyramid
COLOR = (0, 0, 0)
win = 'Image Window'
cv2.namedWindow(win)
########################
def pyr_build(Gi):
lp = []
for i in range (NUM_PYR):
#scale it down
h = Gi.shape[0]
w = Gi.shape[1]
Gi1 = cv2.pyrDown(Gi)
print 'Gi1 has shape', Gi1.shape
#just specifying size as (w, h) works
uGi1 = cv2.pyrUp(Gi1, dstsize=(w, h))
print 'unsmall has shape', uGi1.shape
Gi = Gi.astype(numpy.float32)
uGi1 = uGi1.astype(numpy.float32)
Li = Gi - uGi1
lp.append(Li)
cv2.imshow(win, 0.5 + 0.5 * (Li / numpy.abs(Li).max()))
while cv2.waitKey(15) < 0: pass
Gi = Gi1
Gi = Gi.astype(numpy.float32)
lp.append(Gi)
cv2.imshow(win, Gi/255.0)
while cv2.waitKey(15) < 0: pass
return lp
#########################
def pyr_reconstruct(lp):
Ri = lp[-1]
for i in range(len(lp)-1,0,-1):
h = Ri.shape[0]
w = Ri.shape[1]
print "h = ", h
print "w = ", w
uRi = cv2.pyrUp(Ri, dstsize=(2*w, 2*h))
print "uRi h = ", uRi.shape[0]
print "uRi w = ", uRi.shape[1]
print "lp[i-1] h = ", lp[i-1].shape[0]
print "lp[i-1] w = ", lp[i-1].shape[1]
Ri1 = uRi + lp[i-1]
Ri = Ri1
print "iteration = ", i
Ri = Ri.astype(numpy.uint8)
cv2.imshow(win, Ri)
while cv2.waitKey(15) < 0: pass
#########################
def alpha_blend(A, B, alpha):
A = A.astype(alpha.dtype)
B = B.astype(alpha.dtype)
# if A and B are RGB images, we must pad
# out alpha to be the right shape
print "alpha = ", alpha.shape
if len(A.shape) == 3:
alpha = numpy.expand_dims(alpha, 2)
print "A = ", A.shape
print "B = ", B.shape
print "alpha = ", alpha.shape
return A + alpha*(B-A)
#########################
def img_combine(A, B):
'''A and B are two images: h-w-3 arrays of uint8 numbers'''
pt1 = (0,0) #corners for image A mask
pt2 = (256, 512)
pt3 = (256, 0) #corners for image B mask
pt4 = (512, 512)
pts = [pt1,pt2,pt3,pt4]
"""
for i in range (NUM_PYR):
newpts = []
for point in pts:
newpoint = (int(point[0]*0.5),int(point[1]*0.5))
print newpoint
newpts.append(newpoint)
pts = newpts
maskA = cv2.rectangle(A, pts[0], pts[1], COLOR)
maskB = cv2.rectangle(B, pts[2], pts[3], COLOR)
cv2.imshow("maskA", maskA)
while cv2.waitKey(15) < 0: pass
cv2.imshow("maskB", maskB)
while cv2.waitKey(15) < 0: pass
"""
#make two Lapacian pyramids
lpA = pyr_build(A)
print "A pyramid built"
lpB = pyr_build(B)
lpAB = []
for i in range(len(lpA)):
lpAB.append(blend(lpA[i],lpB[i]))
pyr_reconstruct(lpAB)
#########################
def blend(A,B):
width = A.shape[0]
steepness = 50.0/width #increase numerator to decrease blend width
x = range(width)
midpt = width*0.5*numpy.ones(width)
alpha = 1/(1+numpy.exp(-steepness*(x-midpt)))
blended_img = alpha_blend(A,B,alpha)
cv2.imshow("blend", 0.5 + 0.5 * (blended_img / numpy.abs(blended_img).max()))
while cv2.waitKey(15) < 0: pass
return blended_img
#########################
def main():
G0 = cv2.imread('Golden_Retriever02.JPG')
h = G0.shape[0]
w = G0.shape[1]
lp = pyr_build(G0)
pyr_reconstruct(lp)
imgA = cv2.imread('cat_square.JPG')
imgB = cv2.imread('dog_square.JPG')
pt1 = (0,0)
pt2 = (256, 512)
pt3 = (256, 0)
pt4 = (512, 512)
"""
maskA = cv2.rectangle(imgA, pt1, pt2, COLOR)
maskB = cv2.rectangle(imgB, pt3, pt4, COLOR)
cv2.imshow("mask", maskA)
while cv2.waitKey(15) < 0: pass
cv2.imshow("mask", maskB)
while cv2.waitKey(15) < 0: pass
"""
img_combine(imgA, imgB)
# alpha_blend(imgA, imgB, alph)
main()