Diff of /R/ear_mask.R [000000] .. [242173]

Switch to unified view

a b/R/ear_mask.R
1
#' @rdname ear_mask
2
#' @title Create Mask of the Ears
3
#' @aliases ct_ear_mask,mri_ear_mask
4
#' @description Creates a rough mask of the ear from a head scan
5
#'
6
#' @param file File for face masking - either filename or class nifti
7
#' @param mask file or \code{nifti} to mask the \code{file}
8
#' @param robust If \code{mask = NULL}, then \code{robust} is
9
#' passed to \code{\link{CT_Skull_Stripper}}
10
#' @param template.file Template to warp to original image space
11
#' @param template.ear_mask Mask of template to use as rough ear mask.  If
12
#' \code{template.file} is not specified, \code{template.left_ear_inds} and
13
#' \code{template.right_ear_inds}
14
#' must be
15
#' @param template.left_ear_inds List of length 3 for indices of
16
#' \code{template.file} to indicate the left-ear mask.
17
#' @param template.right_ear_inds List of length 3 for indices of
18
#' \code{template.file} to indicate the right-ear mask.
19
#' @param extend_left after transformation, should the mask be extended to the
20
#' left of the image to ensure all left ear has been removed?
21
#' @param extend_right after transformation, should the mask be extended to the
22
#' right of the image to ensure all right ear has been removed?
23
#' @param typeofTransform Transformation for template to image, passed to
24
#' \code{\link{ants_regwrite}}.
25
#' @param swapdim Should the dimensions be swapped before registration,
26
#' and then reset after
27
#' @param skull_strip Should the data require skull stripping if
28
#' no mask is provided?
29
#' @param verbose Print out diagnostic messages
30
#' @param ... arguments passed to \code{\link{CT_Skull_Stripper}}
31
#' @export
32
#' @return Object of class nifti
33
#' @examples \dontrun{
34
#' file = "~/Desktop/Desktop/scratch/100-318_20070723_0957_CT_3_CT_Head-.nii.gz"
35
#' mask = NULL
36
#' robust = FALSE
37
#' ears = ct_ear_mask(
38
#'    file = file,
39
#'    robust = FALSE
40
#'  )
41
#'  img = readnii(file)
42
#'  rimg = randomize_mask(img, mask = ears)
43
#' }
44
ct_ear_mask = function(
45
  file,
46
  skull_strip = TRUE,
47
  mask = NULL,
48
  robust = TRUE,
49
  template.file =
50
    system.file(
51
      "scct_unsmooth_SS_0.01.nii.gz",
52
      package = "ichseg"),
53
  template.ear_mask = NULL,
54
  template.left_ear_inds = list(170:180, 60:110, 1:60),
55
  template.right_ear_inds = list(1:10, 60:110, 1:60),
56
  extend_left = TRUE,
57
  extend_right = TRUE,
58
  typeofTransform = "Affine",
59
  swapdim = TRUE,
60
  verbose = TRUE,
61
  ...){
62
63
  if (skull_strip) {
64
    mask = .make_ss_mask(file = file,
65
                         mask = mask,
66
                         verbose = verbose,
67
                         robust = robust,
68
                         template.file = template.file, ...)
69
  }
70
71
  template.left_ear_mask = .make_template_mask(
72
    template.file = template.file,
73
    template.mask = template.ear_mask,
74
    template.inds = template.left_ear_inds)
75
  template.right_ear_mask = .make_template_mask(
76
    template.file = template.file,
77
    template.mask = template.ear_mask,
78
    template.inds = template.right_ear_inds)
79
80
  template.ear_mask = template.left_ear_mask | template.right_ear_mask
81
82
  L = .mask_reg(file = file,
83
                mask = mask,
84
                verbose = verbose,
85
                swapdim = swapdim,
86
                template.file = template.file,
87
                typeofTransform = typeofTransform,
88
                template.mask = template.ear_mask)
89
90
  mask_trans = L$mask_trans
91
  img = L$img
92
93
94
  ######################################
95
  # Applying the mask to the image
96
  ######################################
97
  mask_trans = mask_trans > 0.5
98
  any_in_mask = any(mask_trans)
99
  ind = which(mask_trans, arr.ind = TRUE)
100
  dimg = dim(img)
101
  xdim = dimg[1]
102
  midx = xdim/2
103
  if (extend_left || extend_right) {
104
    if (any_in_mask) {
105
      df = data.frame(ind)
106
      df$left = c("left", "right")[ (df$dim1 > midx) + 1]
107
      ss = split(df, df$left)
108
      ss = lapply(ss, function(x) {
109
        x$left = NULL
110
        x
111
      })
112
      inds = NULL
113
      ind = ss$left
114
      if (!is.null(ind) && nrow(ind) > 0) {
115
        xs = seq(1, max(ind$dim1)) # for left
116
        ys = unique(ind$dim2)
117
        zs = unique(ind$dim3)
118
        run_inds = expand.grid(xs, ys, zs)
119
        run_inds = as.matrix(run_inds)
120
      }
121
      inds = rbind(inds, run_inds)
122
123
      ind = ss$right
124
      if (!is.null(ind) && nrow(ind) > 0) {
125
        xs = seq(min(ind$dim1), xdim) # for right
126
        ys = unique(ind$dim2)
127
        zs = unique(ind$dim3)
128
        run_inds = expand.grid(xs, ys, zs)
129
        run_inds = as.matrix(run_inds)
130
      }
131
      inds = rbind(inds, run_inds)
132
133
      newimg = niftiarr(img, 0)
134
      newimg[inds] = 1
135
      newimg = cal_img(newimg)
136
    } else {
137
      warning("No registered object in mask found - cannot extend!")
138
      newimg = mask_trans
139
    }
140
  } else {
141
    newimg = mask_trans
142
  }
143
144
145
  if (swapdim) {
146
    if (verbose) {
147
      message(paste0("# Swapping Dimensions Back\n"))
148
    }
149
    sorient = L$sorient
150
    ori = L$ori
151
    newimg = reverse_rpi_orient(
152
      file = newimg,
153
      convention = ori,
154
      orientation = sorient,
155
      verbose = verbose)
156
  }
157
  return(newimg)
158
}
159
160
161
162
#' @rdname ear_mask
163
#' @export
164
mri_ear_mask = function(
165
  ...,
166
  skull_strip = TRUE,
167
  mask = NULL,
168
  robust = FALSE,
169
  template.file = mni_fname(brain = skull_strip)
170
){
171
172
  L = list(...)
173
  L$robust = robust
174
  L$skull_strip = skull_strip
175
  L$mask = mask
176
  L$template.file = template.file
177
178
  if (is.null(mask) & skull_strip) {
179
    func = function(L, arg, opt) {
180
      nL = names(L)
181
      if (!arg %in% nL) {
182
        L[arg] = opt
183
      }
184
      return(L)
185
    }
186
    uthresh = L$uthresh
187
    if (is.null(uthresh)) {
188
      file = L$file
189
      if (is.null(file)) {
190
        file = L[[1]]
191
      }
192
      uthresh = fslr::fslmax(file)
193
      L$uthresh = uthresh
194
    }
195
196
    L = func(L, "presmooth", FALSE)
197
    L = func(L, "remask", FALSE)
198
    L = func(L, "inskull_mesh", FALSE)
199
    L = func(L, "opts", "-v")
200
  }
201
  res = do.call("ct_ear_mask", args = L)
202
  return(res)
203
}