#!/usr/bin/perl # ------------------------------ MNI Header ---------------------------------- #@NAME : resample_labels #@INPUT : $InVolume - a label volume in some sampling grid and space #@OUTPUT : $OutVolume - a new label volume, with a different sampling # grid and/or space from $InVolume, but hopefully # with volume and shape nearly preserved #@RETURNS : #@DESCRIPTION: Resamples a label volume using the following algorithm: # * split multi-label volume into several binary volumes # * use trilinear interpolation to create a fuzzy label # volume from each # * apply a mask to restrict the labels so they only cover # voxels that fall within the thresholds used when painting # (the mask is determined from the MRI volume resampled # to the same target space as the label volume) # * threshold it (0.5? 0.4? 0.49?) # * recombine into a new multi-label volume #@METHOD : #@GLOBALS : #@CALLS : #@CREATED : January 1996, Greg Ward #@MODIFIED : Sept 1997, John Sled. Configuration for use with N3 release. #@VERSION : $Id: resample_labels.in,v 1.1 2003/04/16 14:29:34 bert Exp $ #----------------------------------------------------------------------------- use MNI::Startup; use MNI::Spawn; use MNI::FileUtilities qw(check_output_dirs); use MNI::PathUtilities qw(split_path); use MNI::MincUtilities qw(volume_params update_history); use MNI::MiscUtilities qw(lcompare); use Getopt::Tabular; # proposed usage example: # # resample_labels jacob_r_frontal.mnc jacob_r_frontal_rsl.mnc -mri_volume $jakob/resampled/linear/jakob_icbm_3898000_t1tal_lin.mnc -mri_thresholds 3.15e5 4.65e5 -resample "-start -1 -43 -32 -nelements 70 116 117" -labels_used 1-3,5-7,10 -label_threshold 0.49 # # BEGIN main program ########################################### $Usage = <= 6 && @_ <= 8; my ($in, $out, $interp, $type, $orient, $params, $transform, $invert) = @_; return if -e $out && !$clobber; Spawn ("mincresample $in $out $interp $type $orient $params " . ($transform ? "-transformation $transform " : "") . ($invert ? " -invert_transformation" : "")); } # ------------------------------ MNI Header ---------------------------------- #@NAME : &ApplyMask #@INPUT : #@OUTPUT : #@RETURNS : #@DESCRIPTION: Masks a volume. Pretty simple wrapper for mincmath, # so the mask and input volume must have identical sampling # grids. #@METHOD : #@GLOBALS : #@CALLS : #@CREATED : January 1996, Greg Ward #@MODIFIED : #----------------------------------------------------------------------------- sub ApplyMask { die "&ApplyMask: wrong number of arguments" unless @_ == 3; my ($in, $out, $mask) = @_; return if -e $out && !$clobber; &Spawn (['mincmath', '-mult', $mask, $in, $out]); } # ------------------------------ MNI Header ---------------------------------- #@NAME : &SegmentAndCombine #@INPUT : #@OUTPUT : #@RETURNS : #@DESCRIPTION: Basically the opposite of &ExtractLabel -- puts a binary # label volume back into a multi-colour volume, merging # it with whatever's already there. #@METHOD : #@GLOBALS : #@CALLS : #@CREATED : January 1996, Greg Ward #@MODIFIED : #----------------------------------------------------------------------------- sub SegmentAndCombine { die "&SegmentAndCombine: wrong number of arguments" unless @_ == 4; my ($in, $out, $inthresholds, $outlabel) = @_; &Spawn (['extracttag', $in, '-volume', $out, '-threshold', @$inthresholds, '-label', $outlabel, '-maxtags', 0, ((-e $out) ? ('-append', '-clobber') : ())]); }