Two-step spectral CT reconstruction¶
This example shows how to generate simulated spectral x-ray projections of an in silico phantom, and then perform a two-step CT reconstruction using Schlomka’s decomposition and Feldkamp et al’s reconstruction.
#!/usr/bin/env python
import sys
import itk
from itk import RTK as rtk
import spekpy as sp
import numpy as np
import xraylib as xrl
# Simulation, decomposition and reconstruction parameters
niterations = 1000
thresholds = [20.0, 40.0, 60.0, 80.0, 100.0, 120.0]
nbins = len(thresholds) - 1
sdd = 1000.0
sid = 500.0
npix_x = 32
npix_y = npix_x // 8
spacing = 256.0 / npix_x
nmat = 2
origin_x = -0.5 * (npix_x - 1) * spacing
origin_y = -0.5 * (npix_y - 1) * spacing
nproj = int(np.pi / 2.0 * npix_x)
spacing_proj = sdd / sid * spacing
origin_proj_x = sdd / sid * origin_x
origin_proj_y = sdd / sid * origin_y
materials = []
materials.append(
{
"material": xrl.GetCompoundDataNISTByIndex(xrl.NIST_COMPOUND_WATER_LIQUID),
"centers": [[0.0, 0.0, 0.0], [-50.0, 0.0, 0.0], [50.0, 0.0, 0.0]],
"semi_axes": [[100.0, 0.0, 100.0], [20.0, 0.0, 20.0], [20.0, 0.0, 20.0]],
"fractions": [1.0, -1.0, -1.0],
"init": 100,
}
)
materials.append(
{
"material": xrl.GetCompoundDataNISTByIndex(
xrl.NIST_COMPOUND_BONE_CORTICAL_ICRP
),
"centers": [[50.0, 0.0, 0.0]],
"semi_axes": [[20.0, 0.0, 20.0]],
"fractions": [1.0],
"init": 0.1,
}
)
# Defines the RTK geometry object using nproj, sdd, sid
geometry = rtk.ThreeDCircularProjectionGeometry.New()
angles = np.linspace(0.0, 360.0, nproj, endpoint=False)
for angle in angles:
geometry.AddProjection(sid, sdd, angle)
rtk.write_geometry(geometry, "geometry.xml")
# Generate a source spectrum using SkekPy, see https://doi.org/10.1002/mp.14945 and
# assume the same spectrum for all pixels in the projection (no bow-tie, no heel effect, etc.)
s = sp.Spek(kvp=120, th=10.5, z=62.56, dk=1.0, shift=0.5, mas=0.01)
s.filter("Al", 2)
energies = s.get_spectrum()[0]
fluence = s.get_spectrum()[1]
energies = np.pad(energies, [1, 0], constant_values=1)
energies = np.concatenate((energies, np.arange(121, 151)))
nenergies = energies.size
fluence = np.pad(fluence, [1, 30])
SpectrumImageType = itk.Image[itk.F, 3]
spectrum = SpectrumImageType.New()
spectrum.SetRegions([nenergies, npix_x, npix_y])
spectrum.AllocateInitialized()
spectrum_np = itk.array_view_from_image(spectrum)
for i in range(npix_y):
for j in range(npix_x):
spectrum_np[i, j, :] = fluence
spectrum.SetSpacing([1.0, spacing_proj, spacing_proj])
spectrum.SetOrigin([0.0, origin_proj_x, origin_proj_y])
itk.imwrite(spectrum, "spectrum.mha")
# Create material images and corresponding projections
ImageType = itk.Image[itk.F, 3]
for i, m in zip(range(nmat), materials):
mat_ref = rtk.constant_image_source(
origin=[origin_x, origin_y, origin_x],
size=[npix_x, npix_y, npix_x],
spacing=[spacing] * 3,
ttype=ImageType,
)
mat_proj = rtk.constant_image_source(
origin=[origin_proj_x, origin_proj_y, 0.0],
size=[npix_x, npix_y, nproj],
spacing=[spacing_proj] * 3,
ttype=ImageType,
)
for c, a, d in zip(m["centers"], m["semi_axes"], m["fractions"]):
mat_ref = rtk.draw_ellipsoid_image_filter(mat_ref, center=c, axis=a, density=d)
mat_proj = rtk.ray_ellipsoid_intersection_image_filter(
mat_proj, geometry=geometry, center=c, axis=a, density=d
)
itk.imwrite(mat_ref, f"mat{i}_ref.mha")
itk.imwrite(mat_proj, f"mat{i}_proj.mha")
mat_proj_np = itk.array_view_from_image(mat_proj)
m["projections"] = mat_proj_np
# Detector response matrix
drm = rtk.constant_image_source(
size=[energies.size, energies.size], spacing=[1.0] * 2, ttype=itk.Image[itk.F, 2]
)
drm_np = itk.array_view_from_image(drm)
for i in range(energies.size):
drm_np[:i, i] = np.arange(i) / i
itk.imwrite(drm, "drm.mha")
# Image of materials basis (linear attenuation coefficients)
mat_basis = rtk.constant_image_source(
size=[nmat, energies.size], spacing=[1.0] * 2, ttype=itk.Image[itk.F, 2]
)
mat_basis_np = itk.array_view_from_image(mat_basis)
for e in range(energies.size):
for i, m in zip(range(nmat), materials):
mat_basis_np[e, i] = (
xrl.CS_Total_CP(m["material"]["name"], energies[e])
* m["material"]["density"]
)
mat_basis_np[e, i] *= 0.1 # to / mm
itk.imwrite(mat_basis, "mat_basis.mha")
# Spectral mixture using line integral of materials and linear attenuation coefficient
CountsImageType = itk.VectorImage[itk.F, 3]
counts = CountsImageType.New()
counts.SetRegions([npix_x, npix_y, nproj])
counts.SetVectorLength(nbins)
counts.AllocateInitialized()
counts.SetOrigin([origin_proj_x, origin_proj_y, 0.0])
counts.SetSpacing([spacing_proj] * 3)
counts_np = itk.array_view_from_image(counts)
for t in range(nbins):
for eimp in range(drm_np.shape[1]):
# Attenuation at energy eimp
att = np.zeros(materials[0]["projections"].shape)
for i, m in zip(range(nmat), materials):
att += m["projections"] * mat_basis_np[eimp, i]
att = np.exp(-att)
selected_energy_indices = np.argwhere(
(energies >= thresholds[t]) & (energies <= thresholds[t + 1])
).flatten()
for edet in selected_energy_indices:
if drm_np[edet, eimp] == 0.0:
continue
# Calculate the weight for the trapezoidal rule
if (
edet == selected_energy_indices[0]
or edet == selected_energy_indices[-1]
):
w = 0.5
else:
w = 1.0
counts_np[:, :, :, t] += w * fluence[eimp] * drm_np[edet, eimp] * att
itk.imwrite(counts, "counts.mha")
# Create initialization for iterative decomposition
DecomposedImageType = itk.VectorImage[itk.F, 3]
decomposed_init = DecomposedImageType.New()
decomposed_init.SetRegions([npix_x, npix_y, nproj])
decomposed_init.SetVectorLength(nmat)
decomposed_init.Allocate()
decomposed_init.SetOrigin([origin_proj_x, origin_proj_y, 0.0])
decomposed_init.SetSpacing([spacing_proj] * 3)
decomposed_init_np = itk.array_view_from_image(decomposed_init)
for i in range(nmat):
decomposed_init_np[:, :, :, i] = materials[i]["init"]
itk.imwrite(decomposed_init, "decomposed_init.mha")
# Thresholds in an itk.VariableLengthVector
thresholds_itk = itk.VariableLengthVector[itk.D](nbins + 1)
for i in range(thresholds_itk.GetSize()):
thresholds_itk[i] = thresholds[i]
# Spectral mixture using line integral of materials and linear attenuation coefficient
DecomposedImageType = itk.VectorImage[itk.F, 3]
decomposed_ref = DecomposedImageType.New()
decomposed_ref.SetRegions([npix_x, npix_y, nproj])
decomposed_ref.SetVectorLength(nmat)
decomposed_ref.Allocate()
decomposed_ref.SetOrigin([origin_proj_x, origin_proj_y, 0.0])
decomposed_ref.SetSpacing([spacing_proj] * 3)
decomposed_ref_np = itk.array_view_from_image(decomposed_ref)
for i, m in zip(range(nmat), materials):
decomposed_ref_np[:, :, :, i] = m["projections"]
itk.imwrite(decomposed_ref, "decomposed_ref.mha")
CountsImageType = itk.VectorImage[itk.F, 3]
counts_forward = CountsImageType.New()
counts_forward.SetRegions([npix_x, npix_y, nproj])
counts_forward.SetVectorLength(nbins)
counts_forward.AllocateInitialized()
counts_forward.SetOrigin([origin_proj_x, origin_proj_y, 0.0])
counts_forward.SetSpacing([spacing_proj] * 3)
counts_forward = rtk.spectral_forward_model_image_filter(
input_decomposed_projections=decomposed_ref,
input_measured_projections=counts_forward,
input_incident_spectrum=spectrum,
detector_response=drm,
material_attenuations=mat_basis,
number_of_energies=energies.size,
number_of_materials=nmat,
thresholds=thresholds_itk,
)
itk.imwrite(counts_forward[0], "counts_forward.mha")
# Projection-based decomposition
decomposed = rtk.simplex_spectral_projections_decomposition_image_filter(
input_decomposed_projections=decomposed_init,
guess_initialization=False,
input_measured_projections=counts,
input_incident_spectrum=spectrum,
detector_response=drm,
material_attenuations=mat_basis,
thresholds=thresholds_itk,
number_of_iterations=niterations,
optimize_with_restarts=True,
log_transform_each_bin=False,
)
itk.imwrite(decomposed[0], "decomposed.mha")
# Reconstruct each material image with FDK
for m in range(nmat):
decomp_mat_proj = itk.image_from_array(
itk.array_from_image(decomposed[0])[:, :, :, m].copy()
)
decomp_mat_proj.CopyInformation(decomposed[0])
mat_recon = rtk.constant_image_source(
origin=[origin_x, origin_y, origin_x],
size=[npix_x, npix_y, npix_x],
spacing=[spacing] * 3,
ttype=ImageType,
)
mat_recon = itk.fdk_cone_beam_reconstruction_filter(
mat_recon, decomp_mat_proj, geometry=geometry
)
itk.imwrite(mat_recon, f"mat{m}_recon.mha")