[3b2327]: / R / segment_lung.R

Download this file

148 lines (138 with data), 3.9 kB

  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
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#' @title Segment Lungs from CT scans
#' @description Segment Lungs from a non-contrast CT scan
#'
#' @param img Filename or \code{antsImage}
#' @param mask Should the image be masked
#' @param lthresh lower threshold for the image
#' @param verbose Print diagnostic messages
#' @param remove_background should background be removed first?
#'
#' @return List of image, lung, lung mask
#' @export
#' @importFrom extrantsr check_ants filler
#' @importFrom neurobase zero_pad maskEmptyImageDimensions mask_img
#' @importFrom oro.nifti voxdim
#' @importFrom stats median
#' @importFrom ANTsRCore resampleImage iMath smoothImage as.antsImage
#' @importFrom ANTsRCore antsImageClone
segment_lung = function(
img,
remove_background = FALSE,
mask = TRUE,
lthresh = -300,
verbose = TRUE
) {
if (verbose) {
message("# Checking Inputs")
}
reg_img = check_ants(img)
if (remove_background) {
background = iMath(img = reg_img <= -1024,
operation = "GetLargestComponent")
reg_img[background == 1] = -Inf
}
img = antsImageClone(reg_img)
vres = voxdim(reg_img)
if (verbose) {
message("# Resampling Image to 1x1x1")
}
reg_img = resampleImage(reg_img, c(1,1,1))
adder = 1025
res = segment_human(
img = reg_img,
adder = adder,
lthresh = -300,
verbose = TRUE
)
ebody = coarse_body(res$body)
ss = res$smoothed
ss = mask_img(ss, ebody)
# inds = getEmptyImageDimensions(body)
# ss = maskEmptyImageDimensions(
# img = ss,
# inds = inds,
# mask.value = 0)
# if (verbose) {
# message("# smoothing image")
# }
# ss = smoothImage(inimg = ss, sigma = 10,
# max_kernel_width = 200)
# # smoothed image is greater than some thresh
# if (mask) {
# vals = as.numeric(ss)
# } else {
# vals = as.numeric(ss)
# vals = vals[ as.numeric(body) > 0]
# }
# med = median(vals)
# body = ss > med
#
# # body = ss > (-100 + adder)
# if (verbose) {
# message("# Getting Humans")
# }
#
# # zero padding in case for any connectedness
# zp = as.array(body)
# kdim = c(1,1,1)
# zp = zero_pad(zp, kdim = kdim)
# zp = as.antsImage(zp, reference = body)
# # getting connected component
# cc = iMath(img = zp, operation = "GetLargestComponent")
# rm(list = "zp"); gc(); gc()
# cc = as.array(cc)
# cc = zero_pad(cc, kdim = kdim, invert = TRUE)
# cc = as.antsImage(cc, reference = body)
#
# if (verbose) {
# message("# Filling Holes")
# }
# # cc = iMath(img = cc, operation = "FillHoles")
# cc = filler(cc, fill_size = 60)
# # cc = filler(cc, fill_size = 40)
# cc = filler(cc, fill_size = 5, dilate = FALSE)
#
# # Dropping non-human stuff
# inds = getEmptyImageDimensions(cc)
#
# if (verbose) {
# message("# Making New image")
# }
#
# # Don't want to drop the indices,
# # just blank them out
# newimg = maskEmptyImageDimensions(img = reg_img, inds = inds)
if (verbose) {
message("# Getting Lungs: Thresholding")
}
newimg = ss
cc = ebody
lung = newimg < (lthresh + adder) & newimg > 0 & cc == 1
if (verbose) {
message("# Getting Lungs: Largest Component")
}
lung = iMath(img = lung, operation = "GetLargestComponent")
# lung = iMath(img = lung, operation = "FillHoles")
if (verbose) {
message("# Getting Lungs: Filling Holes")
}
lung = filler(lung, fill_size = 2)
if (verbose) {
message("# Resampling Back to Original Image Size")
}
lung_mask = resampleImage(lung,
resampleParams = dim(img),
useVoxels = TRUE,
interpType = 1)
# lung_mask = resampleImageToTarget(
# lung, target = img,
# interpType = "nearestNeighbor",
# verbose = verbose)
lung = mask_img(img, lung_mask)
# reg_img = reg_img - adder
L = list(img = img,
lung_mask = lung_mask,
lung = lung
)
return(L)
}