-
Notifications
You must be signed in to change notification settings - Fork 0
/
by_similarity.py
64 lines (51 loc) · 1.55 KB
/
by_similarity.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
import numpy as np
from PIL import Image
import sys, os
from numba import njit
from config import *
path_to_img = sys.argv[1] if len(sys.argv) != 1 else input('Path to image: ')
width = int(input('Width: '))
print('Processing...')
img = Image.open(path_to_img)
# Rescale image
img = img.resize((width*PW, int(img.size[1]/img.size[0]*width / symbol_aspect) * PH))
pix = np.asarray(img.convert('L'))
H, W = pix.shape[0], pix.shape[1]
# Getting symbol patterns
p_dir = 'blurred_symbols/'
patterns = []
for i in range(len(gradient)):
p = np.asarray(Image.open(p_dir + str(i) + '.png').convert('L'))
patterns.append(p)
p = np.array(patterns).flatten()
patterns = np.array(patterns, dtype='float64')
# Normalising symbols patterns
patterns -= np.full((PH, PW), min(p))
patterns *= np.full((PH, PW), 255/max(p))
@njit(fastmath=True)
def difference(a, b):
d = 0
for y in range(PH):
for x in range(PW):
#d += (a[y, x] - b[y, x])**2
d += abs(a[y, x] - b[y, x])
return d
@njit
def check_pattern(pat):
# Finds the most similar symbol
it = 0; min_d = 2560000
for i in range(len(patterns)):
d = difference(patterns[i]/255, pat/255)
if d < min_d:
min_d = d; it = i
return it
f = open('res.txt', 'w')
# Traversing the image by sectors the size of the symbol image and writing result
for y in range(PH, H, PH):
for x in range(PW, W, PW):
part = pix[y-PH:y, x-PW:x]
r = check_pattern(part)
f.write(gradient[r])
f.write('\n')
print(round(y / H * 100, 1))
f.close()