Algorithm Search⌘K

Algorithmcore

Feldkamp-Davis-Kress algorithm

Also called FDK, Feldkamp algorithm.

Approximate cone-beam CT reconstruction for a circular source orbit. Weight each projection, ramp-filter it row by row, and back-project it in 3D with the inverse squared depth.

Time
O(a m d log d + a s n^2) worst
Space
O(a m d + s n^2) extra
Where
a = the number of source positions; d = the number of detector columns; m = the number of detector rows; n = the side of each n x n slice; s = the number of output slices
Properties
deterministicnot in placenot iterativelineariterative
Origin
L. A. Feldkamp, L. C. Davis, J. W. Kress, 1984

The Feldkamp-Davis-Kress (FDK) algorithm reconstructs a 3D object from cone-beam X-ray projections taken while a point source moves on a circle. It is fan-beam filtered back-projection with a flat detector, applied to each detector row as if the row were a tilted fan, followed by a back-projection in 3D. Each projection gets a cosine weight, each detector row is filtered with the ramp filter, and each voxel adds the filtered value of the ray through it, divided by the square of its depth. With aa source positions, an m×dm \times d detector and ss slices of n×nn \times n voxels it takes O(amdlog⁡d+asn2)O(a m d \log d + a s n^2) time with an FFT. The result is exact in the plane of the orbit and for objects that do not change along the axis. Elsewhere it is an approximation, which is good for small cone angles.

In plain words

A cone-beam scanner has a small X-ray source and a flat detector panel that turn around the object together. Each shot records a whole 2D picture, so one turn gives data for many slices at once.

FDK treats each row of the panel as if it were a normal fan of rays. It weights each reading by how much its ray is tilted, sharpens each row with the same filter as in 2D, and then smears each picture back through the volume along the rays to the source. Points near the source get less weight. The method is fast and simple, and it is the standard method for a circular scan. But the rays of one row are tilted out of the slice, so slices far from the plane of the orbit come out with errors.

The problem

The source at the angle β\beta is at S(β)=D ω⊥(β)S(\beta) = D\,\omega^\perp(\beta), on a circle of radius DD in the plane x3=0x_3 = 0, with ω(β)=(cos⁡β,sin⁡β,0)\omega(\beta) = (\cos\beta, \sin\beta, 0) and ω⊥(β)=(−sin⁡β,cos⁡β,0)\omega^\perp(\beta) = (-\sin\beta, \cos\beta, 0). A virtual flat detector passes through the axis of rotation, normal to ω⊥(β)\omega^\perp(\beta). Its point (t,z)(t, z) is t ω(β)+z e3t\,\omega(\beta) + z\,e_3. The data g(β,t,z)g(\beta, t, z) is the integral of the attenuation ff along the ray from S(β)S(\beta) through the point (t,z)(t, z), which is a value of the cone-beam X-ray transform. A real detector farther away gives the same data after a scaling of tt and zz. The scanner gets the line integrals from the intensities by the Beer-Lambert law. The unknown ff is zero outside a ball of radius r<Dr < D.

The detector samples tj=(j−(d−1)/2)τt_j = (j - (d-1)/2)\tau for j=0,…,d−1j = 0, \ldots, d-1 and zr=(r−(m−1)/2)ζz_r = (r - (m-1)/2)\zeta for r=0,…,m−1r = 0, \ldots, m-1, and the scan takes βk=2πk/a\beta_k = 2\pi k/a. The output is a set of slices at the heights x3x_3. Each slice is an n×nn \times n image on [−1,1]2[-1, 1]^2, with the pixel (i,j)(i, j) at x1=−1+(2j+1)/nx_1 = -1 + (2j+1)/n and x2=1−(2i+1)/nx_2 = 1 - (2i+1)/n.

For a point xx, put U=(D−x⋅ω⊥)/DU = (D - x\cdot\omega^\perp)/D, the depth of xx along the central ray divided by DD. The ray from S(β)S(\beta) through xx hits the detector at t′=x⋅ω/Ut' = x\cdot\omega/U and z′=x3/Uz' = x_3/U. The algorithm evaluates the FDK formula

