-
Notifications
You must be signed in to change notification settings - Fork 0
/
Preprocess.py
118 lines (95 loc) · 3.88 KB
/
Preprocess.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Feb 5 20:55:11 2019
@author: weidongming
"""
import numpy as np
import SimpleITK as sitk
from matplotlib import pyplot as plt
class Preprocess(object):
def __init__(self):
#threshold cut
self.threshold_min = 0
self.threshold_max = 1700
# histogram normalization
self.nor_min = 0
self.nor_max = 1700
# histogram plot
self.num_bins = 10
# number of grayscale dilation
self.num_dilation = 6
def threscut(self, subject_data):# 1000,2000
# CT image threshold cut
subject_data[subject_data > self.threshold_max] = self.threshold_max
subject_data[subject_data < self.threshold_min] = self.threshold_min
return subject_data
def normalize_3D(self, img_3D):
""" histogram normalized to 0~1"""
data_3D = img_3D - self.nor_min
data_3D = data_3D / np.float32(self.nor_max-self.nor_min)
#data_3D = img_3D - img_3D.min()
#data_3D = data_3D / np.float32(img_3D.max())
return np.asarray(data_3D, np.float32)
def hist_of_3d_array(self, array_3d):
array_1d = np.reshape(array_3d, [np.size(array_3d)])
num_bins=self.num_bins
hist = plt.hist(array_1d, num_bins, facecolor='blue', alpha=0.5)
return(hist)
def data_augumentation_flip(self, array_3d):
# flip
flip_index = np.random.randint(0,3)
if flip_index == 0:
array_3d = array_3d[::-1,:,:]
if flip_index == 1:
array_3d = array_3d[:,::-1,:]
if flip_index == 2:
array_3d = array_3d[:,:,::-1]
return array_3d
def data_augumentation_noise(self, img_3d):
# Create a list of intensity modifying filters, which we apply to the given images
filter_list = []
# Smoothing filters
filter_list.append(sitk.SmoothingRecursiveGaussianImageFilter())
filter_list[-1].SetSigma(2.0)
filter_list.append(sitk.DiscreteGaussianImageFilter())
filter_list[-1].SetVariance(4.0)
filter_list.append(sitk.BilateralImageFilter())
filter_list[-1].SetDomainSigma(4.0)
filter_list[-1].SetRangeSigma(8.0)
filter_list.append(sitk.MedianImageFilter())
filter_list[-1].SetRadius(8)
# Noise filters using default settings
# Filter control via SetMean, SetStandardDeviation.
filter_list.append(sitk.AdditiveGaussianNoiseImageFilter())
filter_list[-1].SetMean(0.2)
filter_list[-1].SetStandardDeviation(0.7)
# Filter control via SetProbability
filter_list.append(sitk.SaltAndPepperNoiseImageFilter())
# Filter control via SetScale
filter_list.append(sitk.ShotNoiseImageFilter())
# Filter control via SetStandardDeviation
filter_list.append(sitk.SpeckleNoiseImageFilter())
filter_list.append(sitk.AdaptiveHistogramEqualizationImageFilter())
filter_list[-1].SetAlpha(1.0)
filter_list[-1].SetBeta(0.0)
filter_list.append(sitk.AdaptiveHistogramEqualizationImageFilter())
filter_list[-1].SetAlpha(0.0)
filter_list[-1].SetBeta(1.0)
f = filter_list[4]
aug_image = f.Execute(img_3d)
return aug_image
def dilation(self, img_3d):
# GrayeScale Dilation
d = sitk.GrayscaleDilateImageFilter()
for i in range(self.num_dilation):
img_3d = d.Execute(img_3d)
return img_3d
def pre_process(self, img_3d):
# user defined pipeline
img_data = sitk.GetArrayFromImage(img_3d)
img_data = self.threscut(img_data)
img_data = self.normalize_3D(img_data)
img = sitk.GetImageFromArray(img_data)
img = self.dilation(img)
return img