-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
142 lines (108 loc) · 5.33 KB
/
test.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
#!/usr/bin/python3
import numpy as np
from matplotlib import pyplot as plt
import cv2
from os import walk
import utilities
def cmask(center, radius, array):
# print(center)
# print(radius)
a, b = center
ny, nx, _ = array.shape
# ny, nx = array.shape
y, x = np.ogrid[-b:ny-b, -a:nx-a]
return x*x + y*y <= radius*radius
def minimize_sum_of_squared_gradients(img, expected_radius):
center_x, center_y = int(len(img[0]) / 2), int(len(img) / 2)
# to_show = np.zeros((img.shape[0], 2*img.shape[1], 3))
# img_copy = img.copy()
# to_show[:,:img.shape[1]] = img.copy()
# cv2.circle(to_show, (int(center_x), int(center_y)), int(expected_radius), (1, 1, 1))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_avg = np.average(img)
min_sum = np.sum(np.power(img[center_y-expected_radius:center_y+expected_radius, center_x-expected_radius:center_x+expected_radius]-img_avg, 2))
best_param = (expected_radius, center_x, center_y)
for d_x in range(-expected_radius, expected_radius):
cur_x = center_x + d_x
for d_y in range(-expected_radius, expected_radius):
cur_y = center_y + d_y
# utilities.show(img[cur_y-expected_radius:cur_y+expected_radius, cur_x-expected_radius:cur_x+expected_radius])
this_sum = np.sum(np.power(img[cur_y-expected_radius:cur_y+expected_radius, cur_x-expected_radius:cur_x+expected_radius]-img_avg, 2))
if this_sum > min_sum:
min_sum = this_sum
best_param = (expected_radius, cur_x, cur_y)
# print(min_sum, best_param)
# cv2.circle(img_copy, (int(best_param[1]), int(best_param[2])), int(best_param[0]), (1, 1, 1))
# to_show[:,img.shape[1]:,:] = img_copy.copy()
# to_show[:,img.shape[1]:img.shape[1]+1,:] = (0,0,0)
#
# import time
# cv2.imwrite("/home/anders/UNIK4690/project/report/ball_detection_pipeline/{}.png".format(time.time()), (to_show*255).astype(np.uint8))
# utilities.show(to_show)
return min_sum, best_param[0], best_param[1]-center_x, best_param[2]-center_y
# Returns average of squared gradients
def sum_of_squared_gradients(img, mask=None, x_gradient_squared=None, y_gradient_squared=None):
if len(img) == 0 or len(img[0]) == 0:
return float("INF")
if x_gradient_squared is None or y_gradient_squared is None:
x_first = img[:-1,:-1].astype(np.int64)
x_last = img[:-1,1:].astype(np.int64)
y_first = img[:-1,:-1].astype(np.int64)
y_last = img[1:,:-1].astype(np.int64)
x_gradient_squared = np.power(x_last - x_first, 2)
y_gradient_squared = np.power(y_last - y_first, 2)
if mask is None:
x_sum = np.sum(x_gradient_squared)
y_sum = np.sum(y_gradient_squared)
return (x_sum + y_sum) / (len(img[0])+len(img))
else:
# print()
# print(img[mask])
# print(img)
sum_gradient = np.sum(x_gradient_squared[mask]) + np.sum(y_gradient_squared[mask])
num_gradients = np.sum(mask)
# We want to favor bigger matches, so we get a greedy search
return sum_gradient / (((num_gradients/3.14)**0.5)**1.5)
if __name__ == "__main__":
from image import Image
from transformer import Transformer, mean_diff
im = Image("2016-05-02_12:05:59.png", histogram_equalization="rg_chromaticity")
cv2.imwrite("/home/anders/UNIK4690/project/report/playground_detection_pipeline/rg_chromaticity_example_before.png", im.original_bgr)
cv2.imwrite("/home/anders/UNIK4690/project/report/playground_detection_pipeline/rg_chromaticity_example_after.png", im.get_bgr(np.uint8))
exit(0)
transformer = Transformer(filename="playground_transformer_state.json")
try:
import os
filenames = []
for cur in os.walk(os.path.join(utilities.get_project_directory(), "images/microsoft_cam/24h/south/")):
filenames = cur[2]
break
filenames.sort()
for file in filenames:
try:
import datetime
date = datetime.datetime.strptime(file, "%Y-%m-%d_%H:%M:%S.png")
if date < datetime.datetime(2016, 4, 13, 7, 5):
# if date < datetime.datetime(2016, 4, 12, 19, 0):
continue
image = Image(os.path.join(utilities.get_project_directory(), "images/microsoft_cam/24h/south/", file))
except FileNotFoundError:
continue
size = 40
best_transform = utilities.as_uint8(transformer.get_playground_transformation(image))
box = utilities.get_box(best_transform.copy(), side=size)
box_hist = utilities.get_histogram(box)
tot = np.zeros(best_transform.shape[:2]).astype(np.float32)
for row in range(0, 1080, size):
for col in range(0, 1920, size):
box = best_transform[row:row+size, col:col+size]
cur_box_hist = utilities.get_histogram(box)
tot[row:row+size, col:col+size] = cv2.compareHist(box_hist, cur_box_hist, cv2.HISTCMP_CORREL)
tot -= np.amin(tot)
tot *= 1/np.amax(tot)
utilities.show(tot, "Finished", time_ms=1)
# utilities.show(best_transform, time_ms=10, text=img.filename)
except Exception as e:
import traceback
print(e)
traceback.print_tb(e.__traceback__)