Repository navigation
Description
Describe the bug
I have the a series comprising 413 dicom slices the "ImagePositionPatient" tag for the slices are :
slice 1 : -216.500 \ -216.500 \ 70.000
slice 2: -216.500 \ -216.500 \ 68.750
.....
slice 413 : -216.500 \ -216.500 \ -445.000
The "ImageOrientation" is the same for all tags :
(0020,0037) Image Orientation Patient 1.000000\0.000000\0.000000\0.000000\1.000000\0.000000
I read these slices with the Image Series Reader:
reader = sitk.ImageSeriesReader()
reader.SetFileNames(dcm_list)
image = reader.Execute()
Reading the slice from the last to the first (bottom to top), the metadata of the final 3d image is inconsistant with the values of the dicom tags :
image.GetOrigin() -> (-216.5, -216.5, -445.0) seems OK
image.GetDirection() -> (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -1.0) not OK last vector should be 1 instead of -1
if I put the slices from the first to the last I get correct results :
image.GetOrigin() ->(-216.5, -216.5, 70.0) seems OK
image.GetDirection() -> (1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -1.0) now it is OK since we are reading from top to bottom
so depending on the order of the slice given, one give a coherent result in term of metadata and the other does not.
My understanding was that the code computed the 3d direction matrix with both the "image orientation" for the first two axes, and the vector defined by the vector separing the "image position patient" of the two first slice given. In that case it should be 0 / 0 / 1 when reading the image from bottom to top and 0 / 0 / -1 from top to bottom but last vector is always 0 / 0 / -1
To Reproduce
Hard to reproduce without sharing the dcm files itself, on request it can be done
Thanks !
Here are the full information of the final 3d image for reference :
Image (0x40bcd700)
RTTI typeinfo: itk::Image<int, 3u>
Reference Count: 1
Modified Time: 16338
Debug: Off
Object Name:
Observers:
none
Source: (none)
Source output name: (none)
Release Data: Off
Data Released: False
Global Release Data: Off
PipelineMTime: 1807
UpdateMTime: 16334
RealTimeStamp: 0 seconds
LargestPossibleRegion:
Dimension: 3
Index: [0, 0, 0]
Size: [512, 512, 413]
BufferedRegion:
Dimension: 3
Index: [0, 0, 0]
Size: [512, 512, 413]
RequestedRegion:
Dimension: 3
Index: [0, 0, 0]
Size: [512, 512, 413]
Spacing: [0.845703, 0.845703, 1.25]
Origin: [-216.5, -216.5, -445]
Direction:
1 0 0
0 1 0
0 0 -1
IndexToPointMatrix:
0.845703 0 0
0 0.845703 0
0 0 -1.25
PointToIndexMatrix:
1.18245 0 0
0 1.18245 0
0 0 -0.8
Inverse Direction:
1 0 0
0 1 0
0 0 -1
PixelContainer:
ImportImageContainer (0x40f65de0)
RTTI typeinfo: itk::ImportImageContainer<unsigned long, int>
Reference Count: 1
Modified Time: 1875
Debug: Off
Object Name:
Observers:
none
Pointer: 0x7fdb14418010
Container manages memory: true
Size: 108265472
Capacity: 108265472
Activity
blowekamp commentedon May 14, 2025
Does the following code reproduce your issue?
MMRohe commentedon May 14, 2025
Hello @blowekamp
thanks for your help ! here is the output of your sample code here :
So it seems to reproduce the bug on my side, both image have same direction while logically they should be reverse on the last axis
MMRohe commentedon May 14, 2025
I have investigated the issue a little bit more, using your code @blowekamp and testing with "plain itk" and I found that there is a discrepency between itk and sitk results. itk giving the right and expected whereas sitk has this error for the direction matrix, so it seems to be a sitk-problem,
It seems to be because sitk "force" orthogonal direction even though the slice are not, and the itk option "ForceOrthogonalDirectionOff" fixes this issue. it seems quite weird because it makes the final image not having the right meta data in term of physical direction as it is not implied by the dcm tag but force to be orthogonal. Do you know if there is a way to prevent this behavior in a similar way of "ForceOrthogonalDirectionOff" of itk ?
Here is the modified code snapshot, using both itk and sitk to read the same image :
and here is the output I get, with the itk / sitk difference :
blowekamp commentedon May 14, 2025
It looks like the behavior of this "ForceOrthogonalDirection" is buggy in ITK. And it looks like SimpleITK is not exposing this operations. I'll add it to SimpleITK as a work around, but it needs further investigation into the ITK code.
However, there is a default (required?) order returned by sitk::ImageSeriesReader::GetGDCMSeriesFileNames that is expected to work. I am not sure of all the DICOM details, and perhaps some DICOM experts can chime in here, with the DICOM standards.
@dzenanz InsightSoftwareConsortium/ITK@23436a0
MMRohe commentedon May 15, 2025
Hey @blowekamp I have managed to reproduce and investigate a little bit more the results, it seems that the sITK / ITK (and actually it seems to be a ITK issue) uses a some points the tag "SpacingBetweenSlice" to put an intermediate slice spacing value even though the Z spacing is correctly computed at the end using the interslice distance. This behaviour makes it bug somehow with the "ForceOrthogonalDirection" method, because I suspect that at this point it uses the intermediate (and unreliable / wrong) value "SpacingBetweenSlice" instead of the actual distance between two consecutive slice.
To reproduce the issue one can read two consecutive set of dcm slices just changing the value "SpacingBetweenSlice". It does not impact the Z spacing which is correctly computed, but it impact the direction matrix because of this buggy "ForceOrthogonalDirection" here you can find the output of the sample code below when putting a dummy value of -20 in "SpacingBetweenSlice" :
One can see that even though ITK/sITK correctly ignore this value in computing the spacing (but instead use the interslice distance), the fact that this value was added in the dcm tag cause the direction matrix to be wrongly computed by, i guess, "ForceOrthogonalDirection"
blowekamp commentedon May 15, 2025
These inline code examples are getting unwieldily. It may be better to use GH gist to share the code blocks.
dzenanz commentedon May 15, 2025
I think that InsightSoftwareConsortium/ITK@23436a0 is backwards compatible, with
ForceOrthogonalDirectionbeing the only behavior before that commit. Does the problem persist if you revert that commit?MMRohe commentedon May 15, 2025
I did not try to revert the commit, but I tested with simpleitk ==2.3.1 (instead of simpleitk == 2.4.1) which fixes the problem. So it seems that the issue appers somewhere inbetween version 2.3.1 and 2.4.1. I did not yet have time to investigate the changes of ITK corresponding to moving from SimpleITK 2.3.1 -> 2.4.1 and whether the commit you are referencing is part of it
blowekamp commentedon May 15, 2025
If you run
print(sitk.Version())information about the SimpleITK and ITK version is printed. SimpleITK 2.3.1 has a version of ITK along the 5.3.0 release branch and 2.4.1 should have ITK 5.4.3.I am currently testing the "ReverseOrder" flag and I'm getting some unexpected behavior with the image bulk data I'm currently looking into. This issue might be able to be when the image file name order is revered, the meta-data in ITK is the same answer as the original order.
MMRohe commentedon May 16, 2025
Ok thanks ! so it seems to be indeed a bug on the ForceOrthogonalDirection method added in ITK 5.4.3, not sure if I should open a new issue on the ITK github ?
dzenanz commentedon May 16, 2025
ForceOrthogonalDirectionwas added in 2018, and first appeared in a tagged release with v5.0b01. I am afraid this issue is unrelated to that feature.blowekamp commentedon May 16, 2025
The work around with the ForceOrthoganaDirection option is now available in the SimpleITK latest release:
https://github.com/SimpleITK/SimpleITK/releases/tag/latest
zhurong2020 commentedon Oct 3, 2026
Hi @blowekamp,
Thanks for fixing this in InsightSoftwareConsortium/ITK#5357. We tested several SimpleITK builds with a synthetic six-slice axial series whose files differ only in the sign of SpacingBetweenSlices (0018,0088), read in the default
GetGDCMSeriesFileNamesorder.ForceOrthogonalDirectionOff()Values are
direction[2,2]. In the failing case the voxel planes remain in ascending ImagePositionPatient order, but the direction matrix makes their reported physical z coordinates run in the opposite direction.The development build is correct, but the 2.4.x and 2.5.x releases we tested, including 2.5.6 on PyPI, still reproduce the problem. The corresponding change is present on ITK
mainbut not in the currentrelease-5.4version ofitkImageSeriesReader.hxx.The failure is silent and can affect downstream tools that reorient images using the direction matrix; we observed incorrect vertebral-level labels in such a segmentation workflow. In 2.5.6,
ForceOrthogonalDirectionOff()corrects this synthetic axial reproduction, though we have not validated it as a general workaround for other orientations.Would backporting #5357 to ITK
release-5.4, followed by a SimpleITK 2.5.x release, be feasible? If not, documentingForceOrthogonalDirectionOff()as a workaround for this case would still help. If you would prefer the backport request tracked in the ITK repository, we can open an issue there.Minimal synthetic reproduction (pydicom + SimpleITK, no patient data): https://gist.github.com/zhurong2020/563f974baaf33c30f910f98c4d06ebad
Best,
Rong Zhu
ORCID 0009-0008-6087-0581
blowekamp commentedon Oct 3, 2026
Please post here asap: InsightSoftwareConsortium/ITK#6859
zhurong2020 commentedon Nov 1, 2026
Thanks to Bradley and Hans for the fix and the backport, and to the SimpleITK maintainers for the release.
For anyone landing here later: SimpleITK 2.5.7 is built on ITK 5.4.8, which includes InsightSoftwareConsortium/ITK#6930. With 2.5.7, the reproduction script at the top of this thread now reports a last-axis direction of -1 for the top-to-bottom file order and +1 for the reversed order, as expected; 2.5.6 still reports +1 for both. The gist from my earlier comment also passes unchanged on 2.5.7, without needing
ForceOrthogonalDirectionOff().We also re-ran a larger synthetic set (SpacingBetweenSlices +1/-1/absent, sorted or reversed file lists, ForceOrthogonalDirection default or off, three orientations; 36 runs) and six public Philips CT series with negative SpacingBetweenSlices, read in the default
GetGDCMSeriesFileNamesorder. All were correct on 2.5.7, while the six public series had wrong slice positions on 2.5.6.From our side this is resolved; I'll leave closing to the original reporter and the maintainers.
DOC: Note ImageSeriesReader direction fix (SimpleITK#2292) in v2.5.7 …
eliem08 commentedon Nov 20, 2026
Hi @MMRohe and @blowekamp — I'm picking this up to close out the remaining gap.
First, the fix itself is already in place, so there is nothing left to fix in the reader: ITK #5357 (backported as #6930 to release-5.4 / ITK 5.4.8) corrects the sign of the last direction column when
ForceOrthogonalDirectionis on, and SimpleITK 2.5.7 ships it — I verified with a synthetic 3-slice series matching the issue (IPP z = 70 / -187.5 / -445, axial IOP, SBS absent / +1 / -1, both file orders): 2.5.7 reports direction(0,0,-1)top-to-bottom and(0,0,+1)bottom-to-top with correct origins and physical voxel positions in all 6 runs, while 2.5.6 gets the top-to-bottom direction wrong, as reported. Thanks to @zhurong2020 for the 2.5.7 confirmation in November — that matches what I see.What is still missing is a regression test in SimpleITK itself: the existing
DicomSeriesReadertest only tolerates both behaviors (magnitude-only z-column check), so a reintroduction of this bug would not be caught. I am opening a PR that addsTEST(IO, ImageSeriesReader_DirectionFromSliceOrder)inTesting/Unit/sitkImageIOTests.cxx, pinning the corrected contract (exact origin, full direction matrix, z-spacing, and physical voxel-plane positions) for the synthetic series in both file orders and all threeSpacingBetweenSlicesvariants.One honest caveat: I could not compile the C++ test here (CPU-only machine, no full SuperBuild), so the PR is explicit that CI needs to run it. The test logic was verified instead via an exact Python mirror against pip SimpleITK 2.5.7 (passes) and 2.5.6 (fails on the buggy direction, as expected).
Once that PR lands, I believe this issue can be closed — the reader behavior is fixed in 2.5.7+ and pinned by the new test.
BUG: Add regression test for ImageSeriesReader direction from slice o…
BUG: Add test for ImageSeriesReader direction from slice order (Simpl…
BUG: Add test for ImageSeriesReader direction from slice order