Skip to content

pysrx.register

pysrx.register

Register one acquisition onto another, with isotropic scale and radial lens distortion.

Built to compare two microscopes pixel by pixel (the LF4D comparison registers every acquisition onto an LSM 980 stack), and to measure how far their geometries differ: scale error and barrel or pincushion distortion.

Model (reference-frame point p -> moving-frame point q, physical µm):

u   = R(theta) (p - c_ref) + c_ref + t                        in-plane rotation + translation
q   = c_d + (u - c_d) s (1 + k1 |u - c_d|^2 / r_n^2)           isotropic scale + Brown-Conrady k1
z_q = z_p + tz

c_ref is the centre of the reference field (rotation centre), c_d the centre of the moving camera field (the optical axis is assumed central) and r_n the half-diagonal of the moving field, so k1 is the fractional radial distortion at the field corner (k1 < 0: barrel, k1 > 0: pincushion). Tilt is not fitted.

Steps of register:

  1. XY coarse: normalised template matching of 1 µm/px focus-band maximum projections (coarse_xy).
  2. XY rigid: mutual information (SimpleITK Euler2D, Mattes, 0.5 µm/px; fine_xy).
  3. Distortion: Powell search maximising the NCC over (ty, tx, theta, s, k1) at 1 µm/px (fit_distortion).
  4. Z: 1D search maximising the mean plane-wise NCC (search_z).

Independent, model-free check: displacement_field measures the residual displacement left by the rigid-only model by tile-wise phase correlation; radial_displacement and distortion_summary compare its radial component with the fit.

Arrays are NumPy in, NumPy out (float32 images). Interpolation and the NCC search run on a CUDA GPU through pysrx.gpu when CuPy is available, on the CPU otherwise. Step 2 needs SimpleITK (pixi add simpleitk); without it, pass mutual_information=False to register.

FieldTransform dataclass

Reference -> moving map: rotation, 3D translation, isotropic scale and radial distortion k1.

Attributes:

Name Type Description
tz, ty, tx float

Translation (µm).

theta float

In-plane rotation (rad) about (cy, cx).

cy, cx float

Rotation centre (µm, reference frame): the centre of the reference field.

ncc_mip float

NCC of the fit (set by fit_distortion; mip_ncc gives the value on the resampled stack).

s float

Isotropic scale (moving µm per reference µm). s - 1 is the scale error of the pixel-size calibration.

k1 float

Brown-Conrady radial distortion, normalised to the fractional distortion at the field corner.

dcy, dcx float

Distortion centre (µm, moving frame): the centre of the moving field.

rn float

Normalisation radius (µm): the half-diagonal of the moving field.

extra dict

Diagnostics, e.g. ncc_rigid_2d and ncc_distortion_2d from fit_distortion.

for_fields(ref_shape, ref_spacing, mov_shape, mov_spacing, ty=0.0, tx=0.0, theta=0.0, tz=0.0) classmethod

A rigid transform with the centres and rn set from the two fields (as register does).

Rotation about the reference field centre; distortion about the moving field centre, rn its half-diagonal. Shapes and spacings: the last two entries are (y, x); spacings in µm.

to_dict()

All fields as a plain dict (JSON-ready); FieldTransform(**d) rebuilds it.

rigid(y, x, xp=np)

Rotation about (cy, cx) plus translation: reference (y, x) -> undistorted moving (y, x), µm.

distort(ym, xm)

Scale and radial distortion about (dcy, dcx) in the moving frame, µm.

map(z, y, x)

Reference-frame physical coordinates (µm) -> moving-frame physical coordinates (z, y, x).

radial_model(r)

Radial displacement (µm) at radius r (µm) from the field centre: r (s (1 + k1 r^2 / rn^2) - 1).

registration_image(vol)

Sum of the channels, each divided by its 99.5th percentile: one image to register on.

Parameters:

Name Type Description Default
vol ndarray

(C, Z, Y, X), (Z, Y, X) or (Y, X); a 3D array is one channel.

required

Returns:

Type Description
ndarray

float32, the input without its channel axis.

focus_mip(reg, frac=0.5)

Maximum projection over the in-focus band: planes whose 99th percentile is above frac of the peak.

