-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathshape_anomaly.py
More file actions
87 lines (70 loc) · 3.15 KB
/
shape_anomaly.py
File metadata and controls
87 lines (70 loc) · 3.15 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
import warnings
with warnings.catch_warnings():
warnings.filterwarnings("ignore",category=FutureWarning)
import os
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
from smallestenclosingcircle import make_circle
def get_circle_impurity_score(markers, imp_boxes, areas, indices):
scores = np.full(imp_boxes.shape[0], np.infty)
for impurity in indices:
impurity_shape = np.argwhere(markers == impurity + 2)
circle = make_circle(impurity_shape)
circle_area = np.pi * circle[2] ** 2
scores[impurity] = (circle_area - areas[impurity]) / circle_area
return scores
def color_close_to_cirlce(img, markers, indices, scores, areas, save_dir_path):
blank_image = np.zeros(img.shape, np.uint8)
blank_image[:, :] = (255, 255, 255)
jet = plt.get_cmap('jet')
num_under_thresh = 0
for impurity in indices:
# show only under threshold:
# if scores[impurity] <= 0.3 and areas[impurity] > 50:
if areas[impurity] > 50:
num_under_thresh += 1
color = jet(scores[impurity])
blank_image[markers == impurity + 2] = (color[0] * 255, color[1] * 255, color[2] * 255)
else:
blank_image[markers == impurity + 2] = (0, 0, 0)
print("under threshold: {}".format(num_under_thresh))
figure = plt.figure("Colored Circles")
plt.imshow(blank_image, cmap='jet')
plt.colorbar()
plt.clim(0, 1)
plt.title("The color is determined by " + r"$\frac{(S(circle) - S(impurity))}{S(circle)}$" + " , where circle is the minimal circle "
"that covers the impurity")
if not os.path.exists(save_dir_path):
os.makedirs(save_dir_path)
# matplotlib.rcParams.update({'font.size': 100})
figure.set_size_inches(120, 80)
plt.savefig(save_dir_path + "/" + "circle_area_diff.png")
plt.show()
def color_circle_diff_all_impurities(img, markers, imp_boxes, areas, indices, save_dir_path):
scores = get_circle_impurity_score(markers, imp_boxes, areas, indices)
indx = np.argwhere(areas>50)
# scores = np.minimum(scores, 1e6)
# scores = np.maximum(scores, 1e-5)
# normalized_scores = (scores - np.min(scores)) / np.ptp(scores)
fig = plt.figure("circle_diff")
step = 0.02
refined_bins = np.arange(0, 1 + step, step)
plt.hist(scores[indx], bins=refined_bins)
plt.title("circle diff")
plt.savefig(save_dir_path + "/circle_differences.png" , dpi=fig.dpi)
color_close_to_cirlce(img, markers, indices, scores, areas, save_dir_path)
def color_shape_anomaly(img, markers, indices, scores, imp_boxes):
blank_image = np.zeros(img.shape, np.uint8)
blank_image[:, :] = (255, 255, 255)
jet = plt.get_cmap('jet')
for impurity in indices:
color = jet(scores[impurity])
blank_image[markers == impurity + 2] = (color[0] * 255, color[1] * 255, color[2] * 255)
plt.figure("Colored shape anomaly")
plt.imshow(blank_image, cmap='jet')
plt.colorbar()
plt.clim(0, 1)
plt.title("The color is determined by the neural network")
plt.show()
plt.savefig('colored_shape_anomaly.png')