Hi,
I have a 2D sagittal slice stored in Nifti. It won’t open in slicer and I get the following error:
Exception thrown in event: D:\D\S\Slicer-1-build\ITK\Modules\Core\Common\include\itkImageBase.hxx:184:
itk::ERROR: itk::ERROR: Image(0000026509941440): Bad direction, determinant is 0. Direction is -0 0 0 0
1 -0 0 0
0 0 1 0
0 0 0 1
I can perfectly open the nifti using nibabel, and I could check that the header are not corrupted.
I get this same error in coronal, but not when I use axial slices.
My guess is that slicer is cropping the affine matrix because the data are 2D. The crop is probably fine for axial orientation, but wrong in sagittal or coronal.
FYI, the sagittal affine matrix in my example is like:
Even 2D slices need a non-singular 3D direction matrix, because even if you have a single slice in your image, the orientation, position, and spacing (thickness) must be specified in the 3D space.
You can fix this invalid file by setting the directions for all 3 axes. You can compute the direction of the third axis of the image slice as the cross product of the direction of the first two axes.
Hi Andras,
I agree with you, the affine matrix should not be singular. However, as I show in my example, it is absolutely not singular if I look at it with Nibabel in Python. However, 3DSlicer reads a different affine matrix which is singular. Any clues about what cause this behavior?
Thanks!
Probably nibabel adds the missing axis, but ITK just uses what is in the file. You can ask the opinion of ITK folks on the ITK forum, if they think that automatically fixing up the direction matrix is a good idea or not. But in the meantime, it is probably simpler to write the file correctly (or fix it, maybe by reading it using nibabel and writing it to a file).
What software created this nifti file?
Do you work with neuroimaging data? (if not, then I would recommend to stay away from nifti format, and use a better-behaving file format, such as nrrd)
Hi,
We work with breast MRI data, I agree that nifti is not optimal. However, we checked that the affine in the nifti is correct, so we cannot understand that 3DSlicer is seeing a completely different affine. Also, we tried ITK SNAP, and get the same error, so you’re right, it’s probably a good idea to ask ITK’s people.
Also, in order to allow the opening of this file in Slicer, we forced the affine to be the identity matrix, hence mimicking an axial acquisition, which worked. However, this does not orient the 2D slice correctly in the 3D physical reference system, so this is not a perfect solution.
I could not spot anything strange when comparing to an axial 2D nifti.
The nifti was generated with nibabel and an in-house custom python routine. It works well for 3D volume in any orientation, well in 2D axial, but seems to be problematic for Slicer with 2D sagittal and coronal.
Thank you, this shows what the problem is. The first value of dim field is 2 and so only the first two components of the srow matrix are used. Therefore the image axis directions are [0., 0.] and [-0.7813, 0.]. The [0., 0.] axis direction is invalid.
If your image lives in the 3D space then it does not matter how many slices your image has, you must set dimension to 3.
Probably nibabel just ignores the image dimension. This may or may not be correct. Probably nobody really knows what is the correct behavior. This kind of issues are very typical for nifti: the format is poorly defined, ambiguous, and therefore developers who implement it make arbitrary decisions that lead to incompatibilities and misinterpretations. Stay away from nifti. This kind of issues very rarely if ever happen if you use other file formats, such as nrrd. It is really just nifti that has all these problems.
Hi Andras,
Thanks a lot for pointing out the problem. This explanation makes perfect sense for me!
I also agree with your comment about nifti format. It is really a mess and we try to get rid of it as much as possible. I didn’t know nrrd format but I’ll have a look at it, it seems interesting.
Best regards