Skip to content

fix wrong spacing from dicom - #100

Open
dsarrut wants to merge 2 commits into
masterfrom
fix_dicom_spacing
Open

dsarrut wants to merge 2 commits into
masterfrom
fix_dicom_spacing

Conversation

@dsarrut

@dsarrut dsarrut commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Problem

For a multi-file DICOM series without SpacingBetweenSlices (0018,0088) or
GridFrameOffsetVector (3004,000C), read_dicom set the z spacing from the
distance between the first two slices.

When ImagePositionPatient is stored with limited precision, that distance is
rounded, and the error is multiplied by the number of slices. Example with a
true pitch of 1.64557 mm:

Positions stored with z spacing before Error at slice 1150
2 decimals 1.64 or 1.65, depending on the first position −6.4 or +5.1 mm
3 decimals 1.645 −0.7 mm

The origin was correct; only the spacing was affected. The ITK series reader
gives 1.6456 on the same files. We saw the effect on whole-body PET series,
as a PET/CT mismatch growing from feet to head.

Change

  • New series_slice_spacing(slices, io): distance between the first and last
    slice positions along the slice normal, divided by (number of slices − 1).
  • read_dicom uses it for the z spacing when neither tag is present, in place
    of the distance between the first two slices.
  • When a tag is present it is still used. A warning is logged if it disagrees
    with the slice positions (last slice more than 10 % of a slice away).
  • A warning is logged when the slices are not evenly spaced (a gap more than
    10 % off the mean), e.g. a missing slice.

Behaviour change

Series without a spacing tag can get a slightly different z spacing than
before. Series with a tag are converted as before.

Unchanged: origin, direction, slice order, single-slice input, and
read_3d_dicom (single-file images such as RT dose or SPECT).

Tests

Four tests added to Test_Convert, using synthetic series written with
pydicom (no download):

  • positions rounded to 2, 3 or 6 decimals, no tag;
  • tag present: kept, with a warning when it disagrees with the positions;
  • a series with a missing slice (warning expected);
  • a single slice.

test_convert_unsigned_char, test_convert_unsigned_short and
test_convert_dicom_flip fail in my environment (Python 3.14, itk 5.4.7,
pydicom 3.0.2) with and without this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant