-
Notifications
You must be signed in to change notification settings - Fork 1
/
malfCommand.R
59 lines (43 loc) · 1.83 KB
/
malfCommand.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
library( ANTsR )
baseDirectory <- './'
atlasDirectory <- paste0( baseDirectory, 'Atlases/' )
labelsDirectory <- paste0( baseDirectory, 'Labels/' )
targetImage <- antsImageRead( paste0( baseDirectory, "T_template0.nii.gz" ), dimension = 2 )
targetMask <- antsImageClone( targetImage )
targetMask[targetMask == 0] <- 0
targetMask[targetMask != 0] <- 0
outputDirectory <- './OutputANTsR/'
if( ! dir.exists( outputDirectory ) )
{
dir.create( outputDirectory )
}
outputPrefix <- paste0( outputDirectory, 'antsr' )
atlases <- list()
labels <- list()
for( i in seq_len( 20 ) )
{
atlasFile <- paste0( atlasDirectory, "OASIS-TRT-20-", i, "_slice118.nii.gz" )
labelsFile <- paste0( labelsDirectory, "OASIS-TRT-20-", i, "_DKT31_CMA_labels_slice118.nii.gz" )
atlas <- antsImageRead( atlasFile, dimension = 2 )
label <- antsImageRead( labelsFile, dimension = 2 )
cat( "Registering", atlasFile, "to target image." )
registration <- antsRegistration( fixed = targetImage, moving = atlas,
typeofTransform = "SyN", verbose = TRUE )
atlases[[i]] <- registration$warpedmovout
labels[[i]] <- antsApplyTransforms( fixed = targetImage, moving = label,
transformlist <- registration$fwdtransforms, interpolator = "genericLabel" )
targetMask[labels[[i]] != 0] <- 1
}
# Plot the target image and mask
plot( targetImage, targetMask, color.overlay = "jet", alpha = 0.4 )
jlf <- jointLabelFusion( targetI = targetImage, targetIMask = targetMask,
atlasList <- atlases, labelList = labels, verbose = TRUE )
# Write out results
antsImageWrite( jlf$segmentation, paste0( outputPrefix, "jlfSegmentation.nii.gz" ) )
lgm <- labelGeometryMeasures( jlf$segmentation )
for( i in seq_len( length( lgm$Label ) ) )
{
id <- lgm$Label[i]
antsImageWrite( jlf$probabilityimages[[i]],
paste0( outputPrefix, "jlfProbabilityLabel", id, ".nii.gz" ) )
}