-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneighbourhood_analysis.py
More file actions
249 lines (212 loc) · 13.6 KB
/
neighbourhood_analysis.py
File metadata and controls
249 lines (212 loc) · 13.6 KB
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
import os
from PIL import Image, ImageDraw
import numpy as np
class NeighbourhoodAnalysis(object):
def __init__(self, file1=None, file2=None, file1_image=None, file2_image=None, number_of_neighbours=35,
integrity_threshold=0.4):
self.number_of_keypoints_file1 = 0
self.number_of_features_file1 = 0
self.keypoints_features_file1 = None
self.keypoints_coordinates_file1 = None
self.number_of_keypoints_file2 = 0
self.number_of_features_file2 = 0
self.keypoints_features_file2 = None
self.keypoints_coordinates_file2 = None
self.euclidean_distance_features_matrix = None
self.go_columnwise = False
self.keypoints_pairs = np.empty((1, 2, 2), dtype=float)
self.keypoints_integrity = None
self.integral_keypoints = np.array([[[]]]).reshape((0, 2, 2))
self.integrity_threshold = integrity_threshold
# self.minimum_values = np.empty(0, dtype=float)
if file1 is not None:
with open(file1) as fp:
for i, line in enumerate(fp):
if i == 0:
# wczytanie liczby cech
self.number_of_features_file1 = int(line)
elif i == 1:
# wczytanie liczby punktów kluczowych
self.number_of_keypoints_file1 = int(line)
self.keypoints_coordinates_file1 = np.zeros(
(self.number_of_keypoints_file1, 2), dtype=float)
self.keypoints_features_file1 = np.zeros(
(self.number_of_keypoints_file1, self.number_of_features_file1), dtype=float)
elif 1 < i <= (self.number_of_keypoints_file1 + 1):
# wczytanie wartości cech i współrzędnych punktów kluczowych
line_values = np.array([float(x) for x in line.split()])
# line_values = np.array([map(float, line.split())])
# wprowadzenie współrzędnych punktów kluczowych
self.keypoints_coordinates_file1[i - 2][0] = line_values[0]
self.keypoints_coordinates_file1[i - 2][1] = line_values[1]
# wprowadzenie cech punktów kluczowych
for feature in range(self.number_of_features_file1):
self.keypoints_features_file1[i - 2][feature] = line_values[feature + 5]
if i > self.number_of_keypoints_file1 + 1:
break
else:
print("Nie można znaleźć ścieżki - file1")
if file2 is not None:
with open(file2) as fp:
for i, line in enumerate(fp):
if i == 0:
# wczytanie liczby cech
self.number_of_features_file2 = int(line)
elif i == 1:
# wczytanie liczby punktów kluczowych
self.number_of_keypoints_file2 = int(line)
self.keypoints_coordinates_file2 = np.zeros(
(self.number_of_keypoints_file2, 2), dtype=float)
self.keypoints_features_file2 = np.zeros(
(self.number_of_keypoints_file2, self.number_of_features_file2), dtype=float)
elif 1 < i <= (self.number_of_keypoints_file2 + 1):
# wczytanie wartości cech i współrzędnych punktów kluczowych
line_values = np.array([float(x) for x in line.split()])
# wprowadzenie współrzędnych punktów kluczowych
self.keypoints_coordinates_file2[i - 2][0] = line_values[0]
self.keypoints_coordinates_file2[i - 2][1] = line_values[1]
# wprowadzenie cech punktów kluczowych
for feature in range(self.number_of_features_file2):
self.keypoints_features_file2[i - 2][feature] = line_values[feature + 5]
if i > self.number_of_keypoints_file2 + 1:
break
else:
print("Nie można znaleźć ścieżki - file2")
# inicjalizacja tablicy przchowującej odległości euklidesowe między cechami każdej pary punktów z obrazka A i obrazka B
self.euclidean_distance_features_matrix = np.zeros(
(self.number_of_keypoints_file1, self.number_of_keypoints_file2), dtype=float)
# obliczenie odległości euklidesowych między cechami każdej pary punktów z obrazka A i obrazka B
for keypoint_file1 in range(self.number_of_keypoints_file1):
for keypoint_file2 in range(self.number_of_keypoints_file2):
self.euclidean_distance_features_matrix[keypoint_file1][keypoint_file2] = np.linalg.norm(
self.keypoints_features_file1[
keypoint_file1] -
self.keypoints_features_file2[
keypoint_file2])
# sprawdzenie czy w pliku pierwszym czy drugim znaleziono więcej punktów kluczowych
if self.number_of_keypoints_file1 < self.number_of_keypoints_file2:
self.go_columnwise = False
else:
self.go_columnwise = True
# znalezienie najbliższego sąsiada dla każdego punktu
if not self.go_columnwise:
for keypoint_file1 in range(self.number_of_keypoints_file1):
if keypoint_file1 == np.argmin(self.euclidean_distance_features_matrix[:,
np.argmin(self.euclidean_distance_features_matrix[keypoint_file1, :])]):
keypoint_file2 = np.argmin(self.euclidean_distance_features_matrix[keypoint_file1, :])
self.keypoints_pairs = np.append(self.keypoints_pairs,
np.array([np.array([self.keypoints_coordinates_file1[
keypoint_file1][0],
self.keypoints_coordinates_file1[
keypoint_file1][1]]), np.array([
self.keypoints_coordinates_file2[
keypoint_file2][0],
self.keypoints_coordinates_file2[
keypoint_file2][
1]])]).reshape((1, 2, 2)),
axis=0)
# self.minimum_values = np.append(self.minimum_values, np.array([self.euclidean_distance_features_matrix[keypoint_file1][keypoint_file2]]), axis=0)
if self.go_columnwise:
for keypoint_file2 in range(self.number_of_keypoints_file2):
if keypoint_file2 == np.argmin(self.euclidean_distance_features_matrix[
np.argmin(self.euclidean_distance_features_matrix[:, keypoint_file2]),
:]):
keypoint_file1 = np.argmin(self.euclidean_distance_features_matrix[:, keypoint_file2])
self.keypoints_pairs = np.append(self.keypoints_pairs,
np.array([np.array([self.keypoints_coordinates_file1[
keypoint_file1][0],
self.keypoints_coordinates_file1[
keypoint_file1][1]]), np.array([
self.keypoints_coordinates_file2[
keypoint_file2][0],
self.keypoints_coordinates_file2[
keypoint_file2][
1]])]).reshape((1, 2, 2)),
axis=0)
# znalezienie punktów sąsiadujących i obliczenie spójności każdej pary punktów kluczowych
# print(self.get_nearest_keypoints(self.keypoints_pairs[0][1], file2=True))
self.keypoints_integrity = np.zeros(self.keypoints_pairs.shape[0], dtype=float)
for i, pair in enumerate(self.keypoints_pairs):
for near_keypoint in self.get_nearest_keypoints(pair[0], number_of_keypoints=number_of_neighbours):
if self.keypoints_pairs[np.argwhere(self.keypoints_pairs[:, 0] == near_keypoint)[0][0]][
1] in self.get_nearest_keypoints(pair[1], number_of_keypoints=number_of_neighbours, file2=True):
self.keypoints_integrity[i] = self.keypoints_integrity[i] + 1.0
self.keypoints_integrity[i] = self.keypoints_integrity[i] / number_of_neighbours
# sprawdzenie czy dany pnkt przekracza próg spójności
if self.keypoints_integrity[i] >= self.integrity_threshold:
self.integral_keypoints = np.append(self.integral_keypoints, np.array([pair]), axis=0)
# if i % 50 == 0:
# print("Postęp: " + str(float(i/self.keypoints_pairs.shape[0]) * 100.0) + " %")
# print(self.keypoints_integrity)
# print(self.integral_keypoints)
# wizualizacja punktów kluczowych - self.integral_keypoints
self.print_image([file1_image, file2_image], self.keypoints_pairs,
'adjacency_n{}_t{}_f{}.png'.format(number_of_neighbours, integrity_threshold, file1_image[-5]))
pass
def get_nearest_keypoints(self, keypoint_to_check, file2=False, number_of_keypoints=50):
# print(keypoint_to_check)
if not file2:
distances = np.zeros(self.keypoints_pairs.shape[0], dtype=float)
# print(self.keypoints_pairs.shape)
for i, keypoint in enumerate(self.keypoints_pairs[:, 0]):
distances[i] = np.linalg.norm(keypoint_to_check - keypoint)
nearest_keypoints = distances.argsort()[:number_of_keypoints + 1]
# print(distances)
nearest_keypoints = np.delete(nearest_keypoints, 0)
# print(nearest_keypoints)
wanted_keypoints = np.array([[]]).reshape((0, 2))
# value = np.empty(2, dtype=float)
for keypoint in nearest_keypoints:
value = self.keypoints_pairs[keypoint][0]
wanted_keypoints = np.append(wanted_keypoints, np.array([value]), axis=0)
return wanted_keypoints
if file2:
distances = np.zeros(self.keypoints_pairs.shape[0], dtype=float)
# print(self.keypoints_pairs.shape)
for i, keypoint in enumerate(self.keypoints_pairs[:, 1]):
distances[i] = np.linalg.norm(keypoint_to_check - keypoint)
nearest_keypoints = distances.argsort()[:number_of_keypoints + 1]
# print(distances)
nearest_keypoints = np.delete(nearest_keypoints, 0)
# print(nearest_keypoints)
wanted_keypoints = np.array([[]]).reshape((0, 2))
# value = np.empty(2, dtype=float)
for keypoint in nearest_keypoints:
value = self.keypoints_pairs[keypoint][1]
wanted_keypoints = np.append(wanted_keypoints, np.array([value]), axis=0)
return wanted_keypoints
def print_image(self, image_paths, pairs, filename):
points_better_than_threshold = 0
points_equal_threshold = 0
points_worse_than_threshold = 0
images = list(map(Image.open, image_paths))
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
x_offset = images[0].size[0]
new_im = Image.new('RGB', (total_width, max_height))
new_im.paste(images[0], (0, 0))
new_im.paste(images[1], (x_offset, 0))
draw = ImageDraw.Draw(new_im)
for i, pair in enumerate(pairs):
x1 = pair[0][0]
y1 = pair[0][1]
x2 = pair[1][0] + x_offset
y2 = pair[1][1]
if self.keypoints_integrity[i] >= (self.integrity_threshold + 0.05):
color = tuple(np.array([0, 255, 0]))
draw.line((x1, y1, x2, y2), fill=color)
points_better_than_threshold = points_better_than_threshold + 1
if (self.integrity_threshold + 0.05) > self.keypoints_integrity[i] >= self.integrity_threshold:
color = tuple(np.array([255, 255, 0]))
draw.line((x1, y1, x2, y2), fill=color)
points_equal_threshold = points_equal_threshold + 1
if self.integrity_threshold > self.keypoints_integrity[i] >= (self.integrity_threshold - 0.05):
color = tuple(np.array([255, 0, 0]))
draw.line((x1, y1, x2, y2), fill=color)
points_worse_than_threshold = points_worse_than_threshold + 1
result_path = os.path.join(os.path.dirname(image_paths[0]), filename)
new_im.save(result_path)
print("Punkty lepsze niż próg: " + str(points_better_than_threshold))
print("Punkty równe i prawie tak samo dobre jak próg: " + str(points_equal_threshold))
print("Punkty gorsze niż próg: " + str(points_worse_than_threshold))