vanandrew / neurosplitter

A collection of tools for neuroimaging data formatting for deep learning applications
0 stars 0 forks source link

nibabel flips image if the affine is negative #2

Open perronea opened 4 years ago

perronea commented 4 years ago

The affine matrix is taken into account when composing the image, but not when decomposing the image.

If the affine matrix of the original image is [[-1,0,0],[0,1,0],[0,0,1]] the image will be left right flipped. If you decompose that image and recompose it with an identity matrix it will appear the exact same (overlay properly in fslview) but the affine matrix will not be the same. If you try and recompose it with the original affine matrix the affine matrices will then be the same, but it will again flip left and right and the two images will not overlay.

I think the decompose needs to take into account the affine and flip the fdata along the appropriate axis according to the affine matrix. Maybe this is a question for the people at Nibabel.

vanandrew commented 4 years ago

I use nib.as_closest_canonical (see this) to force the orientation of image to be RAS+ in both the decompose/compose functions. This does intentionally change the affine in the header, which in turn can cause changes in the voxel ordering to keep things consistent to the orignal file. The reasoning for doing so was to make it easy to keep the --orientation flag consistent across different images (especially those that have non-diagonal affines).

Just to clarify, are you talking about comparing the two image arrays directly? Without projecting each to physical space (i.e. projecting from voxel to scanner space).

perronea commented 4 years ago

I'm not exactly sure. This came up because when I reconstruct an image with an identity matrix I am able to overlay them in fslview to do a visual comparison. Apparently other software or image viewers will project it to scanner space and if the identity matrices are not the same they cannot be overlaid like this. I tried to add in the original affine, but then it flips the image in fslview. As is I cannot satisfy everyone.

It seems that unless we want to force RAS+ orientation on all images from all subjects or think of another solution we will need to complicate the orientation function in decompose(). What do you think?

perronea commented 4 years ago

For my purposes I actually don't need to force RAS+ so I'm just going to take that out.

vanandrew commented 4 years ago

That's probably the best temp solution for now until we can come up with a better alternative.

Would it be possible for you to email me the relevant file to look at?