-
Notifications
You must be signed in to change notification settings - Fork 0
/
splice.py
239 lines (198 loc) · 7.52 KB
/
splice.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
from PIL import Image
import numpy
from scipy.signal import *
from scipy.sparse import lil_matrix, csr_matrix
import scipy.sparse.linalg
import sys
"""
Params:
mask: PIL image (rgb)
Returns:
mask: logical matrix
"""
def create_mask_from_image(mask):
mask = mask.convert('L') # grayscale
mask = numpy.array(mask)
mask = mask.astype(numpy.float64)
mask[mask>0] = 1 # turn non-white into 1
return mask
"""
Params:
source: PIL image (rgb)
target: PIL image (rgb)
mask: logical matrix
Returns:
result: dictionary of all necessary boundary information
"""
def get_size_info(source, target, mask):
# get target and source heights and widths
targetw, targeth = target.split()[0].size
sourcew, sourceh = source.split()[0].size
# find extent of region
xNon, yNon = numpy.where(mask==1)
startX = max([min(xNon) - 1, 0])
endX = min([max(xNon) + 1, sourceh - 1])
startY = max([min(yNon) - 1, 0])
endY = min([max(yNon) + 1, sourcew - 1])
region_width = endY - startY + 1
region_height = endX - startX + 1
half_region_width = region_width//2
half_region_height = region_height//2
return {"targetw": targetw,
"targeth": targeth,
"sourcew": sourcew,
"sourceh": sourceh,
"startX": startX,
"endX": endX,
"startY": startY,
"endY": endY,
"xNon": xNon,
"yNon": yNon,
"region_width": region_width,
"region_height": region_height,
"half_region_width": half_region_width,
"half_region_height": half_region_height }
"""
Params:
source: PIL image (rgb)
target: PIL image (rgb)
mask: logical matrix
offX: integer
offY: integer
db: dictionary
color: boolean
Returns:
result: PIL image, final result of splice
"""
def splice(source, target, mask, offY, offX, db):
print "db['targeth']:", db
#define coordinates of destination in target image if difference sizes
if db['targeth'] != db['sourceh'] or db['targetw'] != db['sourcew']:
db['startX_offset'] = offX - db['half_region_height']
db['startY_offset'] = offY - db['half_region_width']
db['endX_offset'] = offX + db['half_region_height']
db['endY_offset'] = offY + db['half_region_width']
else: # no offset otherwise
db['startX_offset'] = db['startX']
db['startY_offset'] = db['startY']
db['endX_offset'] = db['endX']
db['endY_offset'] = db['endY']
# process img
source = source.split()
sr, sg, sb = source[0], source[1], source[2]
target = target.split()
tr, tg, tb = target[0], target[1], target[2]
r = splice_gray(sr, tr, mask, db)
g = splice_gray(sg, tg, mask, db)
b = splice_gray(sb, tb, mask, db)
rgb = numpy.dstack((r, g, b))
result = Image.fromarray(rgb)
return result
"""
Params:
source: PIL image (grayscale)
target = PIL image (grayscale)
mask = logical array
db = dictionary of all necessary boundary info
Return:
result = 2D numpy array with one color channel processed
"""
def splice_gray(source, target, mask, db):
# convert images to arrays
target = numpy.array(target)
source = numpy.array(source)
# find boundaries of mask, mask = 2 for boundaries
n = db['xNon'].size
for cy, cx in zip(db['xNon'], db['yNon']):
if cy-1 >= 0 and mask[cy-1, cx] == 0:
mask[cy-1, cx] = 2
if cy+1 < db['sourceh'] and mask[cy+1, cx] == 0:
mask[cy+1, cx] = 2
if cx-1 >= 0 and mask[cy, cx-1] == 0:
mask[cy, cx-1] = 2
if cx+1 < db['sourcew'] and mask[cy, cx+1] == 0:
mask[cy, cx+1] = 2
# clip to relevant space
clipped_mask = mask[db['startX']:db['endX']+1, db['startY']:db['endY']+1]
clipped_target = target[db['startX_offset']-1:db['endX_offset'], db['startY_offset']-1:db['endY_offset']]
clipped_source = source[db['startX']:db['endX']+1, db['startY']:db['endY']+1]
# convert to ints
clipped_mask = clipped_mask.astype(numpy.float64)
clipped_target = clipped_target.astype(numpy.float64)
clipped_source = clipped_source.astype(numpy.float64)
laplacian = numpy.array([[0.0, -1.0, 0.0],
[-1.0, 4.0, -1.0],
[0.0, -1.0, 0.0]])
# get gradient of source and target
source_gradient = scipy.signal.convolve2d(clipped_source, laplacian)
source_gradient = source_gradient[1:db['region_height']+1, 1:db['region_width']+1]
target_gradient = scipy.signal.convolve2d(clipped_target, laplacian)
target_gradient = target_gradient[1:db['region_height']+1, 1:db['region_width']+1]
#initialize A matrix and b matrix
A = lil_matrix((n, n))
b = numpy.zeros((n, 1), numpy.float64)
# build map of unknowns to indices so each unknown has its own unique index
u_dict = {}
index = 0
for cy in range(db['region_width']):
for cx in range(db['region_height']):
if clipped_mask[cx, cy] == 1:
u_dict[(cx, cy)] = index
index = index+1
# build A matrix
index = 0
for cy in range(db['region_width']):
for cx in range(db['region_height']):
if clipped_mask[cx, cy] == 1:
# check neighbors
curb = 0
if cx-1 >= 0:
if clipped_mask[cx-1, cy] == 1: # neighbor is in mask
A[index, index-1] = -1
elif clipped_mask[cx-1, cy] == 2: # neighbor is boundary
curb = curb + clipped_target[cx-1, cy]
if cx+1 < db['region_height']:
if clipped_mask[cx+1, cy] == 1:
A[index, index+1] = -1
elif clipped_mask[cx+1, cy] == 2:
curb = curb + clipped_target[cx+1, cy]
if cy-1 >= 0:
if clipped_mask[cx, cy-1] == 1:
col = u_dict[(cx, cy-1)]
A[index, col] = -1
elif clipped_mask[cx, cy-1] == 2:
curb = curb + clipped_target[cx, cy-1]
if cy+1 < db['region_width']:
if clipped_mask[cx, cy+1] == 1:
col = u_dict[(cx, cy+1)]
A[index, col] = -1
elif clipped_mask[cx, cy+1] == 2:
curb = curb + clipped_target[cx, cy+1]
A[index, index] = 4
# compute b matrix
# version 1: alway add source_gradient
b[index] = curb + source_gradient[cx, cy]
"""
cur_source_grad = source_gradient[cx, cy]
cur_target_grad = target_gradient[cx, cy]
if abs(cur_source_grad) > abs(cur_target_grad):
b[index] = curb + cur_source_grad
else:
b[index] = curb + cur_target_grad
"""
index = index + 1
# solve system
x = scipy.sparse.linalg.spsolve(A.tocsr(), b)
x[x>255] = 255
x[x<0] = 0
x = x.astype(numpy.uint8)
clipped_target = clipped_target.astype(numpy.uint8)
# insert into image
index = 0
for cy in range(db['region_width']):
for cx in range(db['region_height']):
if clipped_mask[cx, cy] == 1:
clipped_target[cx, cy] = x[index]
index += 1
target[db['startX_offset']-1:db['endX_offset'], db['startY_offset']-1:db['endY_offset']] = clipped_target
return target