kmeansSegmentation.Rd
k means image segmentation that is a wrapper around atropos
kmeansSegmentation(img, k, kmask = NULL, mrf = 0.1, verbose = FALSE, ...)
input image
integer number of classes
segment inside this mask
smoothness, higher is smoother
boolean
additional arguments to pass to atropos
segmentation and probability images
This function will likely give different results on multiple runs.
fi <- antsImageRead(getANTsRData("r16"), 2)
orig <- antsImageClone(fi)
fi <- n3BiasFieldCorrection(fi, 4)
seg <- kmeansSegmentation(fi, 3)
seg2 <- kmeansSegmentation(fi, 3)
arr1 <- as.array(seg$segmentation)
arr2 <- as.array(seg2$segmentation)
testthat::expect_equal(arr1, arr2)
set.seed(2)
# set below for slower but numerically repeatable results
# these should be set in .Renviron not by sys calls
# Sys.setenv(ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS = 1 )
orig <- antsImageRead(getANTsRData("r16"), 2)
seg <- kmeansSegmentation(orig, 3, use_random_seed = TRUE)
seg2 <- kmeansSegmentation(orig, 3, use_random_seed = TRUE)
arr1 <- as.array(seg$segmentation)
arr2 <- as.array(seg2$segmentation)
tab <- table(arr1, arr2)
tab
#> arr2
#> arr1 0 1 2 3
#> 0 47118 0 0 0
#> 1 0 2383 0 0
#> 2 0 0 7359 0
#> 3 0 0 1 8675
identical(arr1, arr2)
#> [1] FALSE
seg <- kmeansSegmentation(orig, 3, use_random_seed = FALSE)
seg2 <- kmeansSegmentation(orig, 3, use_random_seed = FALSE)
arr1 <- as.array(seg$segmentation)
arr2 <- as.array(seg2$segmentation)
tab <- table(arr1, arr2)
tab
#> arr2
#> arr1 0 1 2 3
#> 0 47118 0 0 0
#> 1 0 2385 0 0
#> 2 0 0 7353 0
#> 3 0 0 0 8680