forked from neel1998/DIP_Project_2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patchmatch_whole_image.py
303 lines (253 loc) · 8.62 KB
/
patchmatch_whole_image.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import numpy as np
import matplotlib.pyplot as plt
import cv2
import sys
# coordinates of rectangle drawn on image
ref_point = []
def shape_selection(event, x, y, flags, param):
'''
Function for drawing rectangle
'''
global ref_point
if event == cv2.EVENT_LBUTTONDOWN:
ref_point = [(x, y)]
elif event == cv2.EVENT_LBUTTONUP:
ref_point.append((x, y))
cv2.rectangle(image, ref_point[0], ref_point[1], (0, 255, 0), 2)
cv2.imshow("image", image)
def do_patches(nnf, inp1, inp2, siz):
'''
Copy best matching patches to input image
nnf: Nearest neighbour field
inp1: Input image
inp2: Reference image
siz: Patch size
'''
inp_shape = inp1.shape
w = int((siz - 1) / 2)
out = np.zeros(inp_shape, np.float)
for i in range(w, inp_shape[0] - w, siz):
for j in range(w, inp_shape[1] - w, siz):
x = nnf[0][i][j]
y = nnf[1][i][j]
temp = inp2[x - w: x + w + 1, y - w: y + w + 1]
out[i - w: i + w + 1, j - w: j + w + 1] = temp
out = np.uint8(out)
return out
def nearestnf(inp1, inp2, siz, iterations):
'''
This function computes nearest neighbour field followed by propagation and random search process.
inp1: Input image
inp2: Reference image
siz: Patch size
iterations: Number of iterations for which algorithm runs
'''
inp1, inp2 = np.array(inp1, np.float), np.array(inp2, np.float)
w = int((siz - 1) / 2)
inp_shape = np.shape(inp1)
old_sz = inp_shape
# create rectangle divisible by patch size
new_inp_shape = np.zeros(len(inp_shape))
new_inp_shape[0] = inp_shape[0] + siz - inp_shape[0] % siz
new_inp_shape[1] = inp_shape[1] + siz - inp_shape[1] % siz
# preserve the 3rd dimension if colored image
for i in range(2, len(inp_shape)):
new_inp_shape[i] = inp_shape[i]
new_inp_shape = np.uint(new_inp_shape)
new_inp = np.zeros(new_inp_shape)
new_inp[:inp_shape[0], :inp_shape[1]] = inp1[:inp_shape[0], :inp_shape[1]]
new_inp = np.uint8(new_inp)
inp1 = np.copy(new_inp)
inp_shape = new_inp_shape
ref_shape = np.shape(inp2)
# outx if NNF containing x coordinates of reference image
outx = np.random.randint(w, ref_shape[0] - w, (inp_shape[0], inp_shape[1]))
# outy if NNF containing y coordinates of reference image
outy = np.random.randint(w, ref_shape[1] - w, (inp_shape[0], inp_shape[1]))
pad_image = np.pad(inp1, ((w,w),(w,w),(0,0)), 'constant', constant_values=(np.nan, np.nan)) # padded image
off = np.full((inp_shape[0], inp_shape[1]), np.inf) # offset array which error metric between two patches
#initial copmutation of offsets
for i in range(inp_shape[0]):
for j in range(inp_shape[1]):
x = outx[i, j]
y = outy[i, j]
a = pad_image[i: i + siz, j: j + siz, :]
b = inp2[x - w: x + w + 1, y - w: y + w + 1, :]
temp = a - b
temp = temp[~np.isnan(temp)]
temp2 = np.sum(temp ** 2) / len(temp)
off[i, j] = temp2
# Initial NNF
final = do_patches([outx, outy], inp1, inp2, siz)
final = final[:old_sz[0], :old_sz[1]]
plt.subplot(331)
plt.axis('off')
plt.imshow(final)
plt.title("Initial")
for itr in range(iterations):
if itr % 2 == 0:
tot = inp_shape[0] * inp_shape[1]
tot = int(tot / 4)
ctr = 0
# Scan Order: Left to Right, Top to Bottom
for i in range(inp_shape[0]):
for j in range(inp_shape[1]):
# Propagation:
cur = off[i][j] #current patch
left = off[max(i - 1, 0)][j] # left patch
top = off[i][max(j - 1, 0)] # top patch
mn = min(cur, left, top) # best match patch from above three
if mn == left:
x = outx[i - 1][j] + 1
y = outy[i - 1][j]
if x < ref_shape[0] - w and y < ref_shape[1] - w:
outx[i, j] = x
outy[i, j] = y
a = pad_image[i: i + siz, j: j + siz, :]
b = inp2[x - w: x + w + 1, y - w: y + w + 1, :]
temp = a - b
temp = temp[~np.isnan(temp)]
temp2 = np.sum(temp ** 2) / len(temp)
off[i, j] = temp2
elif mn == top:
x = outx[i][j - 1]
y = outy[i][j - 1] + 1
if x < ref_shape[0] - w and y < ref_shape[1] - w:
outx[i, j] = x
outy[i, j] = y
a = pad_image[i: i + siz, j: j + siz, :]
b = inp2[x - w: x + w + 1, y - w: y + w + 1, :]
temp = a - b
temp = temp[~np.isnan(temp)]
temp2 = np.sum(temp ** 2) / len(temp)
off[i, j] = temp2
# Random Search
alpha = 0.5
radius = np.min(ref_shape[:2]) * (alpha**2)
x = outx[i][j]
y = outy[i][j]
while radius > 1:
x_min, x_max = max(x - radius, w), min(x + radius, ref_shape[0] - w - 1)
y_min, y_max = max(y - radius, w), min(y + radius, ref_shape[1] - w - 1)
random_x = np.random.randint(x_min, x_max)
random_y = np.random.randint(y_min, y_max)
#offset random search
a = pad_image[i: i + siz, j: j + siz, :]
b = inp2[random_x - w: random_x + w + 1, random_y - w: random_y + w + 1, :]
temp = a - b
temp = temp[~np.isnan(temp)]
temp2 = np.sum(temp ** 2) / len(temp)
off_rs = temp2
# update if better patch found
if off_rs < off[i, j]:
off[i][j] = off_rs
outx[i][j] = random_x
outy[i][j] = random_y
radius *= alpha
# Various plots at 1/4th 3/4th iteration
ctr += 1
if ctr == tot and itr == 0:
plt.subplot(332)
plt.axis('off')
final = do_patches([outx, outy], inp1, inp2, siz)
final = final[:old_sz[0], :old_sz[1]]
plt.imshow(final)
plt.title("1 / 4 Iteration")
elif ctr == 3 * tot and itr == 0:
plt.subplot(333)
plt.axis('off')
final = do_patches([outx, outy], inp1, inp2, siz)
final = final[:old_sz[0], :old_sz[1]]
plt.imshow(final)
plt.title("3 / 4 Iteration")
else:
# Reverse Scan Order: Right to Left, Bottom to Top
inp_s = [np.uint64(inp_shape[0] - 1), np.uint64(inp_shape[1] - 1)]
for i in range(inp_s[0], -1, -1):
for j in range(inp_s[1], -1, -1):
# Propagation:
cur = off[i][j] # current patch
right = off[min(i + 1, inp_s[0])][j] # right patch
bottom = off[i][min(j + 1, inp_s[1])] # bottom patch
mn = min(cur, right, bottom) # best of above three
if mn == right and cur != right:
x = outx[i + 1][j] - 1
y = outy[i + 1][j]
if x >= w and y >= w:
outx[i, j] = x
outy[i, j] = y
a = pad_image[i: i + siz, j: j + siz, :]
b = inp2[x - w: x + w + 1, y - w: y + w + 1, :]
temp = a - b
temp = temp[~np.isnan(temp)]
temp2 = np.sum(temp ** 2) / len(temp)
off[i, j] = temp2
elif mn == bottom and cur != bottom:
x = outx[i][j - 1]
y = outy[i][j - 1] - 1
if x >= w and y >= w:
outx[i, j] = x
outy[i, j] = y
a = pad_image[i: i + siz, j: j + siz, :]
b = inp2[x - w: x + w + 1, y - w: y + w + 1, :]
temp = a - b
temp = temp[~np.isnan(temp)]
temp2 = np.sum(temp ** 2) / len(temp)
off[i, j] = temp2
# Random Search
alpha = 0.5
radius = np.min(ref_shape[:2]) * (alpha**2)
x = outx[i][j]
y = outy[i][j]
while radius > 1:
x_min, x_max = max(x - radius, w), min(x + radius, ref_shape[0] - w - 1)
y_min, y_max = max(y - radius, w), min(y + radius, ref_shape[1] - w - 1)
random_x = np.random.randint(x_min, x_max)
random_y = np.random.randint(y_min, y_max)
#offset random search
a = pad_image[i: i + siz, j: j + siz, :]
b = inp2[random_x - w: random_x + w + 1, random_y - w: random_y + w + 1, :]
temp = a - b
temp = temp[~np.isnan(temp)]
temp2 = np.sum(temp ** 2) / len(temp)
off_rs = temp2
# update if better patch found
if off_rs < off[i, j]:
off[i][j] = off_rs
outx[i][j] = random_x
outy[i][j] = random_y
radius *= alpha
plt.subplot(3, 3, itr + 4)
plt.axis('off')
final = do_patches([outx, outy], inp1, inp2, siz)
final = final[:old_sz[0], :old_sz[1]]
plt.imshow(final)
plt.title("{} Iteration".format(itr + 1))
# final image made by matching patches
final = do_patches([outx, outy], inp1, inp2, siz)
final = final[:old_sz[0], :old_sz[1]]
return final
if len(sys.argv) != 5:
print("Please provide proper command line arguments")
exit(0)
# Input image
input_img = cv2.imread(sys.argv[1])
input_img = cv2.cvtColor(input_img, cv2.COLOR_BGR2RGB)
input_img_copy = np.copy(input_img)
# Refernce image
ref_img = cv2.imread(sys.argv[2])
ref_img = cv2.cvtColor(ref_img, cv2.COLOR_BGR2RGB)
im = nearestnf(input_img, ref_img, int(sys.argv[3]), int(sys.argv[4]))
plt.subplot(3,3,7)
plt.axis('off')
plt.imshow(input_img_copy)
plt.title("Original Image")
plt.subplot(3,3,8)
plt.axis('off')
plt.imshow(ref_img)
plt.title("Reference Image")
plt.subplot(3,3,9)
plt.axis('off')
plt.imshow(im)
plt.title("Reconstructed Image")
plt.show()