Hello, everyone. I have a question for you.
I have two nii images with identical headers (both with time point 120), I want to put image 1 on the singular time point and image 2 on the even time point to get the image 120*2 time point, how do I do it? AFNI’s 3dUpsample can be upsampled, but the data after upsampling has been filled with data, which bothers me very much
Just to make sure I understand, you want to interleave the two 4D niftis in the time dimension, correct?
So one voxel’s time series in the combined image would be like [T1,1 T2,1 T1,2 T2,2] where T1,1 is image 1, volume 1, T2,1 is image 2, volume 1, etc.? That’s not really upsampling, as far as I can tell, but it’s definitely doable.
If so, then you could do the following:
import nibabel as nb
import numpy as np
img1 = nb.load("file1.nii.gz")
img2 = nb.load("file2.nii.gz")
arr1 = img1.get_fdata()
arr2 = img2.get_fdata()
out_arr = np.zeros(
(arr1.shape[0], arr1.shape[1], arr1.shape[2], arr1.shape[3] * 2),
dtype=arr1.dtype,
)
out_arr[..., ::2] = arr1
out_arr[..., 1::2] = arr2
out_img = nb.Nifti1Image(out_arr, img1.affine, img2.header)
out_img.to_filename("out_file.nii.gz")
If @tsalo is right about what you want to do, there are a few ways in AFNI to mix odds and even from two datasets (either AFNI or NIFTI formats).
# use upsample to double the number of data points
# the in-between points are just linearly interpolated, but we ignore those in the output
3dUpsample -prefix testepi_r1_us -1 2 epi_r1+orig.
3dUpsample -prefix testepi_r2_us -1 2 epi_r2+orig.
# take the evens (0,2,4,...) from the first dataset
# and the odds from the second dataset
# (offset backwards by 1 index in the double-length series)
3dcalc -a testepi_r1_us+orig. -b testepi_r2_us+orig. -c 'b-l' \
-expr 'c*step(mod(l,2))+a*not(mod(l,2))' \
-prefix test_epi_merge
There have been a few related posts on odds and evens volumes that might be useful, like the one here: