How can I create a NIFTI image with the same orientation as an existing NIFTI image

I have a MATLAB program that reads a 256x256x190 NIFTI image, processes it to get 4D image data, and writes the resulting 4D data out as a binary file of floating point vales. I’m then trying to use an AFNI program to convert the binary file to a NIFTI image with the same geometry and orientation as the original 3D NIFTI image, with the exception of being 4D rather then 3D. This seems like it should be a simple thing to do, and to3d almost works, but it’s not setting the orientation properly. I’ve looked through the AFNI command line documentation, and I haven’t found any solution, so any help would be appreciated. The ‘to3d’ command I’m trying to use and the input and resulting image formats are shown below. Thanks in advance!

Cheers,
Steve

$ to3d -prefix newImg.nii -datum float -time:zt 190 3 4000 seqplus -geomparent parentIm.nii 3Df:0:0:256:256:570:temp.bin

$ 3dinfo parentIm.nii

Geometry String: “MATRIX(0.997633,-0.002181,-0.069076,-125.4405,0.01053,-0.982863,0.184971,64.55254,0.067951,0.184325,0.985491,-101.1443):256,256,190”
Data Axes Tilt: Oblique (11.330 deg. from plumb)
Data Axes Approximate Orientation:
first (x) = Right-to-Left
second (y) = Posterior-to-Anterior
third (z) = Inferior-to-Superior [-orient RPI]
R-to-L extent: -125.440 [R] -to- 129.560 [L] -step- 1.000 mm [256 voxels]
A-to-P extent: -190.447 [A] -to- 64.553 [P] -step- 1.000 mm [256 voxels]
I-to-S extent: -101.144 [I] -to- 88.815 [S] -step- 1.005 mm [190 voxels]
Number of values stored at each pixel = 1
– At sub-brick #0 ‘?’ datum type is short

$ 3dinfo newImg.nii

Geometry String: “MATRIX(1,0,0,-125.4405,0,-1,0,64.55254,0,0,1.005076,-101.1443):256,256,190”
Data Axes Tilt: Plumb
Data Axes Orientation:
first (x) = Right-to-Left
second (y) = Posterior-to-Anterior
third (z) = Inferior-to-Superior [-orient RPI]
R-to-L extent: -125.440 [R] -to- 129.560 [L] -step- 1.000 mm [256 voxels]
A-to-P extent: -190.447 [A] -to- 64.553 [P] -step- 1.000 mm [256 voxels]
I-to-S extent: -101.144 [I] -to- 88.815 [S] -step- 1.005 mm [190 voxels]
Number of time steps = 3 Time step = 4.00000s Origin = 0.00000s Number time-offset slices = 190 Thickness = 1.005
– At sub-brick #0 ‘?’ datum type is float: 0 to 2268.57
– At sub-brick #1 ‘?’ datum type is float: 0 to 3830.46
– At sub-brick #2 ‘?’ datum type is float: -1 to 1.68638e-19

Hi, Steve–

If your output volume has a a correct orientation, just not the one you want it to have (i.e., the header and data match), then you can use


3dresample -prefix OUTPUT -orient NEWORIENT -input DSET

to update both the header+data.

If the output dataset has a wrong orientation (i.e., the header and data do not match), then you can use:


3dcopy DSET DSET_COPY
3drefit  -orient NEWORIENT  DSET_COPY

to change the volume. (Note that you don’t have to copy the original DSET, I just did that here because the 3drefit will overwrite the input dset, and so changes are irreparable to it.)

–pt

I’ll add that since you have an original dataset with the proper orientation, using 3dresample with the -master flag would also be a quick way to fix this.

I don’t think it’s that easy. The image passed to to3d using the ‘geomparent’ parameter is oblique, but to3d seems to be ignoring the orientation and returning an image that’s plumb, as shown by the geometry strings returned by 3dinfo (below). The orientation isn’t just ‘LPI’ or some variation - the new image has to have the identical position and oblique orientation.

geomparent image Geometry String: “MATRIX(0.997633,-0.002181,-0.069076,-125.4405,0.01053,-0.982863,0.184971,64.55254,0.067951,0.184325,0.985491,-101.1443):256,256,190”

new image Geometry String: “MATRIX(1,0,0,-125.4405,0,-1,0,64.55254,0,0,1.005076,-101.1443):256,256,190”

Thanks,
Steve

That doesn’t do it either. Just to make sure I’m not missing something obvious, TI_FGATIR.nii is my original oblique 3D image, and temp.bin is the binary floating point 4D ‘image’ data file created from TI_FGATIR.nii. Then, the AFNI commands I’m using are:

to3d -prefix newImg.nii -datum float -time:zt 190 3 4000 seqplus -geomparent TI_FGATIR.nii 3Df:0:0:256:256:570:temp.bin
3dresample -master TI_FGATIR.nii -inset newImg.nii -prefix resampledNewImg.nii

But neither newImg.nii nor resampledNewImg.nii match TI_FGATIR.nii, as shown by the geometry strings returned by 3dinfo, something is still amiss:

3dinfo TI_FGATIR.nii -
Geometry String: “MATRIX(0.997633,-0.002181,-0.069076,-125.4405,0.01053,-0.982863,0.184971,64.55254,0.067951,0.184325,0.985491,-101.1443):256,256,190”
Data Axes Tilt: Oblique (11.330 deg. from plumb)

3dinfo newImg.nii -
Geometry String: “MATRIX(1,0,0,-125.4405,0,-1,0,64.55254,0,0,1.005076,-101.1443):256,256,190”
Data Axes Tilt: Plumb

3dinfo resampledNewImg.nii -
Geometry String: “MATRIX(1,0,0,-125.4405,0,-1,0,64.55254,0,0,1.005076,-101.1443):256,256,190”
Data Axes Tilt: Plumb