Simple resample question - Half the dims

Hello!

I can’t figure out how to simply resample my images with 50% in each dimension!

I have a huge set of T1 images that I want to use in deep learning. To reduce the disk-usage we want to try and resample/shirk the matrix dims.

Example image:


R-to-L extent:  -108.364 [R] -to-    98.636 [L] -step-     1.000 mm [208 voxels]
A-to-P extent:  -123.597 [A] -to-   131.403 [P] -step-     1.000 mm [256 voxels]
I-to-S extent:   -91.698 [I] -to-   163.302 [S] -step-     1.000 mm [256 voxels]

Using zero-padding all the images have 208x256x256 voxels. The voxelsize is almost always 1.000 but some of them vary a little (e.g. 0.997 mm).

3dresample takes it’s options in mm format and this is no good since the mm might vary as I said. Is there a way to just resample the image from 208x256x256 to 104x128x128?

I tireid making a master dataset via:


3dcalc -a jRandomDataset:104,128,128 -expr a -prefix template_resam

Followed by:


3dresample -master template_resam+orig. -inset MNI152_2009_template_SSW.nii.gz -prefix test

To create a master 104,128,128 dataset to use in 3dresample. But while the matrix dims are correct the brain is way out of the frame.

Thanks!

Hi Robin,

There seem to be multiple things going on here. Such a simple thing is more complicated than it ought to be… :slight_smile:

First, and correct me if I am wrong, but you want the resample to keep the original values at the original locations, exactly, is that right? Moving off grid would allow interpolation and blurring, and I expect that you do not want that.

And notably, these datasets start with an even number of voxels. This somewhat breaks what you might want.

Imagine having a 4 voxel dataset at coords -1, 0, 1, 2, that you want to downsample to a 2 voxel dataset. Clearly, one of the endpoints must go. Either you would keep coords -1,1 or 0,2.

If there were an odd number of coordinates, 3dresample could do it (without a -master). So I suggest to first use 3dZeropad to add or remove a slice (in every direction), then use 3dresample to double the dimensions.

A second point is that if some dimensions are not exactly one, that is okay. But the result still must have exactly twice the voxel size of the original. Note that the ‘3dinfo’ extent lines do not give you the exact voxel dimensions, they are truncated to 3 decimal places. “3dAttribute DELTA DATASET” will output the more precise sizes (which sometimes cannot even be stored or shown exactly - we cannot store 2.1 exactly in binary, for example).

A third issue might come from running 3dresample. By default, that tries to preserve the FOV. But in this case (requiring odd dimensions), you want to preserve the exact voxel centers, but double the voxel sizes. That means using ‘-bound_type SLAB’, rather than the default ‘-bound_type FOV’ (which matches the afni GUI’s behavior). Using this on an odd dimension data set, along with -dxyz with exactly double the voxel dimensions, should downsample the dataset by a factor of 2 (with an extra point) in each direction.

This would preserve the original values.

if you start with datasets on the same grid, this would need to be done only once. If they are in orig space and on different grids, life gets harder.

A last issue is obliquity. If resampling seems to move the data (in the GUI), it is oblique. That is a discrepancy between the viewer and the real coordinates. The afni GUI does not want to alter the data values shown via interpolation, so it truncates to a cardinal grid. Any 3dresample step will throw away the obliquity matrix. What to do about this depends on exactly what you plan to do with this data.

Does this seem reasonable?

  • rick

Adding on to Rick’s resampling rhetoric…

The reason the MNI template is only partially displayed, is that the “random” dataset used as the master here is not in the same space as the MNI dataset. The grid doesn’t transform or align the data in any way, and the original xyz positions are simply interpolated onto that random dataset’s grid. The template_resam+orig and the test+orig output are marked as being in the +orig view and orig space

3dcalc -a jRandomDataset:104,128,128 -expr a -prefix template_resam
3dresample -master template_resam+orig. -inset MNI152_2009_template_SSW.nii.gz -prefix test

Resampling the template dataset to a specific grid can be done in the ways Rick suggested.

Thanks a lot Rick! (again)

We are OK with some interpolation/smoothing. We aim to throw 20k+ brains at a 3D CNN and want to see if we get similar results with smaller grids.
We ended up using 3dresample -dxyz 2.0 2.0 2.0. Since all volumes have 1x1x1 mm or values very close to this we expect everyone to end up with 104x128x128 grids. So far so good. I’ll let you know once it finishes =)