-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathutils.py
64 lines (48 loc) · 1.65 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
import numpy as np
from scipy import misc
def histogram_features_generator(image_batch, parameters):
"""
Generates features for histogram-based model
:param image_batch: list of 4 views
:param parameters: parameter dictionary
:return: array of histogram features
"""
histogram_features = []
x = [image_batch[0], image_batch[1], image_batch[2], image_batch[3]]
for view in x:
hist_img = []
for i in range(view.shape[0]):
hist_img.append(histogram_generator(view[i], parameters['bins_histogram']))
histogram_features.append(np.array(hist_img))
histogram_features = np.concatenate(histogram_features, axis=1)
return histogram_features
def histogram_generator(img, bins):
"""
Generates feature for histogram-based model (single view)
:param img: Image array
:param bins: number of buns
:return: histogram feature
"""
hist = np.histogram(img, bins=bins, density=False)
hist_result = hist[0] / (hist[0].sum())
return hist_result
def load_images(image_path, view):
"""
Function that loads and preprocess input images
:param image_path: base path to image
:param view: L-CC / R-CC / L-MLO / R-MLO
:return: Batch x Height x Width x Channels array
"""
image = misc.imread(image_path + view + '.png')
image = image.astype(np.float32)
normalize_single_image(image)
image = np.expand_dims(image, axis=0)
image = np.expand_dims(image, axis=3)
return image
def normalize_single_image(image):
"""
Normalize image in-place
:param image: numpy array
"""
image -= np.mean(image)
image /= np.std(image)