-
Notifications
You must be signed in to change notification settings - Fork 16
/
matching.py
218 lines (150 loc) · 7.08 KB
/
matching.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
import cv2
import os
def fort_image_matching(url_img_name, fort_img_name, crop_area = 0):
url_img_basename = os.path.basename(url_img_name)
url_img_basename, url_img_ext = os.path.splitext(url_img_basename)
url_img = cv2.imread(url_img_name, 3)
fort_img = cv2.imread(fort_img_name, 3)
if url_img is None or fort_img is None:
return 0.0
height, width, channels = url_img.shape
if url_img_ext == '.jpg':
if width > height:
scale = float(288 / height)
else:
scale = float(288 / width)
height_f, width_f, channels_f = fort_img.shape
scale_fort = width_f / 320
url_img = cv2.resize(url_img, None, fx=scale * scale_fort, fy=scale * scale_fort,
interpolation=cv2.INTER_NEAREST)
crop_y1 = 74
crop_y2 = 246
crop_x1 = 74
crop_x2 = 144
if crop_area == 1:
crop_y1 = 74
crop_y2 = 144
crop_x1 = 134
crop_x2 = 246
crop = fort_img[int(crop_y1 * scale_fort):int(crop_y2 * scale_fort), int(crop_x1 * scale_fort):int(crop_x2 * scale_fort)]
# Calculate vector from center of fort image(x=74, y=74, width=172, height=172) to top left corner of crop of fort_img
fi_center_x = (((246 + 74) / 2) * scale_fort) - (crop_x1 * scale_fort)
fi_center_y = (((246 + 74) / 2) * scale_fort) - (crop_y1 * scale_fort)
if crop.mean() == 255 or crop.mean() == 0:
return 0.0
result = cv2.matchTemplate(url_img, crop, cv2.TM_CCOEFF_NORMED)
min_val3, max_val3, min_loc3, max_loc3 = cv2.minMaxLoc(result)
height, width, channels = url_img.shape
ui_center_x = width / 2 - max_loc3[0]
ui_center_y = height / 2 - max_loc3[1]
dif_x = abs(ui_center_x - fi_center_x)
dif_y = abs(ui_center_y - fi_center_y)
if dif_x > 5 or dif_y > 5:
return 0.0
else: # for png file
scale = float(width / (144 - 74))
height_f, width_f, channels_f = fort_img.shape
scale_fort = width_f / 320
url_img = cv2.resize(url_img, None, fx=scale * scale_fort, fy=scale * scale_fort,
interpolation=cv2.INTER_NEAREST)
crop = fort_img[int(74 * scale_fort):int(246 * scale_fort), int(74 * scale_fort):int(144 * scale_fort)]
if crop.mean() == 255 or crop.mean() == 0:
return 0.0
result = cv2.matchTemplate(url_img, crop, cv2.TM_CCOEFF_NORMED)
min_val3, max_val3, min_loc3, max_loc3 = cv2.minMaxLoc(result)
return max_val3
def fort_image_matching_imshow(url_img_name, fort_img_name, crop_area = 0):
url_img_basename = os.path.basename(url_img_name)
url_img_basename, url_img_ext = os.path.splitext(url_img_basename)
url_img = cv2.imread(url_img_name, 3)
fort_img = cv2.imread(fort_img_name, 3)
if url_img is None or fort_img is None:
return 0.0
height, width, channels = url_img.shape
if url_img_ext == '.jpg':
if width > height:
scale = float(288 / height)
else:
scale = float(288 / width)
height_f, width_f, channels_f = fort_img.shape
scale_fort = width_f / 320
url_img = cv2.resize(url_img, None, fx=scale * scale_fort, fy=scale * scale_fort,
interpolation=cv2.INTER_NEAREST)
crop_y1 = 74
crop_y2 = 246
crop_x1 = 74
crop_x2 = 144
if crop_area == 1:
crop_y1 = 74
crop_y2 = 174
crop_x1 = 134
crop_x2 = 246
crop = fort_img[int(crop_y1 * scale_fort):int(crop_y2 * scale_fort),
int(crop_x1 * scale_fort):int(crop_x2 * scale_fort)]
# Calculate vector from center of fort image(x=74, y=74, width=172, height=172) to top left corner of crop of fort_img
fi_center_x = (((246 + 74) / 2) * scale_fort) - (crop_x1 * scale_fort)
fi_center_y = (((246 + 74) / 2) * scale_fort) - (crop_y1 * scale_fort)
if crop.mean() == 255 or crop.mean() == 0:
return 0.0
result = cv2.matchTemplate(url_img, crop, cv2.TM_CCOEFF_NORMED)
min_val3, max_val3, min_loc3, max_loc3 = cv2.minMaxLoc(result)
height, width, channels = url_img.shape
ui_center_x = width / 2 - max_loc3[0]
ui_center_y = height / 2 - max_loc3[1]
dif_x = abs(ui_center_x - fi_center_x)
dif_y = abs(ui_center_y - fi_center_y)
print(dif_x, dif_y)
if dif_x > 5 or dif_y > 5:
return 0.0
else: # for png file
scale = float(width / (144 - 74))
height_f, width_f, channels_f = fort_img.shape
scale_fort = width_f / 320
url_img = cv2.resize(url_img, None, fx=scale * scale_fort, fy=scale * scale_fort,
interpolation=cv2.INTER_NEAREST)
crop = fort_img[int(74 * scale_fort):int(246 * scale_fort), int(74 * scale_fort):int(144 * scale_fort)]
if crop.mean() == 255 or crop.mean() == 0:
return 0.0
result = cv2.matchTemplate(url_img, crop, cv2.TM_CCOEFF_NORMED)
min_val3, max_val3, min_loc3, max_loc3 = cv2.minMaxLoc(result)
top_left = max_loc3
height, width, channels = crop.shape
bottom_right = (top_left[0] + width, top_left[1] + height)
cv2.rectangle(url_img, top_left, bottom_right, (0, 255, 0), 2)
cv2.rectangle(fort_img, (int(crop_x1 * scale_fort), int(crop_y1 * scale_fort)),
(int(crop_x2 * scale_fort), int(crop_y2 * scale_fort)), (0, 0, 255), 2)
cv2.imshow('matching result', url_img)
cv2.imshow('fort image', fort_img)
cv2.imshow('crop', crop)
cv2.waitKey(0)
if dif_x > 5 or dif_y > 5:
return 0.0
return max_val3
def pokemon_image_matching(pokemon_image_name, fort_img_name, is_pokemon):
pokemon_image = cv2.imread(pokemon_image_name, cv2.IMREAD_UNCHANGED)
fort_img = cv2.imread(fort_img_name, 3)
if pokemon_image is None or fort_img is None:
return 100000.0
croped = pokemon_image[0:256,0:190]
height_f, width_f, channels_f = fort_img.shape
scale = 147 / 256 * width_f / 133
scaled = cv2.resize(croped, None, fx=scale, fy=scale)
scaled_h, scaled_w, scaled_c = scaled.shape
channels = cv2.split(scaled)
if is_pokemon:
scale_crop_fort = width_f / 156
target_x = (16*scale_crop_fort)
target_y = (28*scale_crop_fort)
fort_img = fort_img[target_x-2:target_x+2+scaled_h, target_y-2:target_y+2+scaled_w]
else:
scale_crop_fort = width_f / 133
target_x = int(12*scale_crop_fort)
target_y = int(24*scale_crop_fort)
fort_img = fort_img[target_x-2:target_x+2+scaled_h, target_y-2:target_y+2+scaled_w]
scaled_no_alpth = cv2.merge([channels[0], channels[1], channels[2]])
transparent_mask = cv2.merge([channels[3], channels[3], channels[3]])
white_pixels = channels[3].sum()/255
result = cv2.matchTemplate(fort_img, scaled_no_alpth, cv2.TM_SQDIFF, mask=transparent_mask)
min_val3, max_val3, min_loc3, max_loc3 = cv2.minMaxLoc(result)
min_val3 = min_val3 / white_pixels
return min_val3