dcmstack icon indicating copy to clipboard operation
dcmstack copied to clipboard

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

Open Spenhouet opened this issue 3 years ago • 3 comments

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 https://github.com/nipy/nibabel/issues/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 avatar Aug 17 '22 08:08 Spenhouet

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.

Spenhouet avatar Aug 17 '22 15:08 Spenhouet

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.

moloney avatar Feb 10 '23 19:02 moloney

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).

Spenhouet avatar Feb 10 '23 19:02 Spenhouet