-
Notifications
You must be signed in to change notification settings - Fork 10
/
transformations.py
64 lines (48 loc) · 1.74 KB
/
transformations.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
from utils import *
from skimg_local import rgb2hsv, hsv2rgb
def select_channels(img_RGB):
"""
Returns the R' and V* channels for a skin lesion image.
Args:
img_RGB (np.array): The RGB image of the skin lesion
"""
img_RGB_norm = img_RGB / 255.0
img_r_norm = img_RGB_norm[..., 0] / (
img_RGB_norm[..., 0] + img_RGB_norm[..., 1] + img_RGB_norm[..., 2]
)
img_v = np.max(img_RGB, axis=2)
return (img_r_norm, img_v)
def calculate_GRAY(img_RGB):
"""
Returns the single channel grayscale representation of
the skin lesion.
Args:
img_RGB (np.array): The RGB image of the skin lesion
"""
img_torch = torch.from_numpy(img_RGB) + eps
X = torch.log(torch.reshape(img_torch, (-1, 3)))
X_mean = torch.mean(X, 0)
X -= X_mean.expand_as(X)
U, S, V = torch.svd(torch.t(X))
C = torch.mm(X, U[..., :1])
C_reshaped = torch.reshape(C, (128, 128, -1))[..., 0]
C_reshaped_np = C_reshaped.cpu().detach().numpy()
return C_reshaped_np
def calculate_Intrinsic_SA(img_RGB):
"""
Returns the illumination invariant 'intrinsic' image and
the shading attentuated representation for the skin lesion.
Args:
img_RGB (np.array): The RGB image of the skin lesion
"""
img_torch = torch.from_numpy(img_RGB) + eps
angle, projected = entropy_intrinsic(img_torch, calculate_intrinsic_img=True)
projected_np = projected.cpu().detach().numpy()
img_RGB_norm = img_RGB / 255.0
projected_norm = projected_np / 255.0
img_HSV = rgb2hsv(img_RGB)
matched = hist_match(img_HSV[..., 2], projected_norm)
img_HSV[..., 2] = matched
img_RGB_SA_norm = hsv2rgb(img_HSV)
img_RGB_SA = img_RGB_SA_norm * 255
return (projected_np, img_RGB_SA)