How to use sct_image to fix the orientation of the header and data array? (-setorient-data vs. -setorient)

Hi SCT Expert,

I’m so sorry to bother you again regarding this matter. However, having RPI orientation is very important in order to proceed with my analysis and crop my DWI with alignment to T1. We have already discussed this matter and we enforced another orientation e.g AIL and PSL to my data but did not work out as the T1 data coordinate cervical segments were produced on T1 and used to generate T1 template are in RPI orientation (Charley’s T1 dataset). Please find below my issue if you have time to advice please.

I’m trying to re-orient all my ex-vivo T1 and DWI data from ASL to RPI orientation (attached one sample have been already in this orientation).

I have tried the following command as suggested before but no change in the actual orientation when I viewed the output.

Syntax used:

sct_image -i 3904_T1_top.nii.gz -setorient-data RPI -o TOP-T1-RPI.nii.gz

Data Link:

https://drive.google.com/drive/folders/1ik70oZGwSNTEw-VMKwGCZDZN0ZPVJVS-?usp=sharing

I used this version spinalcordtoolbox/5.5

Could you please advice me what should be done?

BTW, here is one sample that were registered to T1/reoriented to RPI via enforcing the orientation using Charley’ mask and cropped from C3-C6 based on Charley T1 coordinate level. However, this method didn’t work with other dataset.




Many thanks in advance and accept my apologizes again.

Cheers,

Ibrahim

Hi @ihattan,

