Read and write MINC2 files from Julia, reading and writing .xfm transformation files, Manipulations with volumes
Minc command:
> mincstats -mean mni_icbm152_t1_tal_nlin_sym_09c.mnc
Mean: 29.61005195
Julia version:
using Minc2
using StatsBase
# read volume from minc file, represent it as array of doubles (Float64 in julia world)
icbm=Minc2.read_volume("mni_icbm152_t1_tal_nlin_sym_09c.mnc", store=Float64)
@show mean(Minc2.array(icbm))
Output mean(Minc2.array(icbm)) = 29.61005194874031
using Minc2
using StatsBase
# read T1w scan
icbm=Minc2.read_volume("mni_icbm152_t1_tal_nlin_sym_09c.mnc", store=Float64)
# read label mask, represent it as array of bytes
lab=Minc2.read_volume("mni_icbm152_cls_tal_nlin_sym_09c.mnc", store=UInt8)
# calculate image statistics per label
for (i,l) in [1=>"CSF",2=>"GM",3=>"WM"]
println("$(l):$(mean(Minc2.array(icbm)[ Minc2.array(lab).==i ]))")
end
Output:
CSF:31.263195070792978
GM:64.88399427312729
WM:84.74593912912998
Create transformation:param2xfm -rotations 30 0 0 rotate.xfm
using Minc2
# read T1w scan
icbm=Minc2.read_volume("mni_icbm152_t1_tal_nlin_sym_09c.mnc", store=Float64)
# read transformation
tfm=Minc2.load_transforms("rotate.xfm")
# apply transformation to the volume
transformed_icbm=Minc2.resample_volume(icbm; tfm, order=2, fill=0.0)
# write the resulting volume into file, append history
# the resulting volume will be stored with unsigned short minc data type
# but will preserve floating point vales used fixe-point arithmetics
Minc2.save_volume("transformed_icbm.mnc",transformed_icbm, store=UInt16, history="Julia example")
using Minc2
# read ROI labels
rois = Minc2.read_volume("rois.mnc", store=UInt8)
# calculate voxel volume
start, step, dir_cos = Minc2.decompose(Minc2.voxel_to_world(rois))
voxel_volume = abs(reduce(*,step,init=1))
# read transformation
tfm = Minc2.load_transforms("nonlinear.xfm")
# define sampling parameters for jacobian field
jacobians = Minc2.empty_volume_like(lab, store=Float64)
# calculate per-voxel jacobians
Minc2.calculate_jacobian!(tfm, jacobians)
# Integrate per ROI
for (i,l) in [1=>"ROI-1",2=>"ROI-2",3=>"ROI-3"]
println("$(l):$( sum((Minc2.array(rois) .== i) .* Minc2.array(jac) )*voxel_volume)")
end
using CairoMakie
using Minc2
# read T1w scan
icbm=Minc2.read_volume("mni_icbm152_t1_tal_nlin_sym_09c.mnc", store=Float64)
# read label mask, represent it as array of bytes
lab=Minc2.read_volume("mni_icbm152_cls_tal_nlin_sym_09c.mnc", store=UInt8)
fig = Figure()
gc = fig[1, 1] = GridLayout()
Minc2.draw_outline_with_labels(gc, icbm, lab,
labels=Dict([1=>"CSF",2=>"GM",3=>"WM"]),
nslices = 5)
resize_to_layout!(fig)
save("mni_icbm152_segmentation.png", fig, px_per_unit = 1)
Will produce
using CairoMakie
using Minc2
# read T1w scan
anat=Minc2.read_volume("mni_icbm152_t1_tal_nlin_sym_09c.mnc",store=Float64)
# read field
gm=Minc2.read_volume("mni_icbm152_gm_tal_nlin_sym_09c.mnc",store=Float64)
fig = Figure()
gc = fig[1, 1] = GridLayout()
# set background to transparent
Minc2.array(gm)[Minc2.array(gm) .< 1e-6] .= NaN
Minc2.draw_outline_with_heatmap(gc, anat, gm,
heat_limits=(0.0,1.0),cmap=:turbo,
nslices = 5)
resize_to_layout!(fig)
save("mni_icbm152_gm.png", fig, px_per_unit = 1)
See examples
directory for more examples:
bloor.jl
- Apply Blooring kernel to minc volume, equivalent to mincbloorcopy.jl
- Create a minc volume with the same dimensions as another one, copy voxelsdraw_heatmap.jl
- Show MNI-ICBM152 template contours with GM proability map, as abovedraw_seg.jl
- Show MNI-ICBM152 template contours with tissue masks overlays, as abovelist_meta.jl
- List all metadata stored with minc file, similar tomincheader
mriview_dual.jl
- Show two minc volumes side-by-side, interactivelymriview.jl
- Show minc volume, interactivelymultilabel_overlap.jl
- calculate voxel-wise generalized overlap coeffecient , method from http://dx.doi.org/10.1109/TMI.2006.880587resample_grid.jl
- apply transformation to _grid fileresample_itk.jl
- resample nifti file with transformations produces by ANTS (multiple transformations can be applied in one go), equivalent toantsApplyTransforms
resample.jl
- apply transformation to minc volume, equivalent tomincresample
xfm_to_jacobian.jl
- calculate jacobian determinant field, for a given .xfm filemincinfo.jl
- show information about mincfile(s) and store it in csv file, equivalent tomincinfo
Preliminary documentation is available at https://www.ilmarin.info/Minc2.jl/
Documentations for underlying libminc and minc tools is available at https://en.wikibooks.org/wiki/MINC