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

Embedding of meta data does not work for multi channel (RGB) data #74

Open
Spenhouet opened this issue Aug 17, 2022 · 3 comments
Open

Comments

@Spenhouet
Copy link

Spenhouet commented Aug 17, 2022

Pretext: I'm painfully aware that this package is no longer maintained. But we have no alternative and our only hope is to wait patiently for nipy/nibabel#977. I'm still reporting this bug here so it might be taken into consideration in the nibabel implementation.


For a 3D RGB image with shape [192, 256, 256, 3] the individual DICOM files (slices) have a shape of [256, 256, 3].

The method DcmMetaExtension.from_sequence currently can't handle a channel dimension.

It then fails at line 611 in dcmmeta.py:

if len(input_shape) > dim and input_shape[dim] != 1:
    raise ValueError("The dim must be singular or not exist for the inputs.")

where input_shape is [256, 256, 3], len(input_shape) is 3 but dim is 2.

@Spenhouet
Copy link
Author

Spenhouet commented Aug 17, 2022

This issue could be fixed at different places in dcmstack e.g. DcmMetaExtension.from_sequence.
For a quick fix for us it was easier to monkey patch nib.nifti1.Nifti1Image.__init__:

import nibabel as nib
import numpy as np

old_init = nib.nifti1.Nifti1Image.__init__


def new_init(
    self: nib.nifti1.Nifti1Image,
    dataobj: np.ndarray,
    affine: np.ndarray,
    *args,
    **kwargs,
):
    has_rgb_channel = dataobj.shape[-1] == 3

    if has_rgb_channel:
        rgb_dtype = np.dtype([('R', 'u1'), ('G', 'u1'), ('B', 'u1')])
        dataobj = dataobj.copy().view(dtype=rgb_dtype)  # type: ignore

    old_init(self, dataobj, affine, *args, **kwargs)  # type: ignore

    if has_rgb_channel:
        self.header.set_intent(code=2003)  # type: ignore


nib.nifti1.Nifti1Image.__init__ = new_init

import dcmstack

I hope that helps.

@moloney
Copy link
Owner

moloney commented Feb 10, 2023

Does Nifti have a spec for RGB, or is there any software that works with RGB Nifti files?

This isn't something I can imagine needing myself, but if someone else wants to create a good PR I can review and merge.

@Spenhouet
Copy link
Author

Not sure where to look for the official Nifti Spec but here RGB is mentioned: https://brainder.org/2012/09/23/the-nifti-file-format/

We often work with MRIcroGL and I believe it's able to display RGB NIfTIs. I think I did read that ITK-snap can also read them.

Maybe I can upload an example image next week(s).

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

No branches or pull requests

2 participants