ANTsR Notes: 7/28/15: Ants R for segmentation/cortical thickness
General comments: You will want to install and load ANTsR: library(ANTsR)
You will also want to do looping and error checking on your script.
I removed that from here for instructional purposes (i.e., makes this page more pithy).
Comments |
Command |
set name |
sub<-"VNS_01" |
load T1 |
t1<-antsImageRead(paste(sub,".nii.gz",sep=""),3) |
field correction |
t1c<-abpN4(t1) |
Comments |
#For optimal registration it is important to take out inhomogeneities in your brain |
Save results |
antsImageWrite(t1c,paste(sub,"_c.nii.gz",sep="")) |
get template brain with skull |
tem<-antsImageRead("/mnt/nfs2/users/alans/niiTemplates/NKI/TT_template.nii.gz") |
Comments |
#This is an important step/choice. Last week we went over how you make your own templates. |
get template brain without skull |
temmask<-antsImageRead("/mnt/nfs2/users/alans/niiTemplates/NKI/TT_template_BrainCerebellumMask.nii.gz") |
Comments |
#This is mask to handle brain extraction. The idea that you can cookie cutter the brain once you have it aligned. |
creates clean brain and segmented brain |
bm<-abpBrainExtraction(t1c,tem,temmask) |
Comments |
#A whole lot of stuff is done here - you get transformations (needed to skull strip) and skull stripping |
Transform |
bmbrain <- antsApplyTransforms(fixed=tem,moving=bm$brain,transformlist=bm$fwdtransforms ) |
Comments |
#This is the ANTs bit. This uses your transformations (created in the prior step) and you can apply them to your brain. |
Save results |
antsImageWrite(bmbrain,paste(sub,"_t1.nii.gz",sep="")) |
Save results |
file.copy(bm$fwdtransforms[1],paste(sub,"_ft.nii.gz",sep="")) |
Save results |
file.copy(bm$fwdtransforms[2],paste(sub,"_ft.mat",sep="")) |
Comments |
#You may want this information later (you can apply it to your functional data etc. |
Transform |
bmseg <- antsApplyTransforms(fixed=tem,moving=bm$kmeansseg,transformlist=bm$fwdtransforms ) |
Comments |
#Here we can save the simple kmeans segmentation for simple analysis but if we want priors we will forge on. |
Save results |
antsImageWrite(bmseg,paste(sub,"_kseg.nii.gz",sep="")) |
get nicer segmented brain info |
kseg<-kmeansSegmentation(bm$brain,3,bm$bmask) |
Comments |
#To get probability images for cortical thickness data. |
Transform |
bmseg <- antsApplyTransforms(fixed=tem,moving=kseg$segmentation,transformlist=bm$fwdtransforms ) |
Save results |
antsImageWrite(bmseg,paste(sub,"_Kseg.nii.gz",sep="")) |
Combine images for rf |
fseg<-lappend(t1c,kseg$probabilityimages) |
Clear some memory |
rm("t1","t1c","temmask") |
Do random forest |
rfsegs<-rfSegmentation(kseg$segmentation, fseg, verbose=TRUE,ntrees=50) |
|
#Random forest can be used to refine the classification this is most probably overkill |
Apply random forest |
rfsegsP<-rfSegmentationPredict( rfsegs$rfModel , fseg , bm$bmask ) |
|
#This is where the random forest is applied |
Save results |
rfseg <- antsApplyTransforms(fixed=tem,moving=rfsegs$segmentation,transformlist=bm$fwdtransforms ) |
Save results |
antsImageWrite(rfsegsP,paste(sub,"_rfseg.nii.gz",sep="")) |
get cortical thickness |
rfkthk<-kellyKapowski(s=rfsegs$segmentation,g=rfsegs$probabilityimages[[2]],w=rfsegs$probabilityimages[[3]],its=45,r=0.5,m=1) |
Comments |
#This is the cortical thickness calculator. |
Transform |
bmrfkthk <- antsApplyTransforms(fixed=tem,moving=rfkthk,transformlist=bm$fwdtransforms ) |
Save results |
antsImageWrite(bmkthk,paste(sub,"_thk.nii.gz",sep="")) |