There are two things to mention here:

  1. For typical reorientation operations, you will want to use the -setorient option instead:

    sct_image -i 3904_T1_top.nii.gz -setorient RPI -o TOP-T1-RPI.nii.gz
    

    This is because a NIfTI image is composed of both a data array and a metadata header. When you reorient an image, you will want to make sure to update both together. The -setorient-data option will only update the data array (but not the header metadata), hence the issue.

    You can double check the header metadata of the new output image by using sct_image -header, like this:

    Header metadata for 3904_T1_top.nii.gz
    joshua@tadpole:~$ sct_image -i 3904_T1_top.nii.gz -setorient RPI -o TOP-T1-RPI.nii.gz
    
    --
    Spinal Cord Toolbox (git-master-84060e21e95ec75a0b0c2df195ac1d95a8ca5fab*)
    
    sct_image -i 3904_T1_top.nii.gz -setorient RPI -o TOP-T1-RPI.nii.gz
    --
    
    /home/joshua/3904_T1_top.nii.gz
    Generate output files...
    File TOP-T1-RPI.nii.gz already exists. Will overwrite it.
    joshua@tadpole:~$ sct_image -i 3904_T1_top.nii.gz -header 
    
    --
    Spinal Cord Toolbox (git-master-84060e21e95ec75a0b0c2df195ac1d95a8ca5fab*)
    
    sct_image -i 3904_T1_top.nii.gz -header
    --
    
    sizeof_hdr	348
    data_type	UINT16
    dim		[4, 256, 1024, 256, 2, 1, 1, 1]
    vox_units	mm
    time_units	s
    datatype	512
    nbyper		2
    bitpix		16
    pixdim		[1.0, 0.078125, 0.078125, 0.078125, nan, 0.0, 0.0, 0.0]
    vox_offset	352
    cal_max		0.000000
    cal_min		0.000000
    scl_slope	1.000000
    scl_inter	0.000000
    phase_dim	0
    freq_dim	0
    slice_dim	0
    slice_name	Unknown
    slice_code	0
    slice_start	0
    slice_end	0
    slice_duration	0.000000
    toffset		0.000000
    intent		Unknown
    intent_code	0
    intent_name	
    intent_p1	0.000000
    intent_p2	0.000000
    intent_p3	0.000000
    qform_name	Scanner Anat
    qform_code	1
    qto_xyz:1	0.000000 0.000000 0.078125 -8.800000 
    qto_xyz:2	-0.078125 0.000000 0.000000 10.300000 
    qto_xyz:3	0.000000 -0.078125 0.000000 40.000000 
    qto_xyz:4	0.000000 0.000000 0.000000 1.000000 
    qform_xorient	Anterior-to-Posterior
    qform_yorient	Superior-to-Inferior
    qform_zorient	Left-to-Right
    sform_name	Scanner Anat
    sform_code	1
    sto_xyz:1	-0.000000 -0.000000 0.078125 -8.800000 
    sto_xyz:2	-0.078125 -0.000000 -0.000000 10.300000 
    sto_xyz:3	0.000000 -0.078125 0.000000 40.000000 
    sto_xyz:4	0.000000 0.000000 0.000000 1.000000 
    sform_xorient	Anterior-to-Posterior
    sform_yorient	Superior-to-Inferior
    sform_zorient	Left-to-Right
    file_type	NIFTI-1+
    file_code	1
    descrip		MRtrix version: 3.0_RC2-7-g4ed6d5ef
    aux_file
    
    Header metadata for TOP-T1-RPI.nii.gz
    joshua@tadpole:~$ sct_image -i TOP-T1-RPI.nii.gz -header
    
    --
    Spinal Cord Toolbox (git-master-84060e21e95ec75a0b0c2df195ac1d95a8ca5fab*)
    
    sct_image -i TOP-T1-RPI.nii.gz -header
    --
    
    sizeof_hdr	348
    data_type	UINT16
    dim		[4, 256, 256, 1024, 2, 1, 1, 1]
    vox_units	mm
    time_units	s
    datatype	512
    nbyper		2
    bitpix		16
    pixdim		[-1.0, 0.078125, 0.078125, 0.078125, nan, 1.0, 1.0, 1.0]
    vox_offset	352
    cal_max		0.000000
    cal_min		0.000000
    scl_slope	1.000000
    scl_inter	0.000000
    phase_dim	0
    freq_dim	0
    slice_dim	0
    slice_name	Unknown
    slice_code	0
    slice_start	0
    slice_end	0
    slice_duration	0.000000
    toffset		0.000000
    intent		Unknown
    intent_code	0
    intent_name	
    intent_p1	0.000000
    intent_p2	0.000000
    intent_p3	0.000000
    qform_name	Scanner Anat
    qform_code	1
    qto_xyz:1	-0.078125 0.000000 0.000000 11.121875 
    qto_xyz:2	0.000000 0.078125 0.000000 -9.621875 
    qto_xyz:3	0.000000 0.000000 0.078125 -39.921875 
    qto_xyz:4	0.000000 0.000000 0.000000 1.000000 
    qform_xorient	Right-to-Left
    qform_yorient	Posterior-to-Anterior
    qform_zorient	Inferior-to-Superior
    sform_name	Scanner Anat
    sform_code	1
    sto_xyz:1	-0.078125 0.000000 0.000000 11.121875 
    sto_xyz:2	0.000000 0.078125 0.000000 -9.621875 
    sto_xyz:3	0.000000 0.000000 0.078125 -39.921875 
    sto_xyz:4	0.000000 0.000000 0.000000 1.000000 
    sform_xorient	Right-to-Left
    sform_yorient	Posterior-to-Anterior
    sform_zorient	Inferior-to-Superior
    file_type	NIFTI-1+
    file_code	1
    descrip		MRtrix version: 3.0_RC2-7-g4ed6d5ef
    aux_file	
    

    It looks like the orientation of the image has changed from ASL to RPI as expected. :slight_smile:

    Additionally, when I open up the newly-generated TOP-T1-RPI.nii.gz in FSLeyes, I see this:

    I can verify that the image header is in the correct orientation, because the FSLeyes cursor behaves as follows:

    • Increasing X-axis coordinates == FSLeyes cursor changes from right to left
    • Increasing Y-axis coordinates == FSLeyes cursor changes from posterior to anterior
    • Increasing Z-axis coordinates == FSLeyes cursor changes from inferior to superior

    Lastly, I can check that the orientation of the data array itself is preserved compared to your input image 3904_T1_top.nii.gz:

    In both images, the ventral horns point towards posterior direction, and the dorsal horns point towards anterior direction, which is expected.

  2. I do notice, though, that data arrays of both 3904_T1_top.nii.gz and TOP-T1-RPI.nii.gz are oriented differently from your “correctly oriented” sample:

    In this “correct” sample screenshot you gave, the ventral horns point towards the anterior direction, while the dorsal horns point towards the posterior direction. But, 3904_T1_top.nii.gz (and subsequently TOP-T1-RPI.nii.gz) appear to be flipped when compared to this sample.

    If this is unexpected, then you may want to double-check the data arrays of all of your different exvivo images, to make sure that they are oriented in the way that you expect them to be (anatomical features positioned with respect to the LR/AP/SI axes) .

    If your original image is not properly oriented, then you will first need to reorient the data array only for the input image, and then use -setorient RPI as recommended in point 1.

