[6969be]: / rocaseg / preproc / custom.py

Download this file

56 lines (40 with data), 1.4 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
import logging
import numpy as np
logging.basicConfig()
logger = logging.getLogger('preprocessing_custom')
logger.setLevel(logging.DEBUG)
class Normalize:
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, img, mask=None):
img = img.astype(np.float32)
img = (img - self.mean) / self.std
if mask is not None:
mask = mask.astype(np.float32)
return img, mask
class UnNormalize:
def __init__(self, mean, std):
self.mean = mean
self.std = std
def __call__(self, *args):
return [(a * self.std + self.mean) for a in args]
class PercentileClippingAndToFloat:
"""Change the histogram of image by doing global contrast normalization."""
def __init__(self, cut_min=0.5, cut_max=99.5):
"""
cut_min - lowest percentile which is used to cut the image histogram
cut_max - highest percentile
"""
self.cut_min = cut_min
self.cut_max = cut_max
def __call__(self, img, mask=None):
img = img.astype(np.float32)
lim_low, lim_high = np.percentile(img, [self.cut_min, self.cut_max])
img = np.clip(img, lim_low, lim_high)
img -= lim_low
img /= img.max()
img = img.astype(np.float32)
if mask is not None:
mask = mask.astype(np.float32)
return img, mask