Keeps nearly empty planes (a light-field volume's out-of-sample planes) out of the projection.

ncc(a, b, mask=None)

Normalised cross-correlation (Pearson) of two images, optionally inside a boolean mask.

Returns NaN when fewer than 100 pixels are compared.

coarse_xy(ref2d, mov2d, ref_spacing, mov_spacing, px=1.0)

Step 1: translation (ty, tx) in µm by normalised template matching at px µm/px.

Parameters:

Name Type Description Default
ref2d ndarray

Reference and moving projections (Y, X), e.g. from focus_mip.

required
mov2d ndarray

Reference and moving projections (Y, X), e.g. from focus_mip.

required
ref_spacing sequence of float

Pixel sizes in µm; the last entry (y and x are assumed square) is used.

required
mov_spacing sequence of float

Pixel sizes in µm; the last entry (y and x are assumed square) is used.

required
px float

Working pixel size, µm.

1.0

fine_xy(ref2d, mov2d, ref_spacing, mov_spacing, ty0, tx0, px=0.5)

Step 2: rigid in-plane fit (theta, ty, tx) by Mattes mutual information (SimpleITK Euler2D).

Rotation about the reference field centre; three-level pyramid (shrink 4, 2, 1), regular-step gradient descent, 25 % random sampling with a fixed seed. Needs SimpleITK (pixi add simpleitk).

Returns:

Type Description
tuple of float

(theta, ty, tx): rad, µm, µm.

fit_distortion(ref2d, mov2d, ref_spacing, mov_spacing, tf, px=1.0, fit_k1=True)

Step 3: maximise the NCC of the reference and the warped moving projection over (ty, tx, theta, s, k1).

Both images are smoothed by a 1 µm Gaussian and compared at px µm/px on the reference field; the Powell search starts from tf (s = 1, k1 = 0) with steps of 1 µm, 1 mrad, 1e-3 and 1e-3 (xtol 1e-3, ftol 1e-6, at most 4000 evaluations). A trial that leaves less than 60 % of the reference field inside the moving field scores 0. Runs on the GPU when available.

Parameters:

Name Type Description Default
ref2d ndarray

Projections (Y, X).

required
mov2d ndarray

Projections (Y, X).

required
ref_spacing sequence of float

Pixel sizes (µm); the last two entries are (y, x).

required
mov_spacing sequence of float

Pixel sizes (µm); the last two entries are (y, x).

required
tf FieldTransform

Starting rigid transform (from fine_xy or coarse_xy), with the centres and rn set.

required
px float

Working pixel size, µm.

1.0
fit_k1 bool

False fits scale only (k1 = 0).

True

Returns:

Type Description
FieldTransform

A new transform; extra holds ncc_rigid_2d (at the start) and ncc_distortion_2d (fitted).

displacement_field(ref2d, mov2d, ref_spacing, mov_spacing, tf, px=1.0, tile_um=48.0, step_um=24.0)

Model-free check: residual displacement of the rigid-only model, by tile-wise phase correlation.

The moving projection is resampled onto the reference field with tf stripped of scale and distortion (s = 1, k1 = 0). Square tiles of tile_um every step_um (Hann window) are matched by phase correlation (1/20 px). A tile is dropped if either side has little texture (std below 5 % of the image's), more than 5 % of it falls outside the moving field, its shift exceeds a quarter tile, or the NCC after the shift is below 0.5.

Returns:

Type Description
ndarray

(n_tiles, 7): y_ref, x_ref, y_mov, x_mov, dy, dx, quality; positions and displacements in µm (moving frame), quality = NCC after the shift. Use radial_displacement for the radial component.

radial_displacement(field_, tf)

Radius (µm from the distortion centre) and radial displacement (µm) of each measured tile.

field_ is the output of displacement_field; compare with tf.radial_model(radius).

distortion_summary(tf, field_=None)

The numbers to report for a fit, as a dict.

Returns:

Type Description
dict

scale_error_pct = 100 (s - 1); k1_pct_at_corner = 100 k1; k1_shift_at_corner_um = k1 rn (the displacement from k1 alone at the field corner); corner_radius_um = rn; and, with a measured field, measured_radius_max_um (beyond it the model is extrapolated), fit_rms_um (RMS of the measured radial displacement about the model) and n_tiles; plus tf.extra.

search_z(ref_reg, mov_reg, ref_spacing, mov_spacing, tf, px=1.0, step=0.25)

Step 4: the z offset tz (µm) that maximises the mean plane-wise NCC.

Starts from the difference of the intensity-weighted centres of the two stacks and scans ±max(15 µm, 2 moving z steps) in step µm; an offset counts only if at least max(3, 60 % of the shared depth) reference planes are more than half inside the moving field. In-plane mapping from tf; runs on the GPU when available.

Parameters:

Name Type Description Default
ref_reg ndarray

(Z, Y, X) registration images (registration_image).

required
mov_reg ndarray

(Z, Y, X) registration images (registration_image).

required
ref_spacing sequence of float

Voxel sizes (z, y, x), µm.

required
mov_spacing sequence of float

Voxel sizes (z, y, x), µm.

required

register(ref, mov, ref_spacing, mov_spacing, distortion=True, mutual_information=True, tile_um=None)

Register mov onto ref: steps 1 to 4, plus the model-free displacement field.

Parameters:

Name Type Description Default
ref ndarray

(C, Z, Y, X) or (Z, Y, X) stacks with (z, y, x) spacings, or (Y, X) images with (y, x) spacings (then no z search, tz = 0). Channels are combined by registration_image; projections by focus_mip.

required
mov ndarray

(C, Z, Y, X) or (Z, Y, X) stacks with (z, y, x) spacings, or (Y, X) images with (y, x) spacings (then no z search, tz = 0). Channels are combined by registration_image; projections by focus_mip.

required
ref_spacing sequence of float

Voxel or pixel sizes, µm.

required
mov_spacing sequence of float

Voxel or pixel sizes, µm.

required
distortion bool

Fit scale and k1 (step 3). False keeps the rigid transform of steps 1 to 2.

True
mutual_information bool

Run step 2 (needs SimpleITK). False goes from template matching straight to step 3.

True
tile_um float

Tile size of displacement_field; default max(48, 45 moving pixels).

None

Returns:

Type Description
tuple

(FieldTransform, field): the transform, and the (n, 7) tile displacements of the rigid model.

resample_to_ref(mov, ref_shape, ref_spacing, mov_spacing, tf, order=1)

Resample mov onto the reference voxel grid through tf (plane by plane, GPU when available).

Parameters:

Name Type Description Default
mov ndarray

(C, Z, Y, X) or (Z, Y, X).

required
ref_shape tuple of int

Reference (Z, Y, X).

required
ref_spacing sequence of float

(z, y, x) voxel sizes, µm.

required
mov_spacing sequence of float

(z, y, x) voxel sizes, µm.

required
order int

Spline order (1: linear; 0: nearest, to show native pixels).

1

Returns:

Type Description
ndarray

float32 with the input's channel axis (if any) and the reference (Z, Y, X); NaN outside the moving field.

mip_ncc(ref, registered)

NCC of the maximum projections of the reference and a resampled stack (resample_to_ref), inside the field.

Both (C, Z, Y, X) or both (Z, Y, X); channels are combined as in registration_image.