-
Notifications
You must be signed in to change notification settings - Fork 0
/
similarity_matrix.py
executable file
·278 lines (209 loc) · 8.15 KB
/
similarity_matrix.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
#!/usr/bin/env python3
import sys
import cv2
import numpy as np
import skimage.measure as measures
import argparse
import pandas as pd
from pathlib import Path
from multiprocessing.pool import Pool
import itertools
from math import floor, ceil, factorial
from tqdm import tqdm
def options():
parser = argparse.ArgumentParser()
parser.add_argument('images', nargs='+')
parser.add_argument('--csv', action='store_true')
parser.add_argument('--rotation_invariant', action='store_true')
parser.add_argument('--parallel', action='store_true')
parser.add_argument('--num_processes', type=int, default=4)
parser.add_argument('--measure', default='ssim', choices=['ssim', 'mse'])
parser.add_argument('--downsample', type=float, default=None)
return parser.parse_args()
def mse(imageA, imageB):
# the 'Mean Squared Error' between the two images is the
# sum of the squared difference between the two images;
# NOTE: the two images must have the same dimension
err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
err /= float(imageA.shape[0] * imageA.shape[1])
# return the MSE, the lower the error, the more "similar"
# the two images are
return err
def compare(imageA, imageB, measure=None):
if not measure:
measure = measures.compare_ssim
return measure(imageA, imageB)
def pad_images(imgA, imgB):
# add padding so that images have the same size (if needed)
width = lambda x: x.shape[1]
height = lambda x: x.shape[0]
# print(imgA.shape)
# print(imgB.shape)
# split padding between right/left or top/bottom
split = lambda v: (v/2, v/2) if v % 2 == 0 else (floor(v/2), ceil(v/2))
if width(imgA) != width(imgB):
argminW = min(imgA, imgB, key=width)
maxW = max(width(imgA), width(imgB))
diff = maxW - width(argminW)
padding_left, padding_right = split(diff)
img_padded = cv2.copyMakeBorder(argminW, 0, 0,
int(padding_left),
int(padding_right),
cv2.BORDER_CONSTANT,
None,
255)
if maxW == width(imgA):
imgB = img_padded
else:
imgA = img_padded
if height(imgA) != height(imgB):
argminH = min(imgA, imgB, key=height)
maxH = max(height(imgA), height(imgB))
diff = maxH - height(argminH)
padding_bottom, padding_top = split(diff)
img_padded = cv2.copyMakeBorder(argminH,
int(padding_bottom),
int(padding_top),
0, 0,
cv2.BORDER_CONSTANT,
None,
255)
if maxH == height(imgA):
imgB = img_padded
else:
imgA = img_padded
# cv2.imshow('ImageA', imgA)
# cv2.imshow('ImageB', imgB)
# input("Proceed")
return (imgA, imgB)
def pad_image(img, w, h):
# add padding so that images have the same size (if needed)
width = lambda x: x.shape[1]
height = lambda x: x.shape[0]
# split padding between right/left or top/bottom
split = lambda v: (v/2, v/2) if v % 2 == 0 else (floor(v/2), ceil(v/2))
if width(img) < w:
diff = w - width(img)
padding_left, padding_right = split(diff)
img = cv2.copyMakeBorder(img, 0, 0,
int(padding_left),
int(padding_right),
cv2.BORDER_CONSTANT,
None,
255)
if height(img) < h:
diff = h - height(img)
padding_top, padding_bottom = split(diff)
img = cv2.copyMakeBorder(img,
int(padding_top),
int(padding_bottom),
0, 0,
cv2.BORDER_CONSTANT,
None,
255)
# print(width(img), height(img))
return img
def rotate(img, angle):
cols, rows = img.shape
M = cv2.getRotationMatrix2D((cols/2,rows/2), angle, 1)
return cv2.warpAffine(img, M, (cols, rows))
def scale(img, width, height):
return cv2.resize(img, (width, height), interpolation = cv2.INTER_CUBIC)
def do_singleprocess(images, measure, is_rotation_invariant=False):
if is_rotation_invariant:
rotations = dict()
for path, img in images:
rotations[path] = (rotate(img, 90),
rotate(img, 180),
rotate(img, 270))
records = []
for a, b in tqdm(list(itertools.combinations(images, 2))):
imgA_path, imgA = a
imgB_path, imgB = b
value = compare(imgA, imgB, measure)
if is_rotation_invariant:
imgB_90, imgB_180, imgB_270 = rotations[imgB_path]
value_90 = compare(imgA, imgB_90, measure)
value_180 = compare(imgA, imgB_180, measure)
value_270 = compare(imgA, imgB_270, measure)
value = max(value, value_90, value_180, value_270)
records.append((imgA_path.name,
imgB_path.name,
value))
return records
class Doer():
def __init__(self, measure, is_rotation_invariant=False, rotations=None):
self.is_rotation_invariant = is_rotation_invariant
self.rotations = rotations
self.measure = measure
def do(self, in_tuple):
(imgA_path, imgA), (imgB_path, imgB) = in_tuple
value = compare(imgA, imgB, measure)
if self.is_rotation_invariant:
imgB_90, imgB_180, imgB_270 = self.rotations[imgB_path]
value_90 = compare(imgA, imgB_90, measure)
value_180 = compare(imgA, imgB_180, measure)
value_270 = compare(imgA, imgB_270, measure)
value = max(value, value_90, value_180, value_270)
return imgA_path.name, imgB_path.name, value
# def do_multiprocess(images, num_processes, is_rotation_invariant=False):
# if is_rotation_invariant:
# rotations = dict()
# for path, img in images:
# rotations[path] = (rotate(img, 90),
# rotate(img, 180),
# rotate(img, 270))
# pool = Pool(num_processes)
# doer = Doer(is_rotation_invariant, rotations)
# records = pool.map(doer.do, list(itertools.combinations(images, 2)))
# return records
def do_multiprocess(images, measure, num_processes, is_rotation_invariant=False):
rotations = None
if is_rotation_invariant:
rotations = dict()
for path, img in images:
rotations[path] = (rotate(img, 90),
rotate(img, 180),
rotate(img, 270))
pool = Pool(num_processes)
doer = Doer(measure, is_rotation_invariant, rotations)
n_images = len(images)
n_combinations = factorial(n_images) / (factorial(2) * factorial(n_images-2) )
records=[]
with tqdm(total=n_combinations) as pbar:
for record in tqdm(pool.imap_unordered(doer.do, list(itertools.combinations(images, 2)), chunksize=50)):
records.append(record)
pbar.update()
return records
if __name__ == '__main__':
opts = options()
if opts.measure == 'ssim':
measure = measures.compare_ssim
else:
measure = measures.compare_nrmse
# Load images
images = []
for image_path in opts.images:
image_path = Path(image_path)
img = cv2.imread(str(image_path))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
images.append((image_path, img))
# Scale all images to lowest size
width = min(map(lambda x: x[1].shape[1], images))
height = min(map(lambda x: x[1].shape[0], images))
if opts.downsample:
width = round(opts.downsample * width)
height = round(opts.downsample * height)
# if rotation invariant, we want all images to be a perfect square
if opts.rotation_invariant:
width = height = max(width, height)
images = [(imgpath, scale(img, width, height)) for imgpath, img in tqdm(images)]
if opts.parallel:
records = do_multiprocess(images, measure, opts.num_processes, opts.rotation_invariant)
else:
records = do_singleprocess(images, measure, opts.rotation_invariant)
df = pd.DataFrame.from_records(records, columns=['A', 'B', 'measure'])
if opts.csv:
df.to_csv("/dev/stdout", index=False)
else:
print(df.to_string())