0

I am trying to train a model where one set of data contain a particular pixDim, whereas another set contains a different pixDim. I want to normalize both the voxel resolution and execute.

Can we change the pixDim dimension of a volumetric data like .nifti.gz or .mgz file using nibabel or any other python library?

For reference, I am talking about pixDim in the header of a volumetric file highlighted in the below image.

enter image description here

Jyotirmay
  • 1,533
  • 3
  • 21
  • 41

1 Answers1

0

The preferable way is to calculate target pixdim or use scipy interpolation method as in below function to achieve target pixdim or steps(in func)

import scipy.interpolate as si
def do_interpolate(values, steps, isLabel=False):
    x, y, z = [steps[k] * np.arange(values.shape[k]) for k in range(3)]  # original grid
    if isLabel:
        method = 'nearest'
    else:
        method = 'linear'

    f = si.RegularGridInterpolator((x, y, z), values, method=method)  # interpolator

    dx, dy, dz = 2.0, 2.0, 3.0  # new step sizes # settings['EVAL']['target_voxel_dimension']
    new_grid = np.mgrid[0:x[-1]:dx, 0:y[-1]:dy, 0:z[-1]:dz]  # new grid
    new_grid = np.moveaxis(new_grid, (0, 1, 2, 3), (3, 0, 1, 2))  # reorder axes for evaluation
    return f(new_grid)

You will get an updated upsampled or downsampled resolution for your volume data with target pixdim maintained.

NOTE: In the above function, values hold 3d volumetric data, steps hold original pixdim data, I have hardcoded target pixdim in the form of dx, dy, dz.

Jyotirmay
  • 1,533
  • 3
  • 21
  • 41