Child pages
  • From MINC (4D) to Matlab and back
Skip to end of metadata
Go to start of metadata

If you've ever tried to get your MINC files loaded in Matlab, especially if your files are 4D files (say a deformation field), you might have realized that it's not trivial. One easy way to do is, is to avoid trying to load 4D files altogether and instead extract the vector components, load those into Matlab separately, save those after you've  dealt with them, and stack them together into a 4D MINC file afterwards.

NRRD reader and writer

Here are the files that can be used to read nrrd files and to subsequently write nrrd files. It's important to use the files that are attached to this page, because the original files perform a permutation on the input matrix which seems to throw off the conversion back to MINC.

Teasing your 4D MINC file apart

# in order to collapse the 4th dimension onto the 3rd dimension, the count for the dimrange parameter should be set to zero:
mincreshape -dimrange vector_dimension=0,0 4D_displacement.mnc x_component_of_displacement.mnc
mincreshape -dimrange vector_dimension=1,0 4D_displacement.mnc y_component_of_displacement.mnc
mincreshape -dimrange vector_dimension=2,0 4D_displacement.mnc z_component_of_displacement.mnc

# get the MINC info; we need this information later on when writing out the nrrd data from Matlab:
mincinfo z_component_of_displacement.mnc
file: z_component_of_displacement.mnc
image: signed__ float -0.69851291179656982422 to 0.66476130485534667969
image dimensions: xspace yspace zspace
 dimension name length  step  start
 -------------- ------  ----  -----
 xspace          225   0.056  -6.27
 yspace          320   0.056  -8.19
 zspace          152   0.056   -4.2

# convert these files to nrrd using itk_convert:
itk_convert x_component_of_displacement.mnc x_component_of_displacement.nrrd
itk_convert y_component_of_displacement.mnc y_component_of_displacement.nrrd
itk_convert z_component_of_displacement.mnc z_component_of_displacement.nrrd

Read data into Matlab

# read the input file and its meta data
[inputdata_x, inputmeta_x] = nrrdread('x_component_of_displacement.nrrd');
[inputdata_y, inputmeta_y] = nrrdread('y_component_of_displacement.nrrd');
[inputdata_z, inputmeta_z] = nrrdread('z_component_of_displacement.nrrd');

# ... data manipulation ...

# write out data (the image spacing is unfortunately not directly returned by the meta data, nor is the origin information so get it from the mincinfo):
nrrdWriter('x_component_of_displacement_Matlab_out.nrrd', inputdata_x, [0.056,0.056,0.056], [-6.27,-8.19,-4.2], inputmeta.encoding);
nrrdWriter('y_component_of_displacement_Matlab_out.nrrd', inputdata_y, [0.056,0.056,0.056], [-6.27,-8.19,-4.2], inputmeta.encoding);
nrrdWriter('z_component_of_displacement_Matlab_out.nrrd', inputdata_z, [0.056,0.056,0.056], [-6.27,-8.19,-4.2], inputmeta.encoding);

Stacking the vector components back together (MINC)

# convert the nrrd back to MINC:
itk_convert x_component_of_displacement_Matlab_out.nrrd x_component_of_displacement_Matlab_out.mnc
itk_convert y_component_of_displacement_Matlab_out.nrrd y_component_of_displacement_Matlab_out.mnc
itk_convert z_component_of_displacement_Matlab_out.nrrd z_component_of_displacement_Matlab_out.mnc

# concatenate the components into a vector field:
mincconcat -concat_dimension vector_dimension x_component_of_displacement_Matlab_out.mnc y_component_of_displacement_Matlab_out.mnc z_component_of_displacement_Matlab_out.mnc 4D_vector_field_from_Matlab.mnc
  • No labels