|
a |
|
b/R/coarse_body.R |
|
|
1 |
#' @title Coarse Masking of Image from Z-slicding |
|
|
2 |
#' @description Calculates a Hull at each slice then stacks them |
|
|
3 |
#' |
|
|
4 |
#' @param x Binary \code{antsImage} |
|
|
5 |
#' @param keep_slices number of slices to keep if needed |
|
|
6 |
#' |
|
|
7 |
#' @return Array or \code{antsImage} object |
|
|
8 |
#' @export |
|
|
9 |
#' @importFrom ANTsRCore is.antsImage |
|
|
10 |
#' @importFrom ptinpoly pip2d |
|
|
11 |
coarse_body = function(x, keep_slices = 20) { |
|
|
12 |
|
|
|
13 |
bb = as.array(x) > 0 |
|
|
14 |
dimg = dim(bb) |
|
|
15 |
number_slices = dimg[3] |
|
|
16 |
dslice = dimg[1:2] |
|
|
17 |
ind = which(bb, arr.ind = TRUE) |
|
|
18 |
omat = array(FALSE, dim = dimg) |
|
|
19 |
|
|
|
20 |
|
|
|
21 |
ind = as.data.frame(ind) |
|
|
22 |
ps = split(ind, ind[,3]) |
|
|
23 |
ps = lapply(ps, function(x){ |
|
|
24 |
as.matrix(x[, 1:2]) |
|
|
25 |
}) |
|
|
26 |
|
|
|
27 |
# get values that are true for ellipse |
|
|
28 |
# same for all slices |
|
|
29 |
L = lapply(dslice, seq) |
|
|
30 |
test.ind = as.matrix(expand.grid(L)) |
|
|
31 |
|
|
|
32 |
res = lapply(ps, ptinpoly::pip2d, Queries = test.ind) |
|
|
33 |
|
|
|
34 |
i = 1 |
|
|
35 |
for (i in seq(number_slices)) { |
|
|
36 |
# print(i) |
|
|
37 |
r = res[[i]] >= 0 |
|
|
38 |
get_ind = test.ind[r, ] |
|
|
39 |
omat[,,i][get_ind] = TRUE |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
prob = apply(omat, c(1,2), mean) |
|
|
43 |
prob = prob > (keep_slices/number_slices) |
|
|
44 |
|
|
|
45 |
for (i in seq(number_slices)) { |
|
|
46 |
# print(i) |
|
|
47 |
omat[,,i][!prob] = FALSE |
|
|
48 |
} |
|
|
49 |
if (is.antsImage(x)) { |
|
|
50 |
omat = as.antsImage(omat, reference = x) |
|
|
51 |
} |
|
|
52 |
return(omat) |
|
|
53 |
} |