forked from cal-pratt/SheetVision
-
Notifications
You must be signed in to change notification settings - Fork 0
/
best_fit.py
40 lines (36 loc) · 1.31 KB
/
best_fit.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
import cv2
import matplotlib.pyplot as plt
import numpy as np
def fit(img, templates, start_percent, stop_percent, threshold):
img_width, img_height = img.shape[::-1]
best_location_count = -1
best_locations = []
best_scale = 1
plt.axis([0, 2, 0, 1])
plt.show(block=False)
x = []
y = []
for scale in [i/100.0 for i in range(start_percent, stop_percent + 1, 3)]:
locations = []
location_count = 0
for template in templates:
template = cv2.resize(template, None,
fx = scale, fy = scale, interpolation = cv2.INTER_CUBIC)
result = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
result = np.where(result >= threshold)
location_count += len(result[0])
locations += [result]
print("scale: {0}, hits: {1}".format(scale, location_count))
x.append(location_count)
y.append(scale)
plt.plot(y, x)
plt.pause(0.00001)
if (location_count > best_location_count):
best_location_count = location_count
best_locations = locations
best_scale = scale
plt.axis([0, 2, 0, best_location_count])
elif (location_count < best_location_count):
pass
plt.close()
return best_locations, best_scale