rordenlab / niimath

niimath - a clone of fslmaths. Try the live demo:
https://niivue.github.io/niivue-niimath/
Other
55 stars 16 forks source link

support array of outDim and outMM #31

Open hanayik opened 2 months ago

hanayik commented 2 months ago

supporting the nibabel conform functionality rather than the limited Freesurfer implementation could have benefits.

Specifically, the ability to supply “outDim” as an array and “outMM” as an array rather than an int and a float.

https://nipy.org/nibabel/reference/nibabel.processing.html#nibabel.processing.conform

This would allow conforming images for any arbitrary dimension specification and voxel sizes that a ML model may expect, rather than the 256x256x256 and 1mm isotropic options currently supported. This would also allow niimath WASM to serve as a more general purpose helper module to prepare images for arbitrary model inference on the "edge" in the browser.

neurolabusc commented 2 months ago

Latest commit has a new feature comply which is the conform function with more options than FreeSurfer/FasterSurfer.

The help for this feature is:

 -comply <nx> <ny> <nz> <dx> <dy> <dz> <f_high> <isLinear> : conform to axial slice with dx*dy*dzmm size and dx*dy*dz voxels. f_high bright clamping (0.98 for top 2%). Linear (1) or nearest-neighbor (0)

Therefore, usage would look like this:

$ niimath T1 -comply 256 128 64 1.1 2.2 2.3 0.98 1 c2
Comply dim 256*128*64, pixdim 1.1*2.2*2.3, f_high 0.98 linear 1
conform input:    min: 0.000000  max: 1588.000000
Rescale:  min: 0.000000  max: 514.512024  scale: 0.495615
src % fslhd c2                                           
...
dim1        256
dim2        128
dim3        64
...
pixdim0     1.000000
pixdim1     1.100000
pixdim2     2.200000
pixdim3     2.300000
...
sform_code  1
sto_xyz:1   1.100000 0.000000 0.000000 -134.376007 
sto_xyz:2   0.000000 2.200000 0.000000 -110.706009 
sto_xyz:3   0.000000 0.000000 2.300000 -86.375999 
sto_xyz:4   0.000000 0.000000 0.000000 1.000000 
sform_xorient   Left-to-Right
sform_yorient   Posterior-to-Anterior
sform_zorient   Inferior-to-Superior

A couple of comments:

hanayik commented 2 months ago

@neurolabusc , this seems to work nicely. My only comment is that the nibabel conform implementation does not rescale the voxel intensity range. Do you think it would be useful to make voxelIntensityScale in niimath optional by using a flag when using the conform or comply niimath arguments? The default could be true so the current behaviour is unchanged.

From the nibabel docs:

Unlike mri_convert --conform, this command does not: Transform data to range [0, 255] Cast to unsigned eight-bit integer

neurolabusc commented 2 months ago

@hanayik as per your suggestion the intensity scaling (from 0..255) is only applied if the f_high is greater than 0.0. This is useful for many cases, including CT scans where the voxel intensity is calibrated in Hounsfield units.

niimath T1 -comply 256 128 64 1.1 2.2 2.3 0.0 1 c2