Kind regards,
Joshua

1 Like

Hi @joshuacwnewton,

Thank you very much for your invaluable information. Thanks again for re-producing the analysis steps. However, I’m still curious why sct_image -setorient RPI not responding while was working in the past (see attached spinal segmentation and header).

  • spinal seg. Header (re-oriented to RPI) that provided by @Charley_Gros which was in ASL orientation before.
sct_image -i 3889_top_sc.nii.gz -header
--

sizeof_hdr	348
data_type	FLOAT64
dim		[3, 250, 250, 1000, 1, 1, 1, 1]
vox_units	mm
time_units	s
datatype	64
nbyper		8
bitpix		64
pixdim		[-1.0, 0.08, 0.08, 0.08, 1.0, 1.0, 1.0, 1.0]
vox_offset	352
cal_max		0.000000
cal_min		0.000000
scl_slope	1.000000
scl_inter	0.000000
phase_dim	0
freq_dim	0
slice_dim	0
slice_name	Unknown
slice_code	0
slice_start	0
slice_end	0
slice_duration	0.000000
toffset		0.000000
intent		Unknown
intent_code	0
intent_name	
intent_p1	0.000000
intent_p2	0.000000
intent_p3	0.000000
qform_name	Scanner Anat
qform_code	1
qto_xyz:1	-0.080000 0.000000 0.000000 10.119999 
qto_xyz:2	0.000000 0.080000 0.000000 -6.619999 
qto_xyz:3	0.000000 0.000000 0.080000 -39.919998 
qto_xyz:4	0.000000 0.000000 0.000000 1.000000 
qform_xorient	Right-to-Left
qform_yorient	Posterior-to-Anterior
qform_zorient	Inferior-to-Superior
sform_name	Scanner Anat
sform_code	1
sto_xyz:1	-0.080000 0.000000 0.000000 10.119999 
sto_xyz:2	0.000000 0.080000 0.000000 -6.619999 
sto_xyz:3	0.000000 0.000000 0.080000 -39.919998 
sto_xyz:4	0.000000 0.000000 0.000000 1.000000 
sform_xorient	Right-to-Left
sform_yorient	Posterior-to-Anterior
sform_zorient	Inferior-to-Superior
file_type	NIFTI-1+
file_code	1
descrip		TE=20;sec=36619.0000
aux_file	

  • Raw T1 header before as seen below…
sct_image -i TI_3889.top.nii -header
--

