/
utils.py
211 lines (166 loc) · 6.63 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
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
import cv2
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from scipy.stats import multivariate_normal
def mean_image(image, labels):
im_rp = image.reshape(-1, image.shape[2])
labels_1d = np.reshape(labels, -1)
uni = np.unique(labels_1d)
uu = np.zeros(im_rp.shape)
for i in uni:
loc = np.where(labels_1d == i)[0]
mm = np.mean(im_rp[loc, :], axis=0)
uu[loc, :] = mm
return np.reshape(uu, [image.shape[0], image.shape[1], image.shape[2]]).astype('uint8')
def cal_greenness(rgb_image):
hsv = cv2.cvtColor(rgb_image, cv2.COLOR_RGB2HSV).astype(np.float64)
hsv[:, :, 0] = hsv[:, :, 0] / 180.0
hsv[:, :, 1] = hsv[:, :, 1] / 255.0
hsv[:, :, 2] = hsv[:, :, 2] / 255.0
mu = np.array([60.0 / 180.0, 160.0 / 255.0, 200.0 / 255.0])
sigma = np.array([.1, .3, .5])
covariance = np.diag(sigma ** 2)
rv = multivariate_normal(mean=mu, cov=covariance)
z = rv.pdf(hsv)
ref = rv.pdf(mu)
absolute_greenness = z/ref
relative_greenness = (z - np.min(z)) / (np.max(z) - np.min(z) + np.finfo(float).eps)
return absolute_greenness, relative_greenness
def crop_img_from_center(img, crop_size=(512, 512)):
assert(img.shape[0] >= crop_size[0])
assert(img.shape[1] >= crop_size[1])
assert(len(img.shape)==2 or len(img.shape)==3)
cw = img.shape[1] // 2
ch = img.shape[0] // 2
x = cw - crop_size[1] // 2
y = ch - crop_size[0] // 2
if len(img.shape) == 3:
return img[y:y + crop_size[0], x:x + crop_size[1], :]
else:
return img[y:y + crop_size[0], x:x + crop_size[1]]
def crop_img_from_center(img, width=512):
assert(img.shape[1] >= width)
assert (len(img.shape) == 2 or len(img.shape) == 3)
height = img.shape[0] * width // img.shape[1]
cw = img.shape[1] // 2
ch = img.shape[0] // 2
x = cw - width // 2
y = ch - height // 2
if len(img.shape) == 3:
return img[y:y + height, x:x + width, :]
else:
return img[y:y + height, x:x + width]
def save_result_img(save_path, rgb_img, img_labels, mean_img, absolute_greenness, relative_greenness, thresholded):
fig = plt.figure(figsize=(15, 10))
ax = fig.add_subplot(2, 3, 1)
ax.set_title('Original image')
plt.axis('off')
plt.imshow(rgb_img)
ax = fig.add_subplot(2, 3, 2)
ax.set_title('Semantic segmentation')
plt.axis('off')
plt.imshow(img_labels)
ax = fig.add_subplot(2, 3, 3)
ax.set_title('Mean image')
plt.axis('off')
plt.imshow(mean_img)
ax = fig.add_subplot(2, 3, 4)
ax.set_title('Binary mask')
plt.axis('off')
plt.imshow(thresholded, cmap='gray')
ax = fig.add_subplot(2, 3, 5)
ax.set_title('Relative greenness')
plt.axis('off')
plt.imshow(relative_greenness, cmap='gray', vmin=0, vmax=1)
ax = fig.add_subplot(2, 3, 6)
ax.set_title('Absolute greenness')
plt.axis('off')
plt.imshow(absolute_greenness, cmap='gray', vmin=0, vmax=1)
plt.tight_layout()
plt.savefig(save_path, bbox_inches='tight')
plt.show(block=False)
plt.close("all")
def save_result_video(save_path, rgb_img, all_img_labels, all_mean_imgs, all_absolute_greenness, all_relative_greenness, all_masks):
imgs = []
fig = plt.figure(figsize=(15, 10))
for i in range(len(all_img_labels)):
ax1 = fig.add_subplot(1, 3, 1)
ax1.set_title('Original image')
ax1.axis('off')
ax1.imshow(cv2.resize(rgb_img, (512, 512)))
ax2 = fig.add_subplot(1, 3, 2)
ax2.set_title('Semantic segmentation')
ax2.axis('off')
ax2.imshow(cv2.resize(all_img_labels[i], (512, 512)))
ax3 = fig.add_subplot(1, 3, 3)
ax3.set_title('Mean image')
ax3.axis('off')
ax3.imshow(cv2.resize(all_mean_imgs[i], (512, 512)))
# plt.tight_layout()
# imgs.append([ax1, ax2, ax3])
ax4 = fig.add_subplot(2, 3, 4)
ax4.set_title('Binary mask')
ax4.axis('off')
ax4.imshow(cv2.resize(all_masks[i], (512, 512)), cmap='gray')
ax5 = fig.add_subplot(2, 3, 5)
ax5.set_title('Relative greenness')
ax5.axis('off')
ax5.imshow(cv2.resize(all_relative_greenness[i], (512, 512)), cmap='gray', vmin=0, vmax=1)
ax6 = fig.add_subplot(2, 3, 6)
ax6.set_title('Relative greenness')
ax6.axis('off')
ax6.imshow(cv2.resize(all_absolute_greenness[i], (512, 512)), cmap='gray', vmin=0, vmax=1)
plt.tight_layout()
imgs.append([ax1, ax2, ax3, ax4, ax5, ax6])
ani = animation.ArtistAnimation(fig, imgs, interval=80, blit=False)
ani.save(save_path)
def save_result_video_old(save_path, rgb_img, gt_mask, all_img_labels, all_mean_imgs, all_greenness, all_masks):
imgs = []
fig = plt.figure(figsize=(10, 15))
for i in range(len(all_img_labels)):
ax1 = fig.add_subplot(2, 3, 1)
ax1.set_title('Original image')
ax1.axis('off')
ax1.imshow(cv2.resize(rgb_img, (512, 512)))
ax2 = fig.add_subplot(2, 3, 2)
ax2.set_title('Semantic segmentation')
ax2.axis('off')
ax2.imshow(cv2.resize(all_img_labels[i], (512, 512)))
ax5 = fig.add_subplot(2, 3, 3)
ax5.set_title('Mean image')
ax5.axis('off')
ax5.imshow(cv2.resize(all_mean_imgs[i], (512, 512)))
ax4 = fig.add_subplot(2, 3, 4)
ax4.set_title('Ground truth')
ax4.axis('off')
ax4.imshow(cv2.resize(gt_mask, (512, 512)), cmap='gray')
ax3 = fig.add_subplot(2, 3, 5)
ax3.set_title('Binary mask')
ax3.axis('off')
ax3.imshow(cv2.resize(all_masks[i], (512, 512)), cmap='gray')
ax6 = fig.add_subplot(2, 3, 6)
ax6.set_title('Greenness')
ax6.axis('off')
ax6.imshow(cv2.resize(all_greenness[i], (512, 512)), cmap='gray', vmin=0, vmax=1)
plt.tight_layout()
imgs.append([ax1, ax2, ax3, ax4, ax5, ax6])
ani = animation.ArtistAnimation(fig, imgs, interval=80, blit=False)
ani.save(save_path)
def color_coded_map(gt, det):
gt = gt.astype(bool)
det = det.astype(bool)
green_area = np.logical_and(det, gt)
red_area = np.logical_and(det, np.logical_not(gt))
blue_area = np.logical_and(np.logical_not(det), gt)
color_map = np.zeros((gt.shape[0], gt.shape[1], 3), dtype=np.uint8)
tmp_map = np.zeros((gt.shape[0], gt.shape[1]), dtype=np.uint8)
tmp_map[green_area] = 255
color_map[:, :, 1] = tmp_map
tmp_map = np.zeros((gt.shape[0], gt.shape[1]), dtype=np.uint8)
tmp_map[red_area] = 255
color_map[:, :, 2] = tmp_map
tmp_map = np.zeros((gt.shape[0], gt.shape[1]), dtype=np.uint8)
tmp_map[blue_area] = 255
color_map[:, :, 0] = tmp_map
return color_map