Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support array of outDim and outMM #31

Open
hanayik opened this issue Sep 5, 2024 · 3 comments
Open

support array of outDim and outMM #31

hanayik opened this issue Sep 5, 2024 · 3 comments

Comments

@hanayik
Copy link
Collaborator

hanayik commented Sep 5, 2024

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
Copy link
Contributor

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:

  • Following FreeSurfer convention, conform saves sagittal slices (LIA) while comply saves as axial slices (RAS).
  • The niimath conform function uses a f_high of 0.98, while FreeSurfer and FastSurfer use 0.999. In my experience, the bright flow artifacts in 7T T1w images can lead FreeSurfer to have insufficient dynamic range.
  • Both conform and comply scale data to the range 0..255, but not that following fslmaths convention output defaults to the float32 datatype. Use -odt char if you want to force the UInt8 datatype.
  • Note you can set the high clamp but not the low clamping. For Magnitude-base MRI data, this is sensible. For CT data you should consider clamping dark voxels (-add 800 -thr 0) before running comply.

neurolabusc added a commit that referenced this issue Sep 8, 2024
@hanayik
Copy link
Collaborator Author

hanayik commented Sep 11, 2024

@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
Copy link
Contributor

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants