|
a |
|
b/rocaseg/preproc/custom.py |
|
|
1 |
import logging |
|
|
2 |
import numpy as np |
|
|
3 |
|
|
|
4 |
|
|
|
5 |
logging.basicConfig() |
|
|
6 |
logger = logging.getLogger('preprocessing_custom') |
|
|
7 |
logger.setLevel(logging.DEBUG) |
|
|
8 |
|
|
|
9 |
|
|
|
10 |
class Normalize: |
|
|
11 |
def __init__(self, mean, std): |
|
|
12 |
self.mean = mean |
|
|
13 |
self.std = std |
|
|
14 |
|
|
|
15 |
def __call__(self, img, mask=None): |
|
|
16 |
img = img.astype(np.float32) |
|
|
17 |
img = (img - self.mean) / self.std |
|
|
18 |
|
|
|
19 |
if mask is not None: |
|
|
20 |
mask = mask.astype(np.float32) |
|
|
21 |
return img, mask |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
class UnNormalize: |
|
|
25 |
def __init__(self, mean, std): |
|
|
26 |
self.mean = mean |
|
|
27 |
self.std = std |
|
|
28 |
|
|
|
29 |
def __call__(self, *args): |
|
|
30 |
return [(a * self.std + self.mean) for a in args] |
|
|
31 |
|
|
|
32 |
|
|
|
33 |
class PercentileClippingAndToFloat: |
|
|
34 |
"""Change the histogram of image by doing global contrast normalization.""" |
|
|
35 |
def __init__(self, cut_min=0.5, cut_max=99.5): |
|
|
36 |
""" |
|
|
37 |
cut_min - lowest percentile which is used to cut the image histogram |
|
|
38 |
cut_max - highest percentile |
|
|
39 |
""" |
|
|
40 |
self.cut_min = cut_min |
|
|
41 |
self.cut_max = cut_max |
|
|
42 |
|
|
|
43 |
def __call__(self, img, mask=None): |
|
|
44 |
img = img.astype(np.float32) |
|
|
45 |
lim_low, lim_high = np.percentile(img, [self.cut_min, self.cut_max]) |
|
|
46 |
img = np.clip(img, lim_low, lim_high) |
|
|
47 |
|
|
|
48 |
img -= lim_low |
|
|
49 |
img /= img.max() |
|
|
50 |
|
|
|
51 |
img = img.astype(np.float32) |
|
|
52 |
if mask is not None: |
|
|
53 |
mask = mask.astype(np.float32) |
|
|
54 |
|
|
|
55 |
return img, mask |