-
Notifications
You must be signed in to change notification settings - Fork 20
/
generateData.py
215 lines (171 loc) · 7.56 KB
/
generateData.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import os
import os.path as osp
import tigre
from tigre.utilities.geometry import Geometry
from tigre.utilities import gpu
import numpy as np
import yaml
import pickle
import scipy.io
import scipy.ndimage.interpolation
from tigre.utilities import CTnoise
import cv2
import matplotlib.pyplot as plt
import argparse
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
def config_parser():
parser = argparse.ArgumentParser()
parser.add_argument("--ctName", default="chest", type=str,
help="Name of CT")
parser.add_argument("--outputName", default="chest_50", type=str,
help="Name of output data")
parser.add_argument("--dataFolder", default="raw", type=str,
help="folder of raw data")
parser.add_argument("--outputFolder", default="./data", type=str,
help="folder of output data")
return parser
def main():
parser = config_parser()
args = parser.parse_args()
dataType = args.ctName
dataFolder = args.dataFolder
outputName = args.outputName
outputFolder = args.outputFolder
matPath = f"./dataGenerator/{dataFolder}/{dataType}/img.mat"
configPath = f"./dataGenerator/{dataFolder}/{dataType}/config.yml"
outputPath = osp.join(outputFolder, f"{outputName}.pickle")
generator(matPath, configPath, outputPath, True)
# %% Geometry
class ConeGeometry_special(Geometry):
"""
Cone beam CT geometry.
"""
def __init__(self, data):
Geometry.__init__(self)
# VARIABLE DESCRIPTION UNITS
# -------------------------------------------------------------------------------------
self.DSD = data["DSD"] / 1000 # Distance Source Detector (m)
self.DSO = data["DSO"] / 1000 # Distance Source Origin (m)
# Detector parameters
self.nDetector = np.array(data["nDetector"]) # number of pixels (px)
self.dDetector = np.array(data["dDetector"]) / 1000 # size of each pixel (m)
self.sDetector = self.nDetector * self.dDetector # total size of the detector (m)
# Image parameters
self.nVoxel = np.array(data["nVoxel"][::-1]) # number of voxels (vx)
self.dVoxel = np.array(data["dVoxel"][::-1]) / 1000 # size of each voxel (m)
self.sVoxel = self.nVoxel * self.dVoxel # total size of the image (m)
# Offsets
self.offOrigin = np.array(data["offOrigin"][::-1]) / 1000 # Offset of image from origin (m)
self.offDetector = np.array(
[data["offDetector"][1], data["offDetector"][0], 0]) / 1000 # Offset of Detector (m)
# Auxiliary
self.accuracy = data["accuracy"] # Accuracy of FWD proj (vx/sample) # noqa: E501
# Mode
self.mode = data["mode"] # parallel, cone ...
self.filter = data["filter"]
def convert_to_attenuation(data: np.array, rescale_slope: float, rescale_intercept: float):
"""
CT scan is measured using Hounsfield units (HU). We need to convert it to attenuation.
The HU is first computed with rescaling parameters:
HU = slope * data + intercept
Then HU is converted to attenuation:
mu = mu_water + HU/1000x(mu_water-mu_air)
mu_water = 0.206
mu_air=0.0004
Args:
data (np.array(X, Y, Z)): CT data.
rescale_slope (float): rescale slope.
rescale_intercept (float): rescale intercept.
Returns:
mu (np.array(X, Y, Z)): attenuation map.
"""
HU = data * rescale_slope + rescale_intercept
mu_water = 0.206
mu_air = 0.0004
mu = mu_water + (mu_water - mu_air) / 1000 * HU
# mu = mu * 100
return mu
def loadImage(dirname, nVoxels, convert, rescale_slope, rescale_intercept, normalize=True):
"""
Load CT image.
"""
if nVoxels is None:
nVoxels = np.array((256, 256, 256))
test_data = scipy.io.loadmat(dirname)
# Loads data in F_CONTIGUOUS MODE (column major), convert to Row major
image_ori = test_data["img"].astype(np.float32)
if convert:
print("Convert from HU to attenuation")
image = convert_to_attenuation(image_ori, rescale_slope, rescale_intercept)
else:
image = image_ori
imageDim = image.shape
zoom_x = nVoxels[0] / imageDim[0]
zoom_y = nVoxels[1] / imageDim[1]
zoom_z = nVoxels[2] / imageDim[2]
if zoom_x != 1.0 or zoom_y != 1.0 or zoom_z != 1.0:
print(f"Resize ct image from {imageDim[0]}x{imageDim[1]}x{imageDim[2]} to "
f"{nVoxels[0]}x{nVoxels[1]}x{nVoxels[2]}")
image = scipy.ndimage.interpolation.zoom(
image, (zoom_x, zoom_y, zoom_z), order=3, prefilter=False
)
image_max = np.max(image)
image_min = np.min(image)
image_mean = np.mean(image)
print("Range of CT image is [%f, %f], mean: %f" % (image_min, image_max, image_mean))
if normalize and image_min !=0 and image_max != 1:
print("Normalize range to [0, 1]")
image = (image - image_min) / (image_max - image_min)
return image
def generator(matPath, configPath, outputPath, show=False):
"""
Generate projections given CT image and configuration.
"""
# Load configuration
with open(configPath, "r") as handle:
data = yaml.safe_load(handle)
# Load CT image
geo = ConeGeometry_special(data)
img = loadImage(matPath, data["nVoxel"], data["convert"],
data["rescale_slope"], data["rescale_intercept"], data["normalize"])
data["image"] = img.copy()
# plt.figure()
# plt.imshow(img[:,:,0])
# plt.show()
# Generate training images
if data["randomAngle"] is False:
data["train"] = {"angles": np.linspace(0, data["totalAngle"] / 180 * np.pi, data["numTrain"]+1)[:-1] + data["startAngle"]/ 180 * np.pi}
else:
data["train"] = {"angles": np.sort(np.random.rand(data["numTrain"]) * data["totalAngle"] / 180 * np.pi) + data["startAngle"]/ 180 * np.pi}
projections = tigre.Ax(np.transpose(img, (2, 1, 0)).copy(), geo, data["train"]["angles"])[:, ::-1, :]
if data["noise"] and data["normalize"]:
print("Add noise to projections")
noise_projections = CTnoise.add(projections, Poisson=1e5, Gaussian=np.array([0, 10]))
noise_projections[noise_projections < 0.0] = 0.0
data["train"]["projections"] = noise_projections
else:
data["train"]["projections"] = projections
# Generate validation images
data["val"] = {"angles": np.sort(np.random.rand(data["numVal"]) * 180 / 180 * np.pi) + data["startAngle"]/ 180 * np.pi}
projections = tigre.Ax(np.transpose(img, (2, 1, 0))
.copy(), geo, data["val"]["angles"])[:, ::-1, :]
if data["noise"] != 0 and data["normalize"]:
print("Add noise to projections")
noise_projections = CTnoise.add(projections, Poisson=1e5, Gaussian=np.array([0, data["noise"]]))
data["val"]["projections"] = noise_projections
else:
data["val"]["projections"] = projections
if show:
print("Display ct image")
tigre.plotimg(img.transpose((2,0,1)), dim="z")
print("Display training images")
tigre.plotproj(data["train"]["projections"][:, ::-1, :])
print("Display validation images")
tigre.plotproj(data["val"]["projections"][:, ::-1, :])
# Save data
os.makedirs(osp.dirname(outputPath), exist_ok=True)
with open(outputPath, "wb") as handle:
pickle.dump(data, handle, pickle.HIGHEST_PROTOCOL)
print(f"Save files in {outputPath}")
if __name__ == "__main__":
main()