MRI geometric distortion: calibration cube
Esaote G-scan Brio (0.25 T), 3D HYCE sequence, 256 × 256 × 164 voxels of 1.055 mm. Scanned at the University of Twente, May 2022. The phantom contains small markers on a regular 3D grid with 20 mm spacing. Comparing where the scanner shows them with where they really are gives the geometric distortion, which is modelled by a 5th order polynomial.
Why a custom method? The scanner's own software can correct geometric distortion in 2D (within each slice), but not in 3D. For 3D acquisitions the through-plane component and its variation across slices stay uncorrected. A custom measurement and correction method was therefore needed to compensate for the distortion in all three dimensions.
3D viewer
Drag to rotate, scroll to zoom, hover a dot for its details. Coordinates are DICOM patient coordinates (LPS) in mm, see coordinate system. The letters L/R, A/P and S/I in the view mark the anatomical directions. Distortion vectors and the deformed lattice can be exaggerated to make the pattern easier to see.
Layers
Slice position (mm)
Exaggeration
View
Distortion versus distance from isocentre
Each dot's displacement from its true position, before and after applying the polynomial correction. Distortion grows quickly towards the edge of the field of view.
Statistics per spherical volume
Statistics for all dots within a sphere of the given radius around the isocentre (true position).
Coordinate system: XYZ, LPS and RAS
All x, y, z values on this page and in the data files are DICOM patient coordinates, taken directly from the DICOM headers (ImagePositionPatient, ImageOrientationPatient = [1 0 0 0 1 0], PixelSpacing). DICOM defines this frame as LPS:
| Axis | LPS (DICOM, ITK, this page) | RAS (3D Slicer, NIfTI, FreeSurfer) | Voxel index → mm |
|---|---|---|---|
| x | +x towards patient Left | xRAS = −x (towards Right) | x = −135 + 1.0547 · column |
| y | +y towards Posterior | yRAS = −y (towards Anterior) | y = −135 + 1.0547 · row |
| z | +z towards Superior (head) | zRAS = z (unchanged) | z = −85.98 + 1.055 · slice |
Converting a point between LPS and RAS is just flipping the sign of x and y. The origin (0, 0, 0) is the same in both. Here it is the centre of the field of view, presumably the magnet isocentre.
Relation to the room. The anatomical labels refer to the patient position entered at the console, here Feet First Supine (FFS), also for a phantom. For a supine patient posterior is down, so +y points towards the floor. At 0° tilt the main field B0 of the G-scan is vertical, i.e. along y. With feet first, +z (superior) points towards the side of the magnet from which the patient entered. Had the scan been registered as Head First Supine, x and z would point the other way in the room. Switch on Scanner, bed & floor in the viewer to see this.
Converting the polynomial. The coefficients cannot simply be relabelled: in RAS the mapping becomes PRAS(r) = D · PLPS(D · r) with D = diag(−1, −1, 1), which changes the sign of some coefficients. Both versions are provided in polynomials.json (keys lps and ras).
Method
Segmentation. The volume is smoothed slightly, a slowly varying background is subtracted, and the result is thresholded. Connected components of 4–200 voxels are dot candidates; the larger bars and cubes (placed in the phantom so the scanner can calibrate itself) are rejected. Each dot's centre is its intensity-weighted centroid.
Grid identification. The dot closest to the scanner origin gets grid index (0, 0, 0). From every identified dot, the six neighbours are predicted 20 mm away, extrapolating the local step from the opposite neighbour so the search follows the distortion. The nearest candidate within 6 mm is accepted. Dots are processed in order of increasing distance to the origin, so the grid grows outwards from the well-behaved centre.
True positions. The ideal 20 mm lattice is rigidly registered (rotation and translation) to the dots within 60 mm of the isocentre, where gradient non-linearity is negligible.
Polynomial fit. Two 5th order 3D polynomials (56 terms per coordinate) are fitted by least squares: a correction that maps distorted image coordinates to true coordinates, and a forward model that maps true coordinates to where they appear in the image. Coordinates are scaled by 100 mm for numerical conditioning.
Using the correction polynomial
The correction polynomial maps a position as it appears in the image (distorted) to its true position. Each output coordinate is a separate polynomial in x, y and z:
The forward polynomial has the same form and does the opposite: true position → position in the image. Exponents and coefficients for both, in LPS and RAS, are in polynomials.json.
- Correcting points measured in an image (landmarks, a needle tip, a segmented surface): apply the
correctionpolynomial to their coordinates. - Undistorting a whole image: for every voxel of the corrected output, at true position p, sample the original image at
forward(p)and interpolate (pull-back resampling). Using the forward polynomial avoids holes in the output. - The fit is only valid inside the volume covered by dots: roughly x −108…93, y −109…91, z −89…92 mm (LPS). Polynomials extrapolate badly outside it.
- The polynomials apply to this scanner with this sequence and positioning (3D HYCE, 0° tilt, FFS). Another sequence or bandwidth may distort differently.
import json
import numpy as np
def load(path="polynomials.json", frame="lps", kind="correction"):
p = json.load(open(path))[frame][kind] # frame: "lps" or "ras"
return np.array(p["exponents"]), np.array(p["coefficients_xyz"]), p["scale_mm"]
def evaluate(poly, xyz):
"""Map (N, 3) positions in mm to (N, 3) positions in mm."""
exponents, coef, s = poly
u = np.atleast_2d(np.asarray(xyz, float)) / s
out = np.zeros_like(u)
for (a, b, c), ck in zip(exponents, coef):
out += np.outer(u[:, 0]**a * u[:, 1]**b * u[:, 2]**c, ck)
return out
correction = load(kind="correction")
print(evaluate(correction, [[50.0, 30.0, -20.0]])) # distorted -> true (LPS, mm)
from scipy.ndimage import map_coordinates # load() and evaluate() as in the Python tab. # volume[z, y, x] as read from the DICOM series; (x0, y0, z0) = ImagePositionPatient # of the first slice; (dx, dy, dz) = voxel size in mm. # The output uses the same grid; enlarge it to keep parts that the correction moves # outside the original field of view. forward = load(kind="forward") zz, yy, xx = np.indices(volume.shape) true_xyz = np.stack([x0 + xx * dx, y0 + yy * dy, z0 + zz * dz], axis=-1).reshape(-1, 3) src = evaluate(forward, true_xyz) # where each true point appears in the scan coords = [(src[:, 2] - z0) / dz, (src[:, 1] - y0) / dy, (src[:, 0] - x0) / dx] corrected = map_coordinates(volume, coords, order=1, cval=0).reshape(volume.shape)
P = jsondecode(fileread('polynomials.json'));
p = P.lps.correction; % or P.ras.correction, P.lps.forward, ...
xyz = [50 30 -20]; % N x 3 positions in mm
u = xyz / p.scale_mm;
out = zeros(size(xyz));
for k = 1:size(p.exponents, 1)
e = p.exponents(k, :);
out = out + (u(:,1).^e(1) .* u(:,2).^e(2) .* u(:,3).^e(3)) * p.coefficients_xyz(k, :);
end
disp(out) % true position in mm
const P = await (await fetch('polynomials.json')).json();
function evaluate(p, [x, y, z]) {
const s = p.scale_mm, u = [x / s, y / s, z / s], out = [0, 0, 0];
p.exponents.forEach(([a, b, c], k) => {
const t = u[0] ** a * u[1] ** b * u[2] ** c;
for (let i = 0; i < 3; i++) out[i] += p.coefficients_xyz[k][i] * t;
});
return out;
}
console.log(evaluate(P.lps.correction, [50, 30, -20])); // true position in mm
#include <math.h>
/* E[k] = exponents, C[k] = coefficients_xyz (copy from polynomials.json), n = 56, s = 100.0 */
void evaluate(const int E[][3], const double C[][3], int n, double s,
const double p[3], double out[3])
{
double u[3] = {p[0] / s, p[1] / s, p[2] / s};
out[0] = out[1] = out[2] = 0.0;
for (int k = 0; k < n; k++) {
double t = pow(u[0], E[k][0]) * pow(u[1], E[k][1]) * pow(u[2], E[k][2]);
for (int i = 0; i < 3; i++) out[i] += C[k][i] * t;
}
}