sizeof_hdr	348
data_type	UINT16
dim		[3, 250, 1000, 250, 1, 1, 1, 1]
vox_units	mm
time_units	s
datatype	512
nbyper		2
bitpix		16
pixdim		[1.0, 0.08, 0.08, 0.08, 0.0, 0.0, 0.0, 0.0]
vox_offset	352
cal_max		0.000000
cal_min		0.000000
scl_slope	1.000000
scl_inter	0.000000
phase_dim	0
freq_dim	0
slice_dim	0
slice_name	Unknown
slice_code	0
slice_start	0
slice_end	0
slice_duration	0.000000
toffset		0.000000
intent		Unknown
intent_code	0
intent_name	
intent_p1	0.000000
intent_p2	0.000000
intent_p3	0.000000
qform_name	Scanner Anat
qform_code	1
qto_xyz:1	0.000000 0.000000 0.080000 -9.800000 
qto_xyz:2	-0.080000 0.000000 0.000000 13.300000 
qto_xyz:3	0.000000 -0.080000 0.000000 40.000000 
qto_xyz:4	0.000000 0.000000 0.000000 1.000000 
qform_xorient	Anterior-to-Posterior
qform_yorient	Superior-to-Inferior
qform_zorient	Left-to-Right
sform_name	Scanner Anat
sform_code	1
sto_xyz:1	-0.000000 -0.000000 0.080000 -9.800000 
sto_xyz:2	-0.080000 -0.000000 -0.000000 13.300000 
sto_xyz:3	0.000000 -0.080000 0.000000 40.000000 
sto_xyz:4	0.000000 0.000000 0.000000 1.000000 
sform_xorient	Anterior-to-Posterior
sform_yorient	Superior-to-Inferior
sform_zorient	Left-to-Right
file_type	NIFTI-1+
file_code	1
descrip		MRtrix version: 3.0_RC2-7-g4ed6d5ef
aux_file	
![raw_T1_ORIENTATION|690x320](upload://ejd46uXDxCO9ZRuHg8dtP4czcys.jpeg)
  • So, I reproduced same result as you did using sct_image -setorient RPI but no fliping noticed when viewed it. I’ll explain how I got the correct orientation even the method does not seem the correct one. I tried to enforce the data to RPI to test cropping for segment level by co-registering the data to the corrected spinal seg
  1. I co-registered B0-mean to T1 using the following syntax

flirt -dof 9 -searchrx -180 180 -searchry -180 180 -searchrz -180 180 -in b0-tmean.nii.gz -ref TI_3889.top.nii -omat b0-t1.mat -out flirt-b0-t1.nii.gz

  1. I co-registered the T1 to its RPI seg using
    flirt -dof 9 -searchrx -180 180 -searchry -180 180 -searchrz -180 180 -in TI_3889.top.nii -ref 3889_top_sc.nii.gz -omat t1-RPI-seg.mat -out t1-RPI-seg.nii.gz

  2. Then, I applied the output transformation t1-RPI-seg.mat to the flirt-b0-t1.nii.gz in step 1 using this syntax

flirt -in flirt-b0-t1.nii.gz -ref 3889_top_sc.nii.gz -applyxfm -init t1-RPI-seg.mat -out b0-t1-RPI-seg.nii.gz

in this way I got the RPI see the new orientation and header info please

  • Header for b0-t1-RPI-seg.nii.gz
sct_image -i b0-t1-RPI-seg.nii.gz -header
--

sizeof_hdr	348
data_type	FLOAT32
dim		[3, 250, 250, 1000, 1, 1, 1, 1]
vox_units	mm
time_units	s
datatype	16
nbyper		4
bitpix		32
pixdim		[-1.0, 0.08, 0.08, 0.08, 0.0, 1.0, 1.0, 1.0]
vox_offset	352
cal_max		0.000000
cal_min		0.000000
scl_slope	1.000000
scl_inter	0.000000
phase_dim	0
freq_dim	0
slice_dim	0
slice_name	Unknown
slice_code	0
slice_start	0
slice_end	0
slice_duration	0.000000
toffset		0.000000
intent		Unknown
intent_code	0
intent_name	
intent_p1	0.000000
intent_p2	0.000000
intent_p3	0.000000
qform_name	Scanner Anat
qform_code	1
qto_xyz:1	-0.080000 0.000000 0.000000 10.119999 
qto_xyz:2	0.000000 0.080000 0.000000 -6.619999 
qto_xyz:3	0.000000 0.000000 0.080000 -39.919998 
qto_xyz:4	0.000000 0.000000 0.000000 1.000000 
qform_xorient	Right-to-Left
qform_yorient	Posterior-to-Anterior
qform_zorient	Inferior-to-Superior
sform_name	Scanner Anat
sform_code	1
sto_xyz:1	-0.080000 0.000000 0.000000 10.119999 
sto_xyz:2	0.000000 0.080000 0.000000 -6.619999 
sto_xyz:3	0.000000 0.000000 0.080000 -39.919998 
sto_xyz:4	0.000000 0.000000 0.000000 1.000000 
sform_xorient	Right-to-Left
sform_yorient	Posterior-to-Anterior
sform_zorient	Inferior-to-Superior
file_type	NIFTI-1+
file_code	1
descrip		6.0.5:9e026117

  • Header for t1-RPI-seg.nii.gz
sct_image -i t1-RPI-seg.nii.gz -header
--

sizeof_hdr	348
data_type	INT32
dim		[3, 250, 250, 1000, 1, 1, 1, 1]
vox_units	mm
time_units	s
datatype	8
nbyper		4
bitpix		32
pixdim		[-1.0, 0.08, 0.08, 0.08, 1.0, 1.0, 1.0, 1.0]
vox_offset	352
cal_max		0.000000
cal_min		0.000000
scl_slope	1.000000
scl_inter	0.000000
phase_dim	0
freq_dim	0
slice_dim	0
slice_name	Unknown
slice_code	0
slice_start	0
slice_end	0
slice_duration	0.000000
toffset		0.000000
intent		Unknown
intent_code	0
intent_name	
intent_p1	0.000000
intent_p2	0.000000
intent_p3	0.000000
qform_name	Scanner Anat
qform_code	1
qto_xyz:1	-0.080000 0.000000 0.000000 10.119999 
qto_xyz:2	0.000000 0.080000 0.000000 -6.619999 
qto_xyz:3	0.000000 0.000000 0.080000 -39.919998 
qto_xyz:4	0.000000 0.000000 0.000000 1.000000 
qform_xorient	Right-to-Left
qform_yorient	Posterior-to-Anterior
qform_zorient	Inferior-to-Superior
sform_name	Scanner Anat
sform_code	1
sto_xyz:1	-0.080000 0.000000 0.000000 10.119999 
sto_xyz:2	0.000000 0.080000 0.000000 -6.619999 
sto_xyz:3	0.000000 0.000000 0.080000 -39.919998 
sto_xyz:4	0.000000 0.000000 0.000000 1.000000 
sform_xorient	Right-to-Left
sform_yorient	Posterior-to-Anterior
sform_zorient	Inferior-to-Superior
file_type	NIFTI-1+
file_code	1
descrip		6.0.5:9e026117
  • The above 2 screenshot were re-oriented using the below mask and header.

  • seg. header info.
sct_image -i 3889_top_sc.nii.gz -header
--

sizeof_hdr	348
data_type	FLOAT64
dim		[3, 250, 250, 1000, 1, 1, 1, 1]
vox_units	mm
time_units	s
datatype	64
nbyper		8
bitpix		64
pixdim		[-1.0, 0.08, 0.08, 0.08, 1.0, 1.0, 1.0, 1.0]
vox_offset	352
cal_max		0.000000
cal_min		0.000000
scl_slope	1.000000
scl_inter	0.000000
phase_dim	0
freq_dim	0
slice_dim	0
slice_name	Unknown
slice_code	0
slice_start	0
slice_end	0
slice_duration	0.000000
toffset		0.000000
intent		Unknown
intent_code	0
intent_name	
intent_p1	0.000000
intent_p2	0.000000
intent_p3	0.000000
qform_name	Scanner Anat
qform_code	1
qto_xyz:1	-0.080000 0.000000 0.000000 10.119999 
qto_xyz:2	0.000000 0.080000 0.000000 -6.619999 
qto_xyz:3	0.000000 0.000000 0.080000 -39.919998 
qto_xyz:4	0.000000 0.000000 0.000000 1.000000 
qform_xorient	Right-to-Left
qform_yorient	Posterior-to-Anterior
qform_zorient	Inferior-to-Superior
sform_name	Scanner Anat
sform_code	1
sto_xyz:1	-0.080000 0.000000 0.000000 10.119999 
sto_xyz:2	0.000000 0.080000 0.000000 -6.619999 
sto_xyz:3	0.000000 0.000000 0.080000 -39.919998 
sto_xyz:4	0.000000 0.000000 0.000000 1.000000 
sform_xorient	Right-to-Left
sform_yorient	Posterior-to-Anterior
sform_zorient	Inferior-to-Superior
file_type	NIFTI-1+
file_code	1
descrip		TE=20;sec=36619.0000
aux_file	
  • However, this method has failed in most of my data. Also, I tried the following syntax to copy the desired header to the data that were failed. Unfortunately, it didn’t work out.

“fslcpgeom - copy certain parts of the header information (image dimensions, voxel dimensions, voxel dimensions units string, image orientation/origin or qform/sform info) from one image to another. Note that only copies from Analyze to Analyze or Nifti to Nifti will work properly. Copying from different files will result in loss of information or potentially incorrect settings.”

Could you please review my steps and advice if possible with a solution to this matter?

  • BTW, for this advice " then you will first need to reorient the data array only for the input image"

how to achieve it please?

Many thanks in advance.

Best,

Ibrahim

My sincere apologies, but I am not personally familiar with the flirt tool, so I don’t think I am able comment on the steps you tried. (@jcohenadad may want to chip in here?)


But, for your second question:

Our goal is to make sure that each image in your dataset is consistent: e.g. we would want the ventral horns to point towards the anterior direction, and the dorsal horns to point towards the posterior direction, across all of your images.

To fix your 3904_T1_top.nii.gz image, then, we need to flip the AP axis of the data array.

Since the header of the image says:

sform_xorient	Anterior-to-Posterior
sform_yorient	Superior-to-Inferior
sform_zorient	Left-to-Right

Specifying -setorient-data PSL will have the effect of flipping just the AP axis.

sct_image -i 3904_T1_top.nii.gz -setorient-data PSL -o 3904_T1_top_AP-flipped.nii.gz

After this, the header metadata will stay the same (“ASL”), but the data array will now be oriented the same way as your other sample (ventral horns pointing towards the anterior direction, dorsal horns pointing towards the posterior direction):


Note: The RL axis of the 3904_T1_top.nii.gz image may also need to be flipped. However, because this is an exvivo scan, it’s difficult for me to tell which is the “correct” left and right sides – it would depend on how the cord was oriented in the scanner, and whether you used any sort of markers for left/right during your scans.


Now that we have corrected the AP axis of the input image’s data array, we can perform the typical -setorient command to change the image from ASL to RPI:

sct_image -i 3904_T1_top_AP-flipped.nii.gz -setorient RPI -o TOP-T1-RPI.nii.gz

The output of this command is an image with “RPI” in the header, that also has a correctly-oriented data array:


I hope this makes sense. Please don’t hesitate to ask further questions if anything is unclear – orientation is a confusing subject! :slight_smile:

Kind regards,
Joshua

1 Like

Many thanks @joshuacwnewton. I appreciate your time and thank you for your invaluable feedback.

For this issue " Note: The RL axis of the 3904_T1_top.nii.gz image may also need to be flipped. However, because this is an exvivo scan, it’s difficult for me to tell which is the “correct” left and right sides – it would depend on how the cord was oriented in the scanner, and whether you used any sort of markers for left/right during your scans."

Could sct_image -setorient handle the flipping? if yes,

sct_image -setorient syntax shows too many option " [-setorient {RIP,LIP,RSP,LSP,RIA,LIA,RSA,LSA,IRP,ILP,SRP,SLP,IRA,ILA,SRA,SLA,RPI,LPI,RAI,LAI,RPS,LPS,RAS,LAS,PRI,PLI,ARI,ALI,PRS,PLS,ARS,ALS,IPR,SPR,IAR,SAR,IPL,SPL,IAL,SAL,PIR,PSR,AIR,ASR,PIL,PSL,AIL,ASL}]"

Which one could be used to flip RL to LR axis please?

I’ll try these steps for my other dataset.

Thanks once again.

Cheers,

Ibrahim

You will want to use -setorient-data option for flipping the RL to LR axis, as well.

The choice of syntax will depend on your input data. For example, if your input data has “ASL” in the header, then you will want to use -setorient-data ASR. (Which will keep the “A” and “S” axes the same, but flip the “L” axis to “R”.)

I agree that the syntax isn’t very clear right now – We have opened an issue for this (#3714), and we will try to make sure that the “help” description is clearer in future versions. :slight_smile:

1 Like

Many thanks @joshuacwnewton for your swift response. The input would be the TOP-T1-RPI.nii.gz (RPI) below. However, I used the ASL as input and works but got messed up orientation when I proceeded with ASR as input using first the -setorient-data PSL and then -setorient RPI.

My sincere apologies again.

Cheers,

Ibrahim

My apologies! My previous response was a bit unclear.

If your goal is to swap both the AP and LR axes of the original 3904_T1_top.nii.gz image, you would use:

sct_image -i 3904_T1_top.nii.gz -setorient-data PSR -o 3904_T1_top_AP-flipped_LR-flipped.nii.gz

Using -setorient-data PSR will take the input ASL image and flip “A” → “P” and “L” → “R” at the same time.

Then, you would pass this image to -setorient RPI like before:

sct_image -i 3904_T1_top_AP-flipped_LR-flipped.nii.gz -setorient RPI -o TOP-T1-RPI.nii.gz

This should result in the following image:

Where both the AP and LR axes of the data array have been flipped prior to the reorientation of the image.

1 Like

Fantastic :tada:

1 Like

Hi @joshuacwnewton,
Sorry to bother regarding the problematic orientation matter. The issue with ASL orientation input to RPI has been solved for some dataset. However, I could not solve LPI orientation as input to get the RPI orientation despite of many attempts using the above syntax i.e ASL, RPI, ASR, RSA, PSL, LIP, PIL etc… I used all those output from the previous syntax to test the RPI orientation but did not work.

Would you be able to advice winch the suitable syntax for my case please?

Here is the data header

data_type	INT32
dim		[3, 256, 256, 1024, 1, 1, 1, 1]
vox_units	mm
time_units	s
datatype	8
nbyper		4
bitpix		32
pixdim		[1.0, 0.078125, 0.078125, 0.078125, 0.0, 0.0, 0.0, 0.0]
vox_offset	352
cal_max		0.000000
cal_min		0.000000
scl_slope	1.000000
scl_inter	0.000000
phase_dim	0
freq_dim	0
slice_dim	0
slice_name	Unknown
slice_code	0
slice_start	0
slice_end	0
slice_duration	0.000000
toffset		0.000000
intent		Unknown
intent_code	0
intent_name	
intent_p1	0.000000
intent_p2	0.000000
intent_p3	0.000000
qform_name	Scanner Anat
qform_code	1
qto_xyz:1	0.078125 0.000000 0.000000 -9.200000 
qto_xyz:2	0.000000 0.078125 0.000000 -9.921875 
qto_xyz:3	0.000000 0.000000 0.078125 -39.921875 
qto_xyz:4	0.000000 0.000000 0.000000 1.000000 
qform_xorient	Left-to-Right
qform_yorient	Posterior-to-Anterior
qform_zorient	Inferior-to-Superior
sform_name	Scanner Anat
sform_code	1
sto_xyz:1	0.078125 0.000000 0.000000 -9.200000 
sto_xyz:2	0.000000 0.078125 0.000000 -9.921875 
sto_xyz:3	0.000000 0.000000 0.078125 -39.921875 
sto_xyz:4	0.000000 0.000000 0.000000 1.000000 
sform_xorient	Left-to-Right
sform_yorient	Posterior-to-Anterior
sform_zorient	Inferior-to-Superior
file_type	NIFTI-1+
file_code	1
descrip		6.0.5:9e026117

Thanks once again for your help and invaluable feedback.

Cheers,

Ibrahim

Hi @ihattan,

No worries whatsoever! I’m happy to help. :slight_smile:

The exact steps, though, will be data-dependent, so I’m not able to guide you unless you are able to share the specific image you’re having trouble with. Are you able to share the image via Dropbox/Google Drive as before?


I will say, as a general guideline, there are 2 possible cases:

  1. The input image has a correctly-oriented data array. (In other words, the orientation of the data array matches the information in the header.)

    In this case, all you would need to do is use “-setorient RPI”.

  2. The input image has an incorrectly-oriented data array. (In other words, the orientation of the data array does not match the information in the header.)

    In this case, you will first need to fix the data array using “-setorient-data”.

    However, the exact command to use (to fix the data array) depends on how the data array is incorrect. (You have to ask yourself “Which axes are wrong and need to be flipped?” etc. which will inform the command you need to use.)

    Then, after you have fixed the data array, you can safely use "-setorient RPI".

Kind regards,
Joshua

1 Like

Hi @joshuacwnewton ,

Thank you very much for your help. Sorry for the delay and please find the data link below.

https://drive.google.com/drive/folders/1ik70oZGwSNTEw-VMKwGCZDZN0ZPVJVS-?usp=sharing

Please let me know if there is any issue while downloading/viewing so I can send it out again.

Many Thanks in advance,

Kind regards,

Ibrahim

Thank you for sharing, @ihattan!

When I open up the “4225_bottom_1.nii” image, I see the following:

image

Similar to your previous image, the dorsal horns point towards the anterior direction, and the ventral horns point towards the posterior direction. So, we know we are dealing with “Case 2”:

We can likely assume that the incorrect data array was caused by a rotation of the cord in the scanner prior to acquisition. A rotation can be represented as 2 reflections. Since we know there was a reflection in the AP axis, the question now is “Was the cord reflected in the LR axis or the SI axis?”


  1. Using “-setorient-data” to fix the data array.

    Say, for example, that you wanted to also flip the LR axis (in addition to the AP axis). Since the header of the image contains the information “LPI”, then to flip both the AP and LR axes, we will need to use this command:

    sct_image -i 4225_bottom_1.nii -setorient-data RAI -o 4225_bottom_1_AP-flipped_LR-flipped.nii
    

    We specify “RAI” because the input image is “LPI”. Therefore:

    • “L” → “R”
    • “P” → “A”
    • The “I” axis will stay the same

    If you wanted to flip the AP and SI axes instead, then you would use this command:

    sct_image -i 4225_bottom_1.nii -setorient-data LAS -o 4225_bottom_1_AP-flipped_SI-flipped.nii
    

    We specify “LAS” because the input image is “LPI”. Therefore:

    • The “L” axis will stay the same
    • “P” → “A”
    • “I” → “S”

    Which command you will need to use depends on which axes you want to fix.


  1. Using “-setorient” to change the orientation of the image.

    After we have fixed the data array, now we can use -setorient RPI like normal.

    For example, if we use the image that had both AP and LR axes flipped:

    sct_image -i 4225_bottom_1_AP-flipped_LR-flipped.nii -setorient RPI -o 4225_bottom_1_RPI.nii
    

    The header now says:

    sform_xorient   Right-to-Left
    sform_yorient   Posterior-to-Anterior
    sform_zorient   Inferior-to-Superior
    

    And when I open this image, I see:

I hope this clears up how to use -setorient-data. Please let me know if you have any further questions or concerns. :slight_smile:

Kind regards,
Joshua

1 Like

Hi @joshuacwnewton,

First of all, many thanks for making this problamatic and important matter very simple by your in depth explanation. Thanks for your patience once again. The dataset were aquiered long time ago using Bruker animal scanner. As you mentioned that you are right some data were oriented incorrectly while and before scanning. I’ve tried to swap the X Y Z using fslswapdim but was a bit confusing for me. Thanks for you and your team effort and developing such a great tools to do so i.e -setorient-data first which is very important.

Many thanks once again :slight_smile:

Kind regards,

Ibrahim

1 Like

Thank you for such kind words, @ihattan! I’m very glad that I was able to help. :heart:

I hope you don’t mind, but I have also updated the title of this post, just so that it is easier for others to find on the forum. :slight_smile:

Kind regards,
Joshua

2 Likes

Fantastic tool and appreciate the update many thanks to you and Spinal Cord Toolbox developers team :tada: