-
Notifications
You must be signed in to change notification settings - Fork 1
/
image_utils.py
executable file
·91 lines (61 loc) · 2.32 KB
/
image_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
# Authors:
# Christian F. Baumgartner ([email protected])
# Lisa M. Koch ([email protected])
import numpy as np
from skimage import measure
import logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(message)s')
try:
import cv2
except:
logging.warning('Could not import opencv. Augmentation functions will be unavailable.')
else:
def rotate_image(img, angle, interp=cv2.INTER_LINEAR):
rows, cols = img.shape[:2]
rotation_matrix = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, 1)
return cv2.warpAffine(img, rotation_matrix, (cols, rows), flags=interp)
def resize_image(im, size, interp=cv2.INTER_LINEAR):
im_resized = cv2.resize(im, (size[1], size[0]), interpolation=interp) # swap sizes to account for weird OCV API
return im_resized
def convert_to_uint8(image):
image = image - image.min()
image = 255.0*np.divide(image.astype(np.float32),image.max())
return image.astype(np.uint8)
def normalise_image(image):
'''
make image zero mean and unit standard deviation
'''
img_o = np.float32(image.copy())
m = np.mean(img_o)
s = np.std(img_o)
return np.divide((img_o - m), s)
def normalise_images(X):
'''
Helper for making the images zero mean and unit standard deviation i.e. `white`
'''
X_white = np.zeros(X.shape, dtype=np.float32)
for ii in range(X.shape[0]):
Xc = X[ii,:,:,:]
mc = Xc.mean()
sc = Xc.std()
Xc_white = np.divide((Xc - mc), sc)
X_white[ii,:,:,:] = Xc_white
return X_white.astype(np.float32)
def reshape_2Dimage_to_tensor(image):
return np.reshape(image, (1,image.shape[0], image.shape[1],1))
def keep_largest_connected_components(mask):
'''
Keeps only the largest connected components of each label for a segmentation mask.
'''
out_img = np.zeros(mask.shape, dtype=np.uint8)
for struc_id in [1, 2, 3]:
binary_img = mask == struc_id
blobs = measure.label(binary_img, connectivity=1)
props = measure.regionprops(blobs)
if not props:
continue
area = [ele.area for ele in props]
largest_blob_ind = np.argmax(area)
largest_blob_label = props[largest_blob_ind].label
out_img[blobs == largest_blob_label] = struc_id
return out_img