forked from sarahmu/Histopathology-Imaging
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
74 lines (66 loc) · 2.02 KB
/
utils.py
File metadata and controls
74 lines (66 loc) · 2.02 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
import time
import math
import numpy as np
import matplotlib.pyplot as plt
plt.switch_backend('agg')
import matplotlib.gridspec as gridspec
from PIL import Image
def local_clock(shift=-25200):
return time.asctime(time.localtime(time.time() + shift))
def chunk(l, chunks):
chunk_list = []
chunk_size = math.floor(len(l) / chunks)
for i in range(chunks - 1):
chunk_list.append(l[chunk_size*i:chunk_size*(i+1)])
chunk_list.append(l[chunk_size*(chunks - 1):])
return chunk_list
def preprocess(img):
img = (img / 255.0) * 2 - 1
return img
def postprocess(img):
img = (img + 1) / 2 * 255.0
return img
def show_images(images, post_process=False, save=False, filepath=None):
N, H, W, C = images.shape
sqrtN = int(np.ceil(np.sqrt(N)))
if post_process:
images = postprocess(images)
images = images.astype(np.uint8)
fig = plt.figure(figsize=(sqrtN, sqrtN))
gs = gridspec.GridSpec(sqrtN, sqrtN)
gs.update(wspace=0.05, hspace=0.05)
for i in range(N):
img = images[i,:,:,:]
ax = plt.subplot(gs[i])
plt.axis('off')
ax.set_xticklabels([])
ax.set_yticklabels([])
ax.set_aspect('equal')
if C == 1:
plt.imshow(img.reshape((H, W)), cmap='gray')
else:
plt.imshow(img)
if save and filepath != None:
fig.savefig(filepath)
plt.close(fig)
return
def save_image(image, filepath, post_process=False):
if post_process:
image = postprocess(image)
image = image.astype(np.uint8)
image_object = Image.fromarray(image)
image_object.save(filepath)
# def save_image(image, filepath, post_process=False, width=4, height=4):
# H, W, C = image.shape
# if post_process:
# image = postprocess(image)
# image = image.astype(np.uint8)
# fig = plt.figure(figsize=(width, height))
# plt.axis('off')
# if C == 1:
# plt.imshow(image.reshape((H, W)), camp='gray')
# else:
# plt.imshow(image)
# fig.savefig(filepath)
# plt.close(fig)
# return