[3b2327]: / R / quick_lung_mask.R

Download this file

19 lines (16 with data), 609 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#' @rdname segment_lung
#' @export
#' @importFrom ANTsR connectedThreshold maskImage
#' @importFrom ANTsRCore iMath
quick_lung_mask = function(img,
lthresh = -300) {
img = check_ants(img)
# just get rid of the standard background just in case
bg_mask = iMath(img < -950, "GetLargestComponent")
mask = maskImage(img > -1023 & img < -900, 1 - bg_mask)
ind = which(as.array(mask) > 0, arr.ind = TRUE)
seed = floor(colMeans(ind))
d = colMeans((t(ind) - seed)^2)
seed = ind[which.min(d),]
simg = connectedThreshold(img, seed = seed, upper = lthresh, lower = -1023)
}