-
Notifications
You must be signed in to change notification settings - Fork 10
/
utils.py
155 lines (133 loc) · 5.5 KB
/
utils.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
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patheffects as path_effects
from skimage.util import img_as_float, random_noise
from skimage.color import rgb2gray
from skimage.filters import gaussian
from skimage.feature import corner_peaks, corner_harris, canny, corner_shi_tomasi, peak_local_max
def img2gray(img):
return img_as_float(rgb2gray(img))
def add_sp_noise(img, amount=0.05):
img = img2gray(img)
img = random_noise(img,
mode='s&p',
amount=amount)
return img
def add_gaussian_noise(img, snr=20):
img = img2gray(img)
k = img.max() - img.min()
deviation = k / 10 ** (snr / 20)
img = random_noise(img,
mode='gaussian',
clip=True,
mean=0,
var=deviation ** 2)
return img
def apply_gaussian_filter(img, sigma=3):
img = gaussian(img,
sigma=sigma,
multichannel=True)
return img
def corners_harris(img, method='k', k=0.05, eps=1e-06, sigma=1.,
min_distance=1, threshold_rel=0.1):
img = img2gray(img)
corners = corner_harris(img,
method=method,
k=k,
eps=eps,
sigma=sigma)
corners = corner_peaks(corners,
min_distance=min_distance,
threshold_abs=None,
threshold_rel=threshold_rel,
exclude_border=True,
indices=True)
return corners
def corners_shi_tomasi(img, sigma=1., min_distance=1, threshold_rel=0.1):
img = img2gray(img)
corners = corner_shi_tomasi(img, sigma)
corners = corner_peaks(corners,
min_distance=min_distance,
threshold_abs=None,
threshold_rel=threshold_rel,
exclude_border=True,
indices=True)
return corners
def edges_canny(img, sigma=1., low_threshold=None, high_threshold=None,
mask=None, use_quantiles=False):
img = img2gray(img)
edges = canny(img, sigma, low_threshold, high_threshold, mask, use_quantiles)
edges_ind = np.array(np.nonzero(edges)).T
return edges_ind
def show_pixel_coords(img, pixel_coords):
plt.figure()
plt.imshow(img, cmap='gray', vmin=0, vmax=1)
plt.title('Pixel corners')
for k in range(len(pixel_coords)):
[x, y] = pixel_coords[k]
plt.plot(y,
x,
'+',
markersize=10,
color='white',
path_effects=[path_effects.withStroke(linewidth=3,
foreground='black')])
plt.show(block=False)
def show_subpix_corners(detector):
plt.figure()
plt.imshow(detector.img, cmap='gray', vmin=0, vmax=1)
plt.title('Subpixel corners coordinates')
[k, n] = np.nonzero(detector.corners_map)
for x, y in zip(k, n):
if not np.isnan(detector.corners_map[x, y]):
plt.plot(detector.y_p_img[x, y],
detector.x_p_img[x, y],
'+',
markersize=10,
color='white',
path_effects=[path_effects.withStroke(linewidth=3,
foreground='black')])
plt.text(detector.y_p_img[x, y],
detector.x_p_img[x, y],
'({:.1f}, {:.1f})'.format(detector.y_p_img[x, y],
detector.x_p_img[x, y]),
color='black',
path_effects=[path_effects.withStroke(linewidth=2,
foreground='white')])
plt.show(block=False)
def show_subpix_edges(detector):
plt.figure()
plt.imshow(detector.img, cmap='gray', vmin=0, vmax=1)
plt.title('Subpixel edges points')
[k, n] = np.nonzero(detector.edges_map)
for x, y in zip(k, n):
if not np.isnan(detector.edges_map[x, y]):
plt.plot(detector.y_p_img[x, y],
detector.x_p_img[x, y],
'.',
markersize=10,
color='white',
path_effects=[path_effects.withStroke(linewidth=3,
foreground='black')])
plt.show(block=False)
def show_corners_angles(detector):
plt.figure()
plt.imshow(detector.img, cmap='gray', vmin=0, vmax=1)
plt.title('Corners angles')
[k, n] = np.nonzero(detector.corners_map)
for x, y in zip(k, n):
if not np.isnan(detector.corners_map[x, y]):
plt.plot(detector.y_p_img[x, y],
detector.x_p_img[x, y],
'+',
markersize=10,
color='white',
path_effects=[path_effects.withStroke(linewidth=3,
foreground='black')])
plt.text(detector.y_p_img[x, y],
detector.x_p_img[x, y],
'${:.1f}^\circ$'.format(np.rad2deg(detector.phi_img[x, y])*2),
color='black',
path_effects=[path_effects.withStroke(linewidth=2,
foreground='white')])
plt.show(block=False)