Downsampling

Hello AFNI group,

I want to downsample my dataset in the Z direction by a factor of 3. More precisely I want to average 3 slices together.
I realized (I think) that 3dresample would not work to achieve that averaging whether I use NN or Li. I am not sure what Blocky exactly do.
However, it seems to me that I could do it with 3dLocStat:
3dLocalstat -nbhd 'RECT(0,0,-1)' -mask mask.nii.gz -stat mean -reduce_grid 1 1 3 -prefix data_lowres.nii.gz data_hires.nii.gz

What do you think? Is that correct? Is that possible to do that using 3dresample?

Thank you for you

Julien

Hi Julien,

Yes, 3dLocalstat seems like an appropriate way to get the averaging that you want. It does not downsample the dataset, but it will get the average.

And yes, you could use 3dresample afterwards to do the actual downsampling, getting every third voxel. Consider:

3dresample -downsample 3 -prefix new.down3.nii -input old.nii

-rick

Thank you Rick for the reply.

It looks like it is possible to do the actual downsampling after averaging all within 3dLocstat by using the option -reduce_grid 1 1 3.
I think it worked as the resulting dim is correct.
original dim:
R-to-L extent: -79.101 [R] -to- 87.028 [L] -step- 1.613 mm [104 voxels]

A-to-P extent: -110.751 [A] -to- 87.636 [P] -step- 1.613 mm [124 voxels]

I-to-S extent: -68.459 [I] -to- 54.741 [S] -step- 1.600 mm [ 78 voxels]

Resulting dim:

R-to-L extent: -79.101 [R] -to- 87.028 [L] -step- 1.613 mm [104 voxels]

A-to-P extent: -110.751 [A] -to- 87.636 [P] -step- 1.613 mm [124 voxels]

I-to-S extent: -66.859 [I] -to- 53.141 [S] -step- 4.800 mm [ 26 voxels]

I am not sure if that is working quite the way you would want, in that the grid reduction will be an interpolated one (in the z-direction). But to get an accurate mean, run 3dLocalstat first followed by the 3dresample version that I suggested (that is a fairly new method, many years newer than 3dLocalstat - we might want to consider updating the method in 3dLocalstat).

You could then compare the results.

How precisely do you want that mean computed? Maybe you won't mind the extra resampling blur...

-rick

Thank you for your reply.

So, I compared both methods and they gives me exactly the same results, bit to bit.
I also checked with the cursor a few voxels and it is the average of 3 voxels in the z direction.

Method 1:
3dLocalstat -nbhd 'RECT(0,0,-1)' -mask CBF_s.nii.gz -stat mean -reduce_grid 1 1 3 -prefix CBF_s_loc.nii.gz CBF_s.nii.gz

Method 2:
3dLocalstat -nbhd 'RECT(0,0,-1)' -mask CBF_s.nii.gz -stat mean -prefix CBF_s_tmp.nii.gz CBF_s.nii.gz
3dresample -dxyz 1.613 1.613 4.8 -rmode NN -prefix CBF_s_loc+resamp.nii.gz -input CBF_s_tmp.nii.gz

And they have the same FOV, vox size ...

Just to be clear, "3dresample -dxyz ..." is not quit what I recommend. Indeed, that ought to give the same result. Using "3dresample -downsample 3" might be more precisely what you want. But if you are happy with this, it is great.

Thanks,

-rick

Thank you Rick.
PS: I did not use the downsample option as it is applying FAC along x, y, z and I just wanted along z.

OMG, I forgot about that aspect. Thanks!

-rick