Algorithmcore
Fan-to-parallel rebinning
Also called fan-beam rebinning.
Turn fan-beam CT data into a parallel-beam sinogram. Each parallel ray is a fan ray with a known source angle and fan angle, read from the fan data by interpolation.
- Time
- O(a d) worst
- Space
- O(a d) extra
- Where
- a = the number of parallel angles; d = the number of parallel detector bins; n = the side of the n x n output image
- Model
- Real RAM
- Properties
- deterministicnot in placenot iterativelineariterative
- Evidence
- Proof sketchCode in 1 languages
Fan-to-parallel rebinning converts the data of a fan-beam CT scanner into a parallel-beam sinogram. Each ray of a parallel projection is also a ray of some fan: its source angle and its fan angle follow from the angle and the offset of the line by one arcsine. The algorithm reads that fan ray from the measured data by bilinear interpolation and stores it. After rebinning, ordinary parallel-beam filtered back-projection, or any other parallel method, reconstructs the image. Rebinning an parallel sinogram takes time. Kak and Slaney call it re-sorting.
In plain words
A fan-beam scanner measures many rays at each source position, and the rays spread out from the source. A parallel scanner measures, at each angle, rays that are all parallel. The two scanners measure the same kind of thing: the total absorption along straight lines. Only the way the lines are grouped differs.
Rebinning regroups the lines. For each line that a parallel projection needs, it finds the source position from which that line was shot and the detector cell that caught it. The scanner rarely has a source exactly at that position, or a cell exactly at that spot, so the value is mixed from the four nearest measurements. When every parallel line has a value, the data looks as if a parallel scanner had made it, and the parallel method takes over.
The problem
The source at the angle is at , at the distance from the center, with and . The ray with the fan angle is the line with the angle and the offset , so the fan data is
with the Radon transform . The input is a full scan with for and an equiangular detector . A flat detector at through the center has the fan angle .
The output is the parallel sinogram with for and . The reconstruction after it is an image on , with pixel centered at and .
Inverting and gives
The algorithm
REBIN(g, D, α, a, d, τ) ▷ g[k][j]: β_k = 2πk/a_f, γ_j = (j - (d_f-1)/2) α- 1
a_f, d_f ← the number of rows and of columns of g - 2
for k ← 0 to a - 1 do - 3
θ ← kπ/a - 4
for j ← 0 to d - 1 do - 5
s ← (j - (d-1)/2) τ - 6
γ ← arcsin(s/D); β ← (θ - γ) mod 2π - 7
u ← β a_f/(2π); v ← γ/α + (d_f-1)/2 - 8
P[k][j] ← BILINEAR(g, u, v) ▷ periodic in u, zero outside the detector in v - 9
return P
Line L6 is the inverse map above. Line L7 turns into fractional indices into the fan data. For a flat detector, line L7 uses . Line L8 reads the four nearest samples. The rows wrap around, because row would be row 0 again.
A full scan holds each line twice: and . The ray and the ray are the same line. An option of the code takes the mean of the two interpolated copies. This changes nothing for exact data and halves the variance of independent noise.
The reconstruction step is plain parallel FBP: filter each row of with the Ram-Lak kernel and back-project over with the weight .
Example
Take , a fan scan with source positions () and bins with . Rebin the parallel sample , .
- Line L6: and .
- Line L7: and .
- Line L8 mixes the source positions 0 and 1 with the weights and , and the bins 6 and 7 with the weights and .
Let the object be the unit disk with density 1. Its fan data does not depend on , and the ray with the fan angle has the length inside the disk. Bin 6 () holds and bin 7 () holds . The interpolated value is . The true value is . The error of comes from the linear interpolation across the curved profile.
A larger setup, measured with the code: , a fan that covers the disk of radius , , , rebinned to angles and bins with , and . The smooth object is the blob with . These numbers are measurements of this setup, not bounds.
| quantity | value |
|---|---|
| largest error of the rebinned sinogram, blob, equiangular detector | 0.0030 |
| same, mean of the two copies | 0.0028 |
| same, flat detector | 0.0084 |
| largest image error, blob, rebinning and parallel FBP | 0.0243 |
| largest image error, blob, parallel FBP on exact parallel data | 0.0153 |
| largest error of the rebinned sinogram, modified Shepp-Logan phantom (5 ellipses) | 0.114 |
| image value at pixel of that phantom, true 0.2 | 0.2102 |
The sinogram error of the phantom is large only at the edges of the ellipses, where the profile has a square-root corner that linear interpolation cannot follow.
Correctness
The proof in proofs/correctness.md has three parts. First, for the map of line L6 gives the only fan ray that is the wanted line, so with exact data at the right points the rebinned sinogram is exact. Second, bilinear interpolation reproduces data of the form exactly and has the error at most , where and bound the second derivatives of the fan data in and . Third, parallel FBP is linear, and its discrete operator has the norm at most in the maximum norm. So the rebinning error adds at most times the largest sinogram error to the image, on top of the error of parallel FBP.
Proof: Correctness of fan-to-parallel rebinningfull
We use the conventions of the page. and . The Radon transform is for all real , so and has the period in . The source at the angle is , and the fan data is for .
The proof has three parts. Part 1 shows that the map of line L6 finds a fan ray that is the wanted parallel ray, so with exact interpolation the rebinned sinogram is exact. Part 2 bounds the error of the bilinear interpolation of line L8 and proves that it is exact on data that is linear in the fan angle and does not depend on the source angle. Part 3 joins the result with the correctness of parallel-beam filtered back-projection.
Part 1. The rebinning map
Lemma 1. Let and . Put and , reduced modulo . Then . The pair with and is the only fan ray that is the oriented line . The fan ray is the same line with the other orientation, .
Proof. By definition for some integer , and has the period in . For uniqueness, has the single solution in , and then modulo . For the last claim, and .
This is what lines L5 to L7 compute. For a flat detector the column of the ray is in place of , and the lemma is the same. With average=True the code takes the mean of the two copies and . By the lemma both copies equal for exact data, so the mean changes nothing for exact data and halves the variance of independent noise in the two readings.
If the fan covers the support of , that is, is zero outside the disk of radius with where , then every parallel sample with is 0, and the samples with map to fan angles inside the detector.
Part 2. Bilinear interpolation
Line L8 reads the fan data at the fractional indices and , where . The rows are periodic: row is row 0. This matches the period of in .
Lemma 2 (exact cases). If for constants and , then line L8 returns at each point with .
Proof. The value does not depend on , and the two row weights and add to 1, so the result is the linear interpolation in . Linear interpolation between and at the fraction gives .
So for such data the rebinned value at is exactly , which the test checks.
Lemma 3 (error bound). Let be on the cell , with and there. Then at each point of the cell the bilinear interpolant satisfies
Proof. Let be linear interpolation in at a fixed , and linear interpolation in at a fixed . Then and . The 1D error of linear interpolation on an interval of length is at most , since vanishes at both ends and equals for some , and . So . The function is at most at both nodes in , and is a convex combination of these node values.
For a smooth the fan data is smooth, and the rebinned sinogram has the error . At the edge of an object of constant density, has a square-root singularity in , the second derivative is not bounded, and the bound does not hold. The measured errors at such edges on the page are much larger than in the smooth case.
Part 3. The reconstruction
Let be the rebinned sinogram and the exact parallel sinogram on the same grid. Parallel-beam FBP is linear, so its output on is its output on plus its output on . The first term is analyzed on the page filtered back-projection, whose proof shows that the method is exact for band-limited data up to the angular quadrature and the linear interpolation of the back-projection. The second term is bounded by the norm of the discrete FBP operator times . The discrete operator is a fixed linear map. Its norm in the maximum norm is at most , using . So the error that the interpolation adds to the image is at most times the largest rebinning error. The bound grows as the detector spacing shrinks, which is the usual amplification of data errors by the ramp filter.
With the values of the test, and the largest rebinning error on the smooth blob is , so the bound is . In the measurement on the page, the largest image error grows from for exact parallel data to after rebinning, a change of , well below the bound.
Complexity
In the real-RAM model, lines L5 to L8 take time for each of the parallel samples, so rebinning takes time and space for the output. The fan data is read but not copied. The parallel FBP after it takes with an FFT, or with the direct sum that the code uses. Rebinning is cheap next to the reconstruction.
Properties
- Linear. The output is a fixed linear combination of the input, with at most 4 nonzero weights per output sample (8 with the mean of two copies).
- Exact map, approximate values. The geometry is exact. The only error is the interpolation, which is second order for smooth data.
- Smoothing. Linear interpolation is a low-pass filter. It reduces the resolution a little and correlates the noise of neighboring samples.
- Detector independence. The parallel method after it does not need to know anything about the fan geometry.
Variants and improvements
- Direct fan-beam FBP. Fan-beam filtered back-projection avoids the interpolation before the filter, at the cost of a distance weight in the back-projection.
- Better interpolation. Cubic or windowed sinc interpolation in reduces the smoothing. Interpolation in matters less when the source angles are dense.
- Two separate steps. Each parallel angle mixes rays from many source angles, but for a fixed fan angle the map is a shift. Some codes interpolate first in for each fan angle, then in for each parallel angle.
- Short scans. Data over plus the fan angle also fills the parallel sinogram over . Each line then exists at least once, and the code needs the copy rule to find it.
- Cone beam. Rebinning the rows of a helical cone-beam scan to parallel fans is a step of several practical helical methods. The exact helical method on this site is Katsevich's algorithm.
When to use it
Use rebinning when a good parallel-beam code is at hand and the fan data is dense, so the interpolation error is small. It also helps when a method needs parallel data, such as direct Fourier reconstruction or a method that uses the parallel range conditions. When the source angles are sparse, the interpolation in is coarse, and direct fan-beam FBP is better.
Pitfalls
- The wrong sign of the fan angle. With the other sign convention for , line L6 must use . Test the map on a point object at a known place, as the test of the code does with and .
- No wrap in . The source angle is periodic. A sample with between the last source and must mix the last row with row 0.
- Parallel bins outside the fan. If , the line is not in the data. The code returns 0, which is right only if the object lies inside the fan.
- Offsets in the detector. A shift of the central ray by a fraction of a bin moves every rebinned sample and blurs the image.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/fan-to-parallel-rebinning/. The authors are listed in prov/D52MD2.jsonl.
"""Fan-to-parallel rebinning, followed by parallel-beam filtered back-projection.
Conventions:
- omega(t) = (cos t, sin t) and omega_perp(t) = (-sin t, cos t). The Radon
transform is R f(theta, s) = integral f(s omega(theta) + t omega_perp(theta)) dt.
- The source at angle beta is S(beta) = D omega_perp(beta). The ray at the fan
angle gamma is the line with theta = beta + gamma and s = D sin gamma, so the
fan data is g(beta, gamma) = R f(beta + gamma, D sin gamma).
- Fan data: a full scan, beta_k = 2 pi k / a for k = 0 .. a - 1, and either an
equiangular detector, gamma_j = (j - (d - 1) / 2) alpha, or a flat virtual
detector through the center, t_j = (j - (d - 1) / 2) tau_f, with the fan angle
atan(t_j / D).
- Parallel data: theta_k = k pi / a_p for k = 0 .. a_p - 1 and
s_j = (j - (d_p - 1) / 2) tau for j = 0 .. d_p - 1.
- The output image is n x n on [-1, 1]^2, pixel (i, j) centered at
x_j = -1 + (2 j + 1) / n and y_i = 1 - (2 i + 1) / n.
"""
import math
def fan_coordinates(theta, s, dist):
"""(beta, gamma) of the line (theta, s): gamma = asin(s / D), beta = theta - gamma mod 2 pi."""
gamma = math.asin(s / dist)
return (theta - gamma) % (2.0 * math.pi), gamma
def bilinear(g, u, v):
"""Interpolate g[k][j] at the fractional indices (u, v): periodic in k, zero outside in j."""
a, d = len(g), len(g[0])
k0 = math.floor(u)
wu = u - k0
j0 = math.floor(v)
wv = v - j0
val = 0.0
for dk, fu in ((0, 1.0 - wu), (1, wu)):
if fu == 0.0:
continue
row = g[(k0 + dk) % a]
for dj, fv in ((0, 1.0 - wv), (1, wv)):
j = j0 + dj
if fv != 0.0 and 0 <= j < d:
val += fu * fv * row[j]
return val
def rebin(g, dist, alpha, a_p, d_p, tau, detector="equiangular", average=False):
"""Parallel sinogram P[k][j] ~ R f(theta_k, s_j) from full-scan fan data g.
alpha is the angle step of an equiangular detector, or the spacing tau_f of
a flat detector. With average=True each parallel sample is the mean of its
two copies in the full scan, (theta, s) and (theta + pi, -s).
"""
a, d = len(g), len(g[0]) # @line L1
dbeta = 2.0 * math.pi / a
c0 = (d - 1) / 2.0
def sample(theta, s):
beta, gamma = fan_coordinates(theta, s, dist) # @line L6
if detector == "equiangular":
v = gamma / alpha + c0 # @line L7
else:
v = dist * math.tan(gamma) / alpha + c0
return bilinear(g, beta / dbeta, v) # @line L8
out = []
for k in range(a_p): # @line L2
theta = k * math.pi / a_p # @line L3
row = []
for j in range(d_p): # @line L4
s = (j - (d_p - 1) / 2.0) * tau # @line L5
val = sample(theta, s)
if average:
val = 0.5 * (val + sample(theta + math.pi, -s))
row.append(val)
out.append(row)
return out # @line L9
def ram_lak_kernel(d, tau):
"""h(m tau), m = -(d - 1) .. d - 1, of the ramp filter band-limited to 1 / (2 tau)."""
out = []
for m in range(-(d - 1), d):
if m == 0:
out.append(1.0 / (4.0 * tau * tau))
elif m % 2 == 0:
out.append(0.0)
else:
out.append(-1.0 / (m * m * math.pi * math.pi * tau * tau))
return out
def parallel_fbp(sino, tau, n):
"""Parallel-beam FBP on [0, pi) with the Ram-Lak kernel and a direct convolution."""
a, d = len(sino), len(sino[0])
h = ram_lak_kernel(d, tau)
c0 = (d - 1) / 2.0
qs = [[tau * sum(h[j - l + d - 1] * p[l] for l in range(d)) for j in range(d)] for p in sino]
xs = [-1.0 + (2 * j + 1) / n for j in range(n)]
ys = [1.0 - (2 * i + 1) / n for i in range(n)]
img = [[0.0] * n for _ in range(n)]
for k, q in enumerate(qs):
c, s = math.cos(k * math.pi / a), math.sin(k * math.pi / a)
for i, y in enumerate(ys):
row = img[i]
for j, x in enumerate(xs):
u = (x * c + y * s) / tau + c0
b = math.floor(u)
w = u - b
if 0 <= b < d:
row[j] += (1.0 - w) * q[b]
if 0 <= b + 1 < d:
row[j] += w * q[b + 1]
return [[math.pi / a * v for v in row] for row in img]
def rebin_and_reconstruct(g, dist, alpha, a_p, d_p, tau, n, detector="equiangular", average=False):
"""Rebin the fan data, then reconstruct with parallel-beam FBP."""
return parallel_fbp(rebin(g, dist, alpha, a_p, d_p, tau, detector, average), tau, n)
test_fan_to_parallel_rebinning.py 128 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/fan-to-parallel-rebinning/. The authors are listed in prov/D52MD2.jsonl.
import math
import random
from fan_to_parallel_rebinning import (
bilinear,
fan_coordinates,
parallel_fbp,
ram_lak_kernel,
rebin,
rebin_and_reconstruct,
)
DIST = 3.0
GAMMA_MAX = math.asin(math.sqrt(2.0) / DIST) # the fan covers the disk of radius sqrt(2)
D = 49
ALPHA = 2.0 * GAMMA_MAX / (D - 1)
TAU_FLAT = 2.0 * DIST * math.tan(GAMMA_MAX) / (D - 1)
A = 64
AP, DP = 32, 47
TAU = 2.0 * math.sqrt(2.0) / DP
N = 32
XS = [-1.0 + (2 * j + 1) / N for j in range(N)]
YS = [1.0 - (2 * i + 1) / N for i in range(N)]
EQUI = [(j - (D - 1) / 2.0) * ALPHA for j in range(D)]
FLAT = [math.atan((j - (D - 1) / 2.0) * TAU_FLAT / DIST) for j in range(D)]
BETAS = [2.0 * math.pi * k / A for k in range(A)]
PHANTOM = [
(1.0, 0.69, 0.92, 0.0, 0.0, 0.0),
(-0.8, 0.6624, 0.874, 0.0, -0.0184, 0.0),
(-0.2, 0.11, 0.31, 0.22, 0.0, -18.0),
(-0.2, 0.16, 0.41, -0.22, 0.0, 18.0),
(0.1, 0.21, 0.25, 0.0, 0.35, 0.0),
]
def ellipse_projection(table, theta, s):
"""The exact Radon transform of an ellipse table (Kak and Slaney, chapter 3)."""
total = 0.0
for rho, a, b, x0, y0, phi in table:
t = theta - math.radians(phi)
u = s - (x0 * math.cos(theta) + y0 * math.sin(theta))
a2 = a * a * math.cos(t) ** 2 + b * b * math.sin(t) ** 2
if u * u < a2:
total += 2.0 * rho * a * b / a2 * math.sqrt(a2 - u * u)
return total
def blob_projection(theta, s):
"""R f for f(x) = exp(-4 pi |x - c|^2) with c = (0.3, -0.2)."""
u = s - (0.3 * math.cos(theta) - 0.2 * math.sin(theta))
return 0.5 * math.exp(-4.0 * math.pi * u * u)
def fan_data(radon, gammas):
return [[radon(b + g, DIST * math.sin(g)) for g in gammas] for b in BETAS]
def parallel_data(radon):
return [[radon(k * math.pi / AP, (j - (DP - 1) / 2.0) * TAU) for j in range(DP)] for k in range(AP)]
def blob_error(img):
return max(
abs(img[i][j] - math.exp(-4.0 * math.pi * ((XS[j] - 0.3) ** 2 + (YS[i] + 0.2) ** 2)))
for i in range(N)
for j in range(N)
)
def test_fan_coordinates() -> None:
beta, gamma = fan_coordinates(0.3, 0.5, 2.0)
assert abs(gamma - 0.25268) < 1e-5 and abs(beta - 0.04732) < 1e-5
rng = random.Random(1)
for _ in range(200):
theta, s = rng.uniform(0, math.pi), rng.uniform(-1.4, 1.4)
beta, gamma = fan_coordinates(theta, s, DIST)
assert 0.0 <= beta < 2.0 * math.pi
assert abs(math.sin(beta + gamma - theta)) < 1e-12 and math.cos(beta + gamma - theta) > 0
assert abs(DIST * math.sin(gamma) - s) < 1e-12
def test_bilinear_is_periodic_in_the_angle() -> None:
g = [[1.0, 2.0], [3.0, 4.0]]
assert bilinear(g, 0.5, 0.5) == 2.5
assert bilinear(g, 1.5, 0.0) == 2.0 # half way between row 1 and row 0
assert bilinear(g, 0.0, 1.5) == 1.0 # half way between column 1 and nothing
def test_exact_on_data_linear_in_the_fan_angle() -> None:
# g = c + e gamma is reproduced by the interpolation, so the result is c + e asin(s / D)
g = [[0.7 + 2.0 * gm for gm in EQUI] for _ in BETAS]
p = rebin(g, DIST, ALPHA, AP, DP, TAU)
for k in range(AP):
for j in range(DP):
s = (j - (DP - 1) / 2.0) * TAU
if abs(s) <= DIST * math.sin(GAMMA_MAX) - 1e-9:
assert abs(p[k][j] - (0.7 + 2.0 * math.asin(s / DIST))) < 1e-12
def test_rebinned_sinogram_is_close() -> None:
want = parallel_data(blob_projection)
for det, gammas, step in (("equiangular", EQUI, ALPHA), ("flat", FLAT, TAU_FLAT)):
for avg in (False, True):
p = rebin(fan_data(blob_projection, gammas), DIST, step, AP, DP, TAU, detector=det, average=avg)
assert max(abs(p[k][j] - want[k][j]) for k in range(AP) for j in range(DP)) < 0.02
def test_reconstruction_of_a_blob() -> None:
img = rebin_and_reconstruct(fan_data(blob_projection, EQUI), DIST, ALPHA, AP, DP, TAU, N)
assert blob_error(img) < 0.03
direct = parallel_fbp(parallel_data(blob_projection), TAU, N)
assert blob_error(direct) < 0.03
def test_phantom_values() -> None:
radon = lambda t, s: ellipse_projection(PHANTOM, t, s)
img = rebin_and_reconstruct(fan_data(radon, EQUI), DIST, ALPHA, AP, DP, TAU, N, average=True)
assert abs(img[16][16] - 0.2) < 0.04
assert abs(img[8][16] - 0.3) < 0.05
def test_ram_lak_kernel() -> None:
h = ram_lak_kernel(3, 0.5)
assert h[2] == 1.0 and h[1] == h[3] == -4.0 / math.pi ** 2 and h[0] == h[4] == 0.0
History
The first CT scanners used parallel translate and rotate motions, and fan-beam scanners came soon after. Reordering the fan rays into parallel projections was the natural first way to reuse the parallel methods. Herman, Lakshminarayanan and Naparstek gave in 1976 a convolution method on the fan data directly. Kak and Slaney treat the re-sorting algorithm and the direct methods side by side in their Section 3.4.
References
- A. C. Kak and M. Slaney, Principles of Computerized Tomographic Imaging, SIAM Classics in Applied Mathematics 33, 2001, Chapter 3, Section 3.4 (reconstruction from fan projections, the subsection on a re-sorting algorithm) and Section 3.3 (parallel projections)
- G. T. Herman, A. V. Lakshminarayanan and A. Naparstek, Convolution reconstruction techniques for divergent beams, Computers in Biology and Medicine 6(4), 1976, pp. 259 to 271 (the direct method that rebinning competes with)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag D52MD2 Edit this page History Report an error Markdown