fFDK(x)=∫02π1U2∫h(t′−u)2 D g(β,u,z′)D2+u2+z′2 du dβ,f_{\mathrm{FDK}}(x) = \int_0^{2\pi} \frac{1}{U^2}\int \frac{h(t' - u)}{2}\,\frac{D\,g(\beta, u, z')}{\sqrt{D^2 + u^2 + z'^2}}\,du\,d\beta,

where hh is the ramp filter, the filter with the frequency response ∣σ∣|\sigma| in ordinary frequency.

The algorithm

  1. FDK(g, D, τ, ζ, n, heights) ▷ g[k][r][j]: source angle β_k = 2πk/a, detector point (t_j, z_r)
  2. 1a, m, d ← the sizes of g
  3. 2h[l] ← (1/2) · RAMLAK(l, τ) for l = -(d-1) .. d-1 ▷ 1/(4τ²) at 0, 0 for even l, -1/(l²π²τ²) for odd l
  4. 3for k ← 0 to a - 1 and each row r do
  5. 4p[j] ← D / sqrt(D² + t_j² + z_r²) · g[k][r][j] for each j
  6. 5q_k[r][j] ← τ Σ_l h[j - l] p[l] for each j ▷ direct sum, or an FFT with zero padding
  7. 6for each height x₃, each pixel (x₁, x₂) and each k do
  8. 7U ← (D - x·ω⊥(β_k)) / D; t' ← x·ω(β_k) / U; z' ← x₃ / U
  9. 8f(x) ← f(x) + BILINEAR(q_k, z'/ζ + (m-1)/2, t'/τ + (d-1)/2) / U²
  10. 9return (2π/a) · f

Line L2 is the band-limited ramp (Ram-Lak) kernel times 12\frac{1}{2}. The 12\frac{1}{2} is there because a full scan measures each line in the mid-plane twice. Line L4 is the cosine weight: the factor D/D2+t2+z2D/\sqrt{D^2 + t^2 + z^2} is the cosine of the angle between the ray and the central ray. Line L5 filters each detector row alone, as an aperiodic discrete convolution. Line L7 projects the voxel onto the detector, and line L8 reads the filtered projection there by bilinear interpolation, with 0 outside the detector, and divides by U2U^2. Line L9 multiplies by the angle step.

Example

Take D=3D = 3. The weight of line L4 at the detector point (t,z)=(0.5,0.4)(t, z) = (0.5, 0.4) is 3/9+0.25+0.16=3/9.41=0.97803/\sqrt{9 + 0.25 + 0.16} = 3/\sqrt{9.41} = 0.9780.

An object that does not change in zz. Let ff be 1 inside the infinite cylinder x12+x22<0.16x_1^2 + x_2^2 < 0.16. The central ray t=0t = 0 in the row z=0z = 0 crosses it along a chord of length 0.80.8. In the row z=1z = 1 the central ray is tilted, and its path through the cylinder is longer by the factor 10/3\sqrt{10}/3, so g=0.8⋅10/3=0.8433g = 0.8 \cdot \sqrt{10}/3 = 0.8433. The weight there is 3/10=0.94873/\sqrt{10} = 0.9487, and 0.9487⋅0.8433=0.80.9487 \cdot 0.8433 = 0.8. So after line L4 the two rows agree. The same holds for every ray, so the weighted rows are equal and every slice of the output is the same as the mid-plane slice. This is Theorem 2 of the proof.

The back-projection of one voxel. Take x=(0.5,0,0.4)x = (0.5, 0, 0.4).

source angle x⋅ωx\cdot\omega x⋅ω⊥x\cdot\omega^\perp UU t′t' z′z' 1/U21/U^2
β=0\beta = 0 0.5 0 1 0.5 0.4 1
β=π/2\beta = \pi/2 0 -0.5 7/6 0 0.3429 0.7347

At β=π/2\beta = \pi/2 the source is at (−3,0,0)(-3, 0, 0), and the voxel is farther from it than the axis is. So the ray through the voxel hits the detector lower, at z′=0.4⋅6/7z' = 0.4 \cdot 6/7, and the voxel gets a smaller weight.

Measurements. The table gives the results of the code on exact cone-beam data with D=3D = 3, a=32a = 32, d=41d = 41 columns with τ=0.075\tau = 0.075, m=13m = 13 rows with ζ=1/6\zeta = 1/6, and n=16n = 16. These are measurements of this setup, not bounds.

object result
Gaussian e−8∣x−c∣2e^{-8\vert x - c\vert^2}, c=(0.2,−0.1,0)c = (0.2, -0.1, 0), largest error in the slice x3=0x_3 = 0 0.0167
same Gaussian, largest error in the slice x3=0.25x_3 = 0.25 0.0136
cylinder of radius 0.4 around (0.2,−0.1)(0.2, -0.1), pixel (7,9)(7, 9) at the distance 0.16 from the axis of the cylinder, slices x3=0x_3 = 0 and x3=0.6x_3 = 0.6 1.0002 and 1.0002

To see the error of the formula itself, take the blob e−8((x1−0.5)2+(x2−0.5)2)−40(x3−h)2e^{-8((x_1 - 0.5)^2 + (x_2 - 0.5)^2) - 40(x_3 - h)^2}, which is thin in x3x_3, and read the output at its center (0.5,0.5,h)(0.5, 0.5, h), where the true value is 1. The detector has the height ±1.2\pm 1.2.

height hh a=32a = 32, m=41m = 41, d=61d = 61 a=64a = 64, m=81m = 81, d=121d = 121
0 0.9935 0.9988
0.25 0.9503 0.9752
0.5 0.9090 0.9264

In the mid-plane the error becomes small as the sampling becomes finer, as Theorem 1 predicts. At the height 0.5 it is 9 percent and then 7 percent, so most of it does not come from the sampling. This is the cone-beam error of FDK (measurement).

Correctness

The proof in proofs/correctness.md shows that the ray from the source through the point (t′,z′)(t', z') passes through xx, and then proves two exactness results. Theorem 1: in the mid-plane x3=0x_3 = 0 the formula only reads the row z=0z = 0, whose rays lie in that plane, and it is then the 2D flat-detector fan-beam formula, which is exact. Theorem 2: if ff does not depend on x3x_3, a tilted ray crosses the object along a path that is longer by the factor D2+t2+z2/D2+t2\sqrt{D^2 + t^2 + z^2}/\sqrt{D^2 + t^2}, and the cosine weight cancels this factor. So the weighted rows are equal, and every slice is reconstructed as the mid-plane. Both results also hold for the discrete algorithm in a form that the tests check exactly. For a general ff and x3≠0x_3 \ne 0 the measurement above shows an error that does not come from the sampling. A heuristic reason is that the rays used for one voxel do not lie in one plane. A deeper reason is that the circular orbit fails the Tuy condition: a plane x3=cx_3 = c with c≠0c \ne 0 that cuts the object does not meet the orbit.

Proof: Correctness of the Feldkamp-Davis-Kress algorithmfull

We use the conventions of the page. ω(β)=(cos⁡β,sin⁡β,0)\omega(\beta) = (\cos\beta, \sin\beta, 0), ω⊥(β)=(−sin⁡β,cos⁡β,0)\omega^\perp(\beta) = (-\sin\beta, \cos\beta, 0) and e3=(0,0,1)e_3 = (0, 0, 1). The source is S(β)=D ω⊥(β)S(\beta) = D\,\omega^\perp(\beta), and the detector point (t,z)(t, z) is P=t ω(β)+z e3P = t\,\omega(\beta) + z\,e_3. The data is g(β,t,z)=∫0∞f(S+λv) dλg(\beta, t, z) = \int_0^\infty f(S + \lambda v)\,d\lambda with the unit vector v=(P−S)/∣P−S∣v = (P - S)/|P - S| and ∣P−S∣=D2+t2+z2|P - S| = \sqrt{D^2 + t^2 + z^2}. The FDK formula is

fFDK(x)=∫02π1U2 q(β,t′,z′) dβ,q(β,t,z)=∫h(t−u)2 D g(β,u,z)D2+u2+z2 du,f_{\mathrm{FDK}}(x) = \int_0^{2\pi} \frac{1}{U^2}\,q(\beta, t', z')\,d\beta, \qquad q(\beta, t, z) = \int \frac{h(t - u)}{2}\,\frac{D\,g(\beta, u, z)}{\sqrt{D^2 + u^2 + z^2}}\,du,

with U=(D−x⋅ω⊥)/DU = (D - x\cdot\omega^\perp)/D, t′=x⋅ω/Ut' = x\cdot\omega/U and z′=x3/Uz' = x_3/U, where hh is the ramp filter. The proof shows two exactness results, that the code computes the discrete form of them exactly, and why the formula is not exact in general. It relies on the 2D flat-detector fan-beam formula, which fan-beam filtered back-projection proves:

Fact (2D flat-detector fan-beam formula). Let f0f_0 be smooth with support in the open disk of radius r<Dr < D, and let g0(β,t)g_0(\beta, t) be its integral along the line in the plane from Dω⊥(β)D\omega^\perp(\beta) through t ω(β)t\,\omega(\beta). Then for ∣x∣<r|x| < r,

f0(x)=∫02π1U2∫h(t′−u)2 D g0(β,u)D2+u2 du dβ.f_0(x) = \int_0^{2\pi} \frac{1}{U^2}\int \frac{h(t' - u)}{2}\,\frac{D\,g_0(\beta, u)}{\sqrt{D^2 + u^2}}\,du\,d\beta.

Part 1. The geometry of the back-projection

Lemma 1. The point xx lies on the ray from S(β)S(\beta) through the detector point (t′,z′)(t', z'), and DUD U is the depth of xx along the central ray, that is, (x−S)⋅(−ω⊥)=DU(x - S)\cdot(-\omega^\perp) = D U.

Proof. (x−S)⋅(−ω⊥)=D−x⋅ω⊥=DU(x - S)\cdot(-\omega^\perp) = D - x\cdot\omega^\perp = D U. The detector point is P′−S=t′ω+z′e3+D(−ω⊥)P' - S = t'\omega + z' e_3 + D(-\omega^\perp), and x−S=(x⋅ω) ω+x3e3+DU(−ω⊥)x - S = (x\cdot\omega)\,\omega + x_3 e_3 + D U(-\omega^\perp), because the three vectors ω\omega, e3e_3 and −ω⊥-\omega^\perp are orthonormal. With t′=x⋅ω/Ut' = x\cdot\omega/U and z′=x3/Uz' = x_3/U we get x−S=U(P′−S)x - S = U(P' - S), so xx is on the ray. □\square

The code computes UU, t′t' and z′z' in this way (line L7), and the test checks Lemma 1 at random points.

Part 2. The mid-plane

Theorem 1. Let ff be smooth with support in a ball of radius r<Dr < D. For x=(x1,x2,0)x = (x_1, x_2, 0) with ∣x∣<r|x| < r, fFDK(x)=f(x)f_{\mathrm{FDK}}(x) = f(x).

Proof. For x3=0x_3 = 0, z′=0z' = 0, so the formula only reads q(β,t′,0)q(\beta, t', 0), which only uses the data row z=0z = 0. The rays of this row lie in the plane x3=0x_3 = 0, so g(β,u,0)=g0(β,u)g(\beta, u, 0) = g_0(\beta, u) for f0=f(⋅,⋅,0)f_0 = f(\cdot, \cdot, 0). The weight is D/D2+u2D/\sqrt{D^2 + u^2}. So fFDK(x)f_{\mathrm{FDK}}(x) is the right side of the Fact for f0f_0, which equals f0(x1,x2)=f(x)f_0(x_1, x_2) = f(x). □\square

Part 3. Objects that do not depend on zz

Lemma 2. Let f(x1,x2,x3)=f0(x1,x2)f(x_1, x_2, x_3) = f_0(x_1, x_2) with f0f_0 integrable on lines. Then g(β,t,z)=D2+t2+z2D2+t2 g0(β,t)g(\beta, t, z) = \dfrac{\sqrt{D^2 + t^2 + z^2}}{\sqrt{D^2 + t^2}}\,g_0(\beta, t).

Proof. Write v=(v12,v3)v = (v_{12}, v_3). Then f(S+λv)=f0(S12+λv12)f(S + \lambda v) = f_0(S_{12} + \lambda v_{12}), and v12=(tω−Dω⊥)/D2+t2+z2v_{12} = (t\omega - D\omega^\perp)/\sqrt{D^2 + t^2 + z^2} has the length D2+t2/D2+t2+z2\sqrt{D^2 + t^2}/\sqrt{D^2 + t^2 + z^2}. Put μ=λ∣v12∣\mu = \lambda|v_{12}|. Then S12+λv12S_{12} + \lambda v_{12} runs along the planar ray from Dω⊥D\omega^\perp through tωt\omega with the unit speed in μ\mu, and dλ=dμ/∣v12∣d\lambda = d\mu/|v_{12}|. So g=g0/∣v12∣g = g_0/|v_{12}|. □\square

Theorem 2. Let f(x1,x2,x3)=f0(x1,x2)f(x_1, x_2, x_3) = f_0(x_1, x_2) with f0f_0 smooth and supported in the disk of radius r<Dr < D. Then fFDK(x)=f(x)f_{\mathrm{FDK}}(x) = f(x) for every xx with ∣(x1,x2)∣<r|(x_1, x_2)| < r and every height x3x_3.

Proof. By Lemma 2 the weighted data is Dg/D2+u2+z2=Dg0(β,u)/D2+u2D g/\sqrt{D^2 + u^2 + z^2} = D g_0(\beta, u)/\sqrt{D^2 + u^2}, which does not depend on zz. So q(β,t,z)=q(β,t,0)q(\beta, t, z) = q(\beta, t, 0) for every zz, and fFDK(x)f_{\mathrm{FDK}}(x) equals fFDKf_{\mathrm{FDK}} at (x1,x2,0)(x_1, x_2, 0), because UU and t′t' do not depend on x3x_3. By the Fact (or Theorem 1) this is f0(x1,x2)f_0(x_1, x_2). □\square

The detector must be tall enough: the proof needs the data at the height z′=x3/Uz' = x_3/U.

Part 4. The discrete algorithm

The code samples tj=(j−(d−1)/2)τt_j = (j - (d-1)/2)\tau and zr=(r−(m−1)/2)ζz_r = (r - (m-1)/2)\zeta and βk=2πk/a\beta_k = 2\pi k/a. Lines L2 to L5 replace the convolution by τ∑l12hB((j−l)τ) pl\tau\sum_l \frac{1}{2}h_B((j - l)\tau)\,p_l, with hBh_B the band-limited ramp (Ram-Lak) kernel. Line L8 reads qq at (t′,z′)(t', z') by bilinear interpolation, with 0 outside the detector, and line L9 replaces ∫02πdβ\int_0^{2\pi} d\beta by (2π/a)∑k(2\pi/a)\sum_k. In each row this is the discrete 2D flat-detector fan-beam algorithm, whose errors (band limit, sums over the bins and the angles, interpolation) are discussed on the fan-beam page. Two facts are exact in the discrete case too.

Proposition 1 (discrete mid-plane). If mm is odd, the output in the slice x3=0x_3 = 0 depends only on the row r=(m−1)/2r = (m-1)/2 of the data.

Proof. For x3=0x_3 = 0, z′=0z' = 0, so the fractional row index is 0/ζ+(m−1)/20/\zeta + (m-1)/2, an integer r0r_0. Bilinear interpolation then gives the weight 1 to row r0r_0 and 0 to the row r0+1r_0 + 1, which the code skips. The filter of line L5 works on each row alone, so row r0r_0 of qq depends only on row r0r_0 of the data. □\square

Proposition 2 (discrete z-invariance). If g(βk,tj,zr)=D2+tj2+zr2D2+tj2 ckjg(\beta_k, t_j, z_r) = \frac{\sqrt{D^2 + t_j^2 + z_r^2}}{\sqrt{D^2 + t_j^2}}\,c_{kj} for numbers ckjc_{kj}, as Lemma 2 gives for a zz-invariant object, then in exact arithmetic the output is the same in every slice x3x_3 at every pixel where the fractional row index z′/ζ+(m−1)/2z'/\zeta + (m-1)/2 lies in [0,m−1][0, m-1].

Proof. After line L4 every row of projection kk is the same vector Dckj/D2+tj2D c_{kj}/\sqrt{D^2 + t_j^2}, so after line L5 every row of qkq_k is the same vector QkQ_k. Bilinear interpolation between two equal rows, with weights 1−w1 - w and ww, gives QkQ_k interpolated in tt alone, provided both rows exist. If the index is exactly m−1m - 1 the second weight is 0 and only one row is read. This value does not depend on x3x_3. The factor 1/U21/U^2 and t′t' do not depend on x3x_3 either. □\square

The test checks Proposition 1 by changing all other rows at random, and Proposition 2 on an infinite cylinder at three heights. In floating point the two weights of Proposition 2 are computed with rounding, so the test allows 10−910^{-9}.

Part 5. Why FDK is not exact in general

For a point with x3≠0x_3 \ne 0, the rays that FDK uses for one source position lie in the tilted plane through S(β)S(\beta) and the detector row z=z′z = z'. Each source position uses a different tilted plane, and these planes do not form a single plane through xx. The 2D argument of Theorem 1 needs all the rays in one plane, so it does not apply. This is a heuristic, not a proof that the formula fails. The page gives a measurement: a blob that is thin in zz and lies at the height 0.50.5 comes out about 7 percent too low, and the error changes little when the sampling is made finer (the peak goes from 0.909 to 0.926), while the same blob in the mid-plane is reconstructed within 0.2 percent. A deeper reason is Tuy's condition: a plane x3=cx_3 = c with c≠0c \ne 0 that cuts the object does not meet the source circle, so the circular orbit does not give the data that exact inversion needs (see the Tuy condition and Grangeat's formula).

Complexity

In the real-RAM model:

  • Lines L3 to L5 take O(d2)O(d^2) for each row of each projection with the direct sum, or O(dlog⁡d)O(d \log d) with an FFT of length at least 2d−12d - 1. That is O(amd2)O(a m d^2) or O(amdlog⁡d)O(a m d \log d) in total.
  • Lines L6 to L8 take O(1)O(1) for each source and voxel, so O(asn2)O(a s n^2) for ss slices.

The total is O(amdlog⁡d+asn2)O(a m d \log d + a s n^2) with an FFT, and the code uses the direct sum. For a cube of N3N^3 voxels with a,m,da, m, d all of order NN the back-projection costs O(N4)O(N^4), which dominates. The extra space is O(amd)O(a m d) for the filtered projections and O(sn2)O(s n^2) for the volume. A code can also filter and back-project one projection at a time and keep only O(md)O(m d) of the filtered data.

Properties

  • Linear. The output is a linear function of the data, as the code tests.
  • Exact in the mid-plane. In the plane of the orbit it is the 2D fan-beam method (theorem).
  • Exact for objects that do not change along the axis (theorem). The detector must be tall enough to see the voxel.
  • Approximate elsewhere. The error grows with the distance from the mid-plane and is largest for objects with fast changes along the axis (measurement above).
  • Row-wise filtering. The filter runs along the detector rows, which are parallel to the plane of the orbit. It does not mix the rows, so the filtered data of one projection is available at once.

Variants and improvements

  • Short scan. Parker's weights from fan-beam filtered back-projection reduce the scan to π\pi plus the fan angle. They make the mid-plane exact again, and off the mid-plane the result is still an approximation.
  • Curved detector. For a cylindrical detector, use the equiangular fan kernel in each row with the matching weights.
  • Exact methods. A circle does not give enough data for exact inversion. Grangeat's formula relates cone-beam data to derivatives of the 3D Radon transform, and a helical orbit with Katsevich's algorithm is exact.
  • Iterative methods. SART and SIRT handle cone beams with any orbit and reduce the cone-beam error at a much higher cost.

When to use it

Use FDK for cone-beam CT with a circular orbit and a moderate cone angle. It is fast, it is easy to run on a GPU, and its error is small near the mid-plane. If the cone angle is large or the object changes fast along the axis far from the mid-plane, use a helical scan with an exact method, or an iterative method.

Pitfalls

  • Missing cosine weight. Without the factor D/D2+t2+z2D/\sqrt{D^2 + t^2 + z^2}, rows far from the center are too bright, and Theorem 2 fails.
  • The wrong depth weight. The weight is 1/U21/U^2 with the depth along the central ray, not one over the squared distance from the source to the voxel. With a flat detector these differ off the central ray.
  • Short detector. If z′=x3/Uz' = x_3/U falls outside the detector, the code reads 0 and the voxel is too dark. Voxels near the source side need the tallest detector.
  • Real and virtual detector. If the detector is at the distance DdD_d from the source and not at the axis, scale tt and zz by D/DdD/D_d before the algorithm.
  • Filtering along the wrong direction. Filtering the columns, or the whole 2D projection, is not FDK and does not reduce to the fan-beam formula in the mid-plane.

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.

feldkamp_davis_kress.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/feldkamp-davis-kress/. The authors are listed in prov/7MNYCN.jsonl.
"""The Feldkamp-Davis-Kress (FDK) algorithm for a circular cone-beam scan.

Conventions:
- omega(b) = (cos b, sin b, 0), omega_perp(b) = (-sin b, cos b, 0), e3 = (0, 0, 1).
- The source at the angle beta is S(beta) = D omega_perp(beta), on a circle of
  radius D in the plane z = 0 (the mid-plane).
- The detector is a virtual flat detector through the axis, normal to
  omega_perp(beta). The detector point (t, z) is t omega(beta) + z e3. The data
  g[k][r][j] is the line integral of f along the ray from S(beta_k) through the
  detector point (t_j, z_r), with beta_k = 2 pi k / a,
  t_j = (j - (d - 1) / 2) tau and z_r = (r - (m - 1) / 2) zeta.
- The output slices are n x n images on [-1, 1]^2 at given heights x3. Pixel
  (i, j) has its center at x_j = -1 + (2 j + 1) / n and y_i = 1 - (2 i + 1) / n.
"""

import math


def ram_lak(m, tau):
    """h(m tau) of the ramp filter |sigma| band-limited to 1 / (2 tau)."""
    if m == 0:
        return 1.0 / (4.0 * tau * tau)
    if m % 2 == 0:
        return 0.0
    return -1.0 / (m * m * math.pi * math.pi * tau * tau)


def cone_ray(beta, t, z, dist):
    """The source point and the unit direction of the ray to the detector point (t, z)."""
    c, s = math.cos(beta), math.sin(beta)
    src = (-dist * s, dist * c, 0.0)
    det = (t * c, t * s, z)
    v = (det[0] - src[0], det[1] - src[1], det[2] - src[2])
    norm = math.sqrt(v[0] ** 2 + v[1] ** 2 + v[2] ** 2)
    return src, (v[0] / norm, v[1] / norm, v[2] / norm)


def detector_coordinates(x, y, z, beta, dist):
    """(U, t', z') of the point (x, y, z): U = (D - x . omega_perp) / D, t' = x . omega / U, z' = z / U."""
    c, s = math.cos(beta), math.sin(beta)
    u = (dist - (-x * s + y * c)) / dist
    return u, (x * c + y * s) / u, z / u


def filter_row(p, kern, tau):
    """q_j = tau * sum_l kern[j - l] p_l, the aperiodic convolution by a direct sum."""
    d = len(p)
    return [tau * sum(kern[j - l + d - 1] * p[l] for l in range(d)) for j in range(d)]


def bilinear(q, u, v):
    """Interpolate q[r][j] at the fractional row index u and column index v, 0 outside."""
    r0, j0 = math.floor(u), math.floor(v)
    wu, wv = u - r0, v - j0
    val = 0.0
    for dr, fr in ((0, 1.0 - wu), (1, wu)):
        r = r0 + dr
        if fr == 0.0 or r < 0 or r >= len(q):
            continue
        row = q[r]
        for dj, fj in ((0, 1.0 - wv), (1, wv)):
            j = j0 + dj
            if fj != 0.0 and 0 <= j < len(row):
                val += fr * fj * row[j]
    return val


def fdk_filter(data, dist, tau, zeta):
    """Cosine weight and row-wise ramp filter (h / 2) of each projection."""
    a, m, d = len(data), len(data[0]), len(data[0][0])  # @line L1
    kern = [0.5 * ram_lak(i, tau) for i in range(-(d - 1), d)]  # @line L2
    ts = [(j - (d - 1) / 2.0) * tau for j in range(d)]
    zs = [(r - (m - 1) / 2.0) * zeta for r in range(m)]
    out = []
    for k in range(a):  # @line L3
        proj = []
        for r in range(m):
            w = [dist / math.sqrt(dist * dist + t * t + zs[r] * zs[r]) for t in ts]
            p = [wj * v for wj, v in zip(w, data[k][r])]  # @line L4
            proj.append(filter_row(p, kern, tau))  # @line L5
        out.append(proj)
    return out


def fdk(data, dist, tau, zeta, n, heights):
    """FDK reconstruction of the slices x3 = h for h in heights, each an n x n image."""
    a, m, d = len(data), len(data[0]), len(data[0][0])
    q = fdk_filter(data, dist, tau, zeta)
    xs = [-1.0 + (2 * j + 1) / n for j in range(n)]
    ys = [1.0 - (2 * i + 1) / n for i in range(n)]
    cz, ct = (m - 1) / 2.0, (d - 1) / 2.0
    vol = [[[0.0] * n for _ in range(n)] for _ in heights]
    for k in range(a):
        beta = 2.0 * math.pi * k / a
        for sl, x3 in enumerate(heights):
            for i, y in enumerate(ys):  # @line L6
                row = vol[sl][i]
                for j, x in enumerate(xs):
                    u, tp, zp = detector_coordinates(x, y, x3, beta, dist)  # @line L7
                    row[j] += bilinear(q[k], zp / zeta + cz, tp / tau + ct) / (u * u)  # @line L8
    w = 2.0 * math.pi / a
    return [[[w * v for v in row] for row in img] for img in vol]  # @line L9
test_feldkamp_davis_kress.py 115 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/feldkamp-davis-kress/. The authors are listed in prov/7MNYCN.jsonl.

import math
import random

from feldkamp_davis_kress import cone_ray, detector_coordinates, fdk, fdk_filter, filter_row, ram_lak

DIST = 3.0
A, M, D, N = 32, 13, 41, 16
TAU = 3.0 / (D - 1)
ZETA = 2.0 / (M - 1)
XS = [-1.0 + (2 * j + 1) / N for j in range(N)]
YS = [1.0 - (2 * i + 1) / N for i in range(N)]


def gauss3(src, v, c=(0.2, -0.1, 0.0), alpha=8.0):
    """Line integral of exp(-alpha |x - c|^2) along the line src + t v, with v a unit vector."""
    w = [c[i] - src[i] for i in range(3)]
    t = sum(w[i] * v[i] for i in range(3))
    d2 = sum(w[i] ** 2 for i in range(3)) - t * t
    return math.sqrt(math.pi / alpha) * math.exp(-alpha * d2)


def cylinder(src, v, c=(0.2, -0.1), r=0.4):
    """Line integral of the infinite cylinder (x - c1)^2 + (y - c2)^2 < r^2 with density 1."""
    nxy = math.hypot(v[0], v[1])
    ux, uy = v[0] / nxy, v[1] / nxy
    wx, wy = c[0] - src[0], c[1] - src[1]
    t = wx * ux + wy * uy
    d2 = wx * wx + wy * wy - t * t
    return 2.0 * math.sqrt(r * r - d2) / nxy if d2 < r * r else 0.0


def cone_data(fun, a=A, m=M, d=D):
    out = []
    for k in range(a):
        beta = 2.0 * math.pi * k / a
        proj = []
        for r in range(m):
            z = (r - (m - 1) / 2.0) * ZETA
            proj.append([fun(*cone_ray(beta, (j - (d - 1) / 2.0) * TAU, z, DIST)) for j in range(d)])
        out.append(proj)
    return out


def test_kernel_and_filter() -> None:
    assert ram_lak(0, 0.5) == 1.0 and ram_lak(2, 0.5) == 0.0
    assert abs(ram_lak(1, 0.5) + 4.0 / math.pi ** 2) < 1e-15
    kern = [ram_lak(i, 1.0) for i in range(-2, 3)]
    q = filter_row([0.0, 1.0, 0.0], kern, 1.0)
    assert q == [kern[1], kern[2], kern[3]]
    # one projection with a single nonzero entry at (t, z) = (0, 0): weight 1, kernel h / 2
    data = [[[0.0] * 5 for _ in range(3)]]
    data[0][1][2] = 1.0
    out = fdk_filter(data, DIST, 0.5, 0.5)
    assert out[0][0] == [0.0] * 5 and out[0][2] == [0.0] * 5
    assert all(abs(out[0][1][j] - 0.25 * ram_lak(j - 2, 0.5)) < 1e-15 for j in range(5))


def test_ray_geometry() -> None:
    rng = random.Random(3)
    for _ in range(50):
        x, y, z = rng.uniform(-1, 1), rng.uniform(-1, 1), rng.uniform(-1, 1)
        beta = rng.uniform(0, 2 * math.pi)
        u, tp, zp = detector_coordinates(x, y, z, beta, DIST)
        src, v = cone_ray(beta, tp, zp, DIST)
        w = (x - src[0], y - src[1], z - src[2])
        cross = (w[1] * v[2] - w[2] * v[1], w[2] * v[0] - w[0] * v[2], w[0] * v[1] - w[1] * v[0])
        assert max(abs(c) for c in cross) < 1e-12
        # D U is the depth of the point along the central ray
        depth = (-src[0]) * w[0] + (-src[1]) * w[1]
        assert abs(depth / DIST - DIST * u) < 1e-12


def test_gaussian_blob() -> None:
    vol = fdk(cone_data(gauss3), DIST, TAU, ZETA, N, [0.0, 0.25])
    for sl, h in enumerate([0.0, 0.25]):
        for i in range(N):
            for j in range(N):
                want = math.exp(-8.0 * ((XS[j] - 0.2) ** 2 + (YS[i] + 0.1) ** 2 + h * h))
                assert abs(vol[sl][i][j] - want) < 0.03


def test_mid_plane_uses_only_the_mid_row() -> None:
    rng = random.Random(11)
    data = cone_data(gauss3)
    changed = [[row if r == (M - 1) // 2 else [rng.uniform(-3, 3) for _ in row] for r, row in enumerate(p)] for p in data]
    assert fdk(changed, DIST, TAU, ZETA, N, [0.0]) == fdk(data, DIST, TAU, ZETA, N, [0.0])


def test_z_invariant_object_is_exact_at_every_height() -> None:
    vol = fdk(cone_data(cylinder), DIST, TAU, ZETA, N, [0.0, 0.3, -0.45])
    for i in range(N):
        for j in range(N):
            assert abs(vol[1][i][j] - vol[0][i][j]) < 1e-9
            assert abs(vol[2][i][j] - vol[0][i][j]) < 1e-9
            r = math.hypot(XS[j] - 0.2, YS[i] + 0.1)
            if r < 0.3:
                assert abs(vol[0][i][j] - 1.0) < 0.05
            elif r > 0.55:
                # 32 views of a sharp edge leave small streaks outside the object
                assert abs(vol[0][i][j]) < 0.2


def test_linearity() -> None:
    d1 = cone_data(gauss3, a=8)
    d2 = cone_data(cylinder, a=8)
    mix = [[[2.0 * u - 0.5 * v for u, v in zip(r1, r2)] for r1, r2 in zip(p1, p2)] for p1, p2 in zip(d1, d2)]
    v1 = fdk(d1, DIST, TAU, ZETA, 4, [0.2])
    v2 = fdk(d2, DIST, TAU, ZETA, 4, [0.2])
    vm = fdk(mix, DIST, TAU, ZETA, 4, [0.2])
    for i in range(4):
        for j in range(4):
            assert abs(vm[0][i][j] - (2.0 * v1[0][i][j] - 0.5 * v2[0][i][j])) < 1e-12

History

The early CT scanners measured one slice at a time. Feldkamp, Davis and Kress published in 1984 a practical algorithm for the data of a 2D detector and a circular orbit. They extended the fan-beam method by row-wise filtering of weighted projections and a 3D back-projection. Their algorithm reduces to the fan-beam method in the mid-plane and is the standard method for circular cone-beam CT. Tuy showed in 1983 which orbits give enough data for exact inversion, and a circle is not one of them. This is why exact methods for cone beams use other orbits, such as a helix.

References

  1. L. A. Feldkamp, L. C. Davis and J. W. Kress, Practical cone-beam algorithm, Journal of the Optical Society of America A 1(6), 1984, the whole paper, which starts at p. 612
  2. A. C. Kak and M. Slaney, Principles of Computerized Tomographic Imaging, SIAM Classics in Applied Mathematics 33, 2001, Chapter 3, Section 3.6 (three-dimensional reconstructions)
  3. H. K. Tuy, An inversion formula for cone-beam reconstruction, SIAM Journal on Applied Mathematics 43(3), 1983, the data condition for exact cone-beam inversion, which the circular orbit does not satisfy

Where this leads

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.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example