Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resampling: Ghost slices due to rounding errors #146

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

robertschweizer
Copy link

I am using dicom2nifti with resampling, to convert DICOM datasets with missing slices to NIfTI.

For certain z-spacings, the resulting NIfTI files have ghost slices at the end of the 3d array, which only contain 0s.

I found that the float32 header.get_zooms() value
when calling resample_nifti_images() leads to rounding errors.
Currently, resample_nifti_images() creates a ghost slice at the end of the nifti volume in this case.

Example code that triggers this problem:

z_spacing = 0.45  # In float64: 0.45000000000000001
nifti = Nifti1Image(
    dataobj=numpy.ones((3, 3, 3)),
    affine=numpy.array(
        [
            [1, 0, 0, 0],
            [0, 1, 0, 0],
            [0, 0, z_spacing, 0],
            [0, 0, 0, 1],
        ]
    ),
)
voxel_size = nifti.header.get_zooms()  # Rounds to float32: 0.44999998807907104492
nifti_out = resample_nifti_images([nifti], voxel_size)
assert nifti_out.shape == (3, 3, 3)  # Without the fix, this is (3, 3, 4)

I'm adding two commits here. Each one would prevent the ghost slices on its own, feel free to merge only one of them.

For certain z-spacings, using the float32 `header.get_zooms()` value
leads to rounding errors. Currently, `resample_nifti_images()` creates
a ghost slice at the end of the nifti volume.

This makes `resample_nifti_images()` robust
enough to handle such rounding errors.
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