-
Notifications
You must be signed in to change notification settings - Fork 204
/
Copy pathsingle_pass_algorithms.py
203 lines (156 loc) · 7.06 KB
/
single_pass_algorithms.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import copy
import numpy as np
from tigre.utilities.Atb import Atb
from tigre.utilities.filtering import filtering
def FDK(proj, geo, angles, **kwargs):
"""
solves CT image reconstruction.
:param proj: np.array(dtype=float32),
Data input in the form of 3d
:param geo: tigre.utilities.geometry.Geometry
Geometry of detector and image (see examples/Demo code)
:param angles: np.array(dtype=float32)
Angles of projection, shape = (nangles,3) or (nangles,)
:param filter: str
Type of filter used for backprojection
opts: "ram_lak" (default)
"shep_logan"
"cosine"
"hamming"
"hann"
:param verbose: bool
Feedback print statements for algorithm progress
:param kwargs: dict
keyword arguments
:return: np.array(dtype=float32)
Usage:
-------
>>> import tigre
>>> import tigre.algorithms as algs
>>> import numpy
>>> from tigre.demos.Test_data import data_loader
>>> geo = tigre.geometry(mode='cone',default_geo=True,
>>> nVoxel=np.array([64,64,64]))
>>> angles = np.linspace(0,2*np.pi,100)
>>> src_img = data_loader.load_head_phantom(geo.nVoxel)
>>> proj = tigre.Ax(src_img,geo,angles)
>>> output = algs.FDK(proj,geo,angles)
tigre.demos.run() to launch ipython notebook file with examples.
--------------------------------------------------------------------
This file is part of the TIGRE Toolbox
Copyright (c) 2015, University of Bath and
CERN-European Organization for Nuclear Research
All rights reserved.
License: Open Source under BSD.
See the full license at
https://github.com/CERN/TIGRE/license.txt
Contact: [email protected]
Codes: https://github.com/CERN/TIGRE/
--------------------------------------------------------------------
Coded by: MATLAB (original code): Ander Biguri
PYTHON : Reuben Lindroos
"""
verbose = kwargs["verbose"] if "verbose" in kwargs else False
gpuids = kwargs["gpuids"] if "gpuids" in kwargs else None
dowang = kwargs["dowang"] if "dowang" in kwargs else True
def zeropadding(proj, geo):
zgeo = copy.deepcopy(geo)
padwidth = int(2 * geo.offDetector[1] / geo.dDetector[1])
zgeo.offDetector[1] = geo.offDetector[1] - \
padwidth / 2 * geo.dDetector[1]
zgeo.nDetector[1] = abs(padwidth) + geo.nDetector[1]
zgeo.sDetector[1] = zgeo.nDetector[1] * zgeo.dDetector[1]
theta = (geo.sDetector[1] / 2 - abs(geo.offDetector[1])
) * np.sign(geo.offDetector[1])
if geo.offDetector[1] > 0:
zproj = np.zeros(
(proj.shape[0] , proj.shape[1], proj.shape[2]+ padwidth), dtype=proj.dtype)
for ii in range(proj.shape[0]):
zproj[ii,:, :] = np.concatenate(
(np.zeros((proj.shape[1], padwidth)), proj[ii,:,:]), axis=1)
else:
zproj = np.zeros(
(proj.shape[0] , proj.shape[1] , proj.shape[2]+ abs(padwidth)), dtype=proj.dtype)
for ii in range(proj.shape[0]):
zproj[ii, :, :] = np.concatenate(
(proj[ ii,:, :], np.zeros((proj.shape[1], abs(padwidth)))), axis=1)
return zproj, zgeo, theta
def preweighting2(proj, geo, theta):
"""
Preweighting using Wang function
:param proj: np.array(dtype=float32),
Data input in the form of 3d
:param geo: tigre.utilities.geometry.Geometry
Geometry of detector and image (see examples/Demo code)
:param theta: np.array(dtype=float32)
Angles of projection, shape = (nangles,3) or (nangles,)
:return: np.array(dtype=float32), np.array(dtype=float32)
"""
offset = geo.offDetector[1]
offset = offset + (geo.DSD / geo.DSO) #* geo.COR
us = np.arange(-geo.nDetector[1]/2 + 0.5, geo.nDetector[1] /
2 - 0.5 + 1) * geo.dDetector[1] + abs(offset)
us = us * geo.DSO / geo.DSD
abstheta = np.abs(theta * geo.DSO / geo.DSD)
w = np.ones(proj[0, :, :].shape)
for ii in range(geo.nDetector[1]):
t = us[ii]
if np.abs(t) <= abstheta:
w[:,ii] = 0.5 * (np.sin((np.pi / 2) * np.arctan(t /
geo.DSO) / (np.arctan(abstheta / geo.DSO))) + 1)
if t < -abstheta:
w[:,ii] = 0
if theta < 0:
w = np.fliplr(w)
proj_w = proj.copy() # preallocation
for ii in range(proj.shape[0]):
proj_w[ii, :, :,] = proj[ii, :, :] * w * 2
return proj_w, w
if not np.any(geo.offDetector):
dowang = False
if dowang:
if verbose:
print('FDK: applying detector offset weights')
# Zero-padding to avoid FFT-induced aliasing
zproj, zgeo, theta = zeropadding(proj, geo)
# Preweighting using Wang function to save memory
proj, _ = preweighting2(zproj, zgeo, theta)
# Replace original proj and geo
# proj = proj_w;
geo = zgeo
geo = copy.deepcopy(geo)
geo.check_geo(angles)
geo.checknans()
geo.filter = kwargs["filter"] if "filter" in kwargs else None
# Weight
proj_filt = np.zeros(proj.shape, dtype=np.float32)
xv = np.arange((-geo.nDetector[1] / 2) + 0.5,
1 + (geo.nDetector[1] / 2) - 0.5) * geo.dDetector[1]
yv = np.arange((-geo.nDetector[0] / 2) + 0.5,
1 + (geo.nDetector[0] / 2) - 0.5) * geo.dDetector[0]
(yy, xx) = np.meshgrid(xv, yv)
w = geo.DSD[0] / np.sqrt((geo.DSD[0] ** 2 + xx ** 2 + yy ** 2))
np.multiply(proj, w, out=proj_filt)
proj_filt = filtering(proj_filt, geo, angles,
parker=False, verbose=verbose)
# geo.proj = proj_filt
return Atb(proj_filt, geo, geo.angles, "FDK", gpuids=gpuids)
fdk = FDK
def fbp(proj, geo, angles, **kwargs): # noqa: D103
__doc__ = FDK.__doc__ # noqa: F841
if geo.mode != "parallel":
raise ValueError("Only use FBP for parallel beam. Check geo.mode.")
geox = copy.deepcopy(geo)
geox.check_geo(angles)
verbose = kwargs["verbose"] if "verbose" in kwargs else False
gpuids = kwargs["gpuids"] if "gpuids" in kwargs else None
geo.filter = kwargs["filter"] if "filter" in kwargs else None
proj_filt = filtering(copy.deepcopy(proj), geox,
angles, parker=False, verbose=verbose)
if not isinstance(geo.DSO, np.ndarray):
return Atb(proj_filt, geo, angles, gpuids=gpuids)* geo.DSO / geo.DSD
else:
return Atb(proj_filt, geo, angles, gpuids=gpuids)* geo.DSO[0] / geo.DSD[0]