[Brains-users] Script for clipping the brain and making all data a
specified size reguardless of input image size.
Hans Johnson
hans-johnson at uiowa.edu
Wed Jun 1 09:31:23 PDT 2005
Clipping Brain Images and Filling Background
The following is a script for clipping a brain from the skull and filling
the background of an image with "CSF" values. It is used for high
dimensional warping.
to be used via brains2 -b scriptname
set wdir [ pwd ]
set acpcdir [ getACPCdir $wdir ]
set mrqid [ getScanIdFromACPCdir $acpcdir ]
set suffix "segment_BOBF.hdr"
set aliases_filename "temp_aliases"
set closeoperator_mm 3
set thresholdvalue 70
set basedirname /raid1/data/methods/warp/BOBF_IPL_PET_MAPPING_I1_L1
set outfilename ${basedirname}/images/${mrqid}_${suffix}
set outfilename ${wdir}/${mrqid}_${suffix}
set filename ${basedirname}/data/$aliases_filename
set fileptr [ open ${filename} a ]
puts ${fileptr} "S${mrqid} ${outfilename}"
close ${fileptr}
if { [ file isfile $outfilename ] } {
puts "File exists ($outfilename), exiting"
exit 0
} else {
set segPath [ getSegmentImageName $acpcdir ]
#set t1Path [ getResampledT1ImageName $acpcdir ]
set brainPath [ getBrainMaskName $acpcdir ]
if { [ file isfile $segPath ] && [ file isfile $brainPath ] } {
puts "Processing files segmentedImage $segPath brainMask
$brainPath"
set origseg [ b2 load image $segPath ]
set origdims [ b2 get dims image $origseg ]
set origres [ b2 get res image $origseg ]
set newdims $origdims
set newres $origres
set tnsfm [ b2 create affine-transform $newdims $newres $origdims
$origres ]
# This produced an identity affine transform. Note: to get
# an inter-spatial identity transform, use the dims and res
# you want to end up with for newdims and newres.
if {$tnsfm == -1} {
puts "Script halting: 'b2 create affine-transform' couldn't
handle dims list '$origdims' and res list '$origres'."
exit 1
}
b2 set transform $tnsfm image $origseg
set seg [ b2 resample image $origseg plane= coronal ]
set origbrain [ b2 load mask $brainPath ]
set brain [ b2 resample mask forward $origbrain $tnsfm ]
b2 destroy image $origseg
b2 destroy mask $origbrain
set imageres 1.015625
set closeoperator [ expr ${closeoperator_mm} / ${imageres} ]
set closeoperator_max [ expr ${closeoperator} * 3.0 ]
puts "close operator sizes $closeoperator ${closeoperator_max}"
#set t1 [ b2 load image $t1Path ]
set brainmask [ b2 erode mask $brain $closeoperator_max ]
set thresholdmask [ b2 threshold image $seg $thresholdvalue ]
set tbmask [ b2 and masks $brain $thresholdmask ]
set errodemask [ b2 erode mask $tbmask $closeoperator ]
set dilatemask [ b2 dilate mask $errodemask $closeoperator ]
set filledmask [ b2 or masks $dilatemask $brainmask ]
set notmask [ b2 not mask $filledmask ]
set backgroundimage [ b2 sum masks [ list $notmask $notmask ] ]
set clbgimage [ b2 clip image $backgroundimage $notmask ]
set csfbgimage [ b2 multiply images $clbgimage scale= [expr
${thresholdvalue}/2 ] data-type= unsigned-8bit ]
#set cut [ b2 clip image $t1 $filledmask ]
set cut [ b2 clip image $seg $filledmask ]
b2 hide every mask
b2 destroy image $brain
b2 destroy image $backgroundimage
b2 destroy image $clbgimage
set finalbigimage [ b2 sum images [ list $cut $csfbgimage ] ]
b2 destroy image $csfbgimage
b2 destroy image $cut
set finalimage $finalbigimage
b2 save image ${outfilename} strictAnalyze75 $finalimage
data-type= unsigned-8bit plane= coronal
} else {
puts "ERROR ERROR ERROR: Missing necessary files segmentedImage
$segPath brainMask $brainPath from $wdir"
exit -1
}
}
b2 exit
--Hans 14:15, 26 Oct 2004 (CDT)
More information about the Brains-users
mailing list