-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfbp_examples.py
167 lines (127 loc) · 5.94 KB
/
fbp_examples.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
from libraries.lib_noise import *
from libraries.lib_tomophantom_sinogram import *
from libraries.lib_parula_cmap import *
from libraries.lib_filtered_backprojection import *
from matplotlib import ticker
import matplotlib.patches as patches
#### Setup ####
cmap = "gray"
epsilon = None
size = np.array([1000, 1000])
model_number = 1
def ret_backproject(num_angles, arc, noise=False):
### using tomophantom
phantom, sinogram = tomophantom_model(model_number, size[0], num_angles, arc, noise=False)
sinogram = elsa.DataContainer(sinogram)
# sinogram = np.reshape(sinogram, [num_angles, int(np.sqrt(2)*size[0])])
# print(np.shape(sinogram))
volume_descriptor = elsa.VolumeDescriptor(size)
# generate circular trajectory
sino_descriptor = elsa.CircleTrajectoryGenerator.createTrajectory(
num_angles, volume_descriptor, arc, size[0]*100, size[0])
# setup operator for 2d X-ray transform
projector = elsa.JosephsMethodCUDA(volume_descriptor, sino_descriptor)
if noise:
sinogram = add_tomophantom_poisson(sinogram)
# from tomobar.methodsDIR import RecToolsDIR
# angles = np.linspace(0, arc-0.1,num_angles,dtype='float32')
# angles_rad = angles*(np.pi/180.0)
# P = int(np.sqrt(2)*size[0]) #detectors
# RecToolsDIR = RecToolsDIR(DetectorsDimH = P, # Horizontal detector dimension
# DetectorsDimV = None, # Vertical detector dimension
# CenterRotOffset = None, # Center of Rotation scalar
# AnglesVec = angles_rad, # A vector of projection angles in radians
# ObjSize = size[0], # Reconstructed object dimensions (scalar)
# device_projector='cpu')
# filtered_backprojection = RecToolsDIR.FBP(sinogram) #perform FBP
filtered_backprojection = projector.applyAdjoint(elsa.DataContainer(sinogram))
# plt.figure(0)
# plt.imshow(np.asarray(filtered_backprojection))
# plt.show()
filtered_backprojection = projector.applyAdjoint(elsa.DataContainer(filter_sinogram(sinogram, sino_descriptor)))
filtered_backprojection = np.asarray(filtered_backprojection)
filtered_backprojection = filtered_backprojection
return filtered_backprojection
def CG(num_angles, arc, n, noise=False):
### using tomophantom
phantom, sinogram = tomophantom_model(model_number, size[0], num_angles, arc, noise=False)
sinogram = elsa.DataContainer(sinogram)
# sinogram = np.reshape(sinogram, [num_angles, int(np.sqrt(2)*size[0])])
# print(np.shape(sinogram))
volume_descriptor = elsa.VolumeDescriptor(size)
# generate circular trajectory
sino_descriptor = elsa.CircleTrajectoryGenerator.createTrajectory(
num_angles, volume_descriptor, arc, size[0]*100, size[0])
# setup operator for 2d X-ray transform
projector = elsa.JosephsMethodCUDA(volume_descriptor, sino_descriptor)
if noise:
sinogram = add_tomophantom_poisson(sinogram)
sinogram = elsa.DataContainer(sinogram)
problem = elsa.WLSProblem(projector, sinogram)
solver = elsa.CG(problem, 0.00005)
reconstruction = solver.solve(n)
return np.asarray(reconstruction)
arc = 180
fntsize = 8
figure, ax = plt.subplots(nrows=2, ncols=4, figsize=(13.2, 6))
i = 0
fig = []
fig.append(ret_backproject(1600,arc, True))
fig.append(ret_backproject(400,arc, True))
fig.append(ret_backproject(40,arc, True))
fig.append(CG(40, arc, 10, True))
i = 0
ax[i,0].imshow(fig[0], vmin=np.min(fig[0]), vmax=np.max(fig[0]), cmap=cmap)
rect = patches.Rectangle((400, 400), 200, 200, linewidth=1, edgecolor='r', facecolor='none')
ax[i,0].add_patch(rect)
ax[i,1].imshow(fig[1], vmin=np.min(fig[1]), vmax=np.max(fig[1]), cmap=cmap)
rect = patches.Rectangle((400, 400), 200, 200, linewidth=1, edgecolor='r', facecolor='none')
ax[i,1].add_patch(rect)
ax[i,2].imshow(fig[2], vmin=np.min(fig[2]), vmax=np.max(fig[2]), cmap=cmap)
rect = patches.Rectangle((400, 400), 200, 200, linewidth=1, edgecolor='r', facecolor='none')
ax[i,2].add_patch(rect)
ax[i,3].imshow(fig[3], vmin=np.min(fig[3]), vmax=np.max(fig[3]), cmap=cmap)
rect = patches.Rectangle((400, 400), 200, 200, linewidth=1, edgecolor='r', facecolor='none')
ax[i,3].add_patch(rect)
ax[i,0].axis('off')
ax[i,1].axis('off')
ax[i,2].axis('off')
ax[i,3].axis('off')
i = 1
ax[i,0].imshow(fig[0][400:600,400:600], vmin=np.min(fig[0]), vmax=np.max(fig[0]), cmap=cmap)
ax[i,1].imshow(fig[1][400:600,400:600], vmin=np.min(fig[1]), vmax=np.max(fig[1]), cmap=cmap)
im = ax[i,2].imshow(fig[2][400:600,400:600], vmin=np.min(fig[2]), vmax=np.max(fig[2]), cmap=cmap)
ax[i,3].imshow(fig[3][400:600,400:600], vmin=np.min(fig[3]), vmax=np.max(fig[3]), cmap=cmap)
ax[i,0].axis('off')
ax[i,1].axis('off')
ax[i,2].axis('off')
ax[i,3].axis('off')
plt.subplots_adjust(wspace=0, hspace=0)
ticklabels = ['0', '0.5', '1.0']
cb = figure.colorbar(im,ax=ax.ravel().tolist(), fraction=0.046)
tick_locator = ticker.LinearLocator(numticks=3)
cb.locator = tick_locator
cb.update_ticks()
cb.set_ticklabels(ticklabels)
# cb.outline.set_visible(False)
# cb.set_ticks([])
# cb.set_ticks([0, 0.5, 1])
# plt.gca().set_visible(False)
save_path = os.path.dirname(os.path.abspath(__file__)) + "/fbp_examples/"
plt.savefig(save_path + "FBP_examples_comparison_full.png", dpi=300, bbox_inches='tight')
# plt.show()
def save_fig(f, name, max, min, red_box=False):
plt.figure()
plt.axis("off")
plt.imshow(f, cmap=cmap, vmax=max, vmin=min)
if red_box:
rect = patches.Rectangle((400, 400), 200, 200, linewidth=2, edgecolor='r', facecolor='none')
ax = plt.gca()
ax.add_patch(rect)
plt.savefig(name, dpi=280, bbox_inches='tight', pad_inches=0)
plt.close('all')
count = 1
for f in fig:
save_fig(f, save_path + "FBP_Examples_" + str(count) + ".png", np.max(f), np.min(f), red_box=True)
save_fig(f[400:600,400:600], save_path + "FBP_Examples_" + str(count) + "_zoomed.png", np.max(f), np.min(f))
count = count + 1