|
a |
|
b/R/dil_ero.R |
|
|
1 |
#' @title Fill image holes with dilation then erosion |
|
|
2 |
#' @description This function calls \code{mean_image} to dilate an image, then calls |
|
|
3 |
#' it again to erode it. |
|
|
4 |
#' @param file (character) filename of image to be filled |
|
|
5 |
#' @param outfile (character) name of resultant filled file |
|
|
6 |
#' @param nvoxels (integer) Number of voxels to smooth over, creates vxvxv box. |
|
|
7 |
#' @param zeropad (logical) Perform \code{zero_pad} before running. |
|
|
8 |
#' @param remove.ends (logical) Remove top and bottom dilation. |
|
|
9 |
#' @param tol (double) Tolerance for thresholding after \code{\link{fft}}. |
|
|
10 |
#' @param refill (logical) Run \code{\link{fslfill}} after dilation/erosion. |
|
|
11 |
#' @param retimg (logical) return image of class nifti |
|
|
12 |
#' @param reorient (logical) If retimg, should file be reoriented when read in? |
|
|
13 |
#' Passed to \code{\link{readNIfTI}}. |
|
|
14 |
#' @param intern (logical) pass to \code{\link{system}} |
|
|
15 |
#' @param verbose (logical) print out command before running |
|
|
16 |
#' @param ... additional arguments passed to \code{\link{readNIfTI}}. |
|
|
17 |
#' @return character or logical depending on intern |
|
|
18 |
#' @importFrom neurobase zero_pad |
|
|
19 |
#' @note This function binarizes the image before running. |
|
|
20 |
#' @export |
|
|
21 |
dil_ero = function(file, |
|
|
22 |
outfile = NULL, |
|
|
23 |
nvoxels = 3, |
|
|
24 |
zeropad = TRUE, |
|
|
25 |
remove.ends = FALSE, |
|
|
26 |
tol = .Machine$double.eps^0.5, |
|
|
27 |
refill = TRUE, |
|
|
28 |
retimg = FALSE, |
|
|
29 |
reorient = FALSE, |
|
|
30 |
intern=TRUE, verbose = TRUE, |
|
|
31 |
...){ |
|
|
32 |
have.outfile = TRUE |
|
|
33 |
|
|
|
34 |
if (retimg){ |
|
|
35 |
if (is.null(outfile)) { |
|
|
36 |
outfile = tempfile() |
|
|
37 |
have.outfile = FALSE |
|
|
38 |
} |
|
|
39 |
} else { |
|
|
40 |
stopifnot(!is.null(outfile)) |
|
|
41 |
} |
|
|
42 |
|
|
|
43 |
file = check_nifti(file, reorient = reorient) |
|
|
44 |
img = file > 0 |
|
|
45 |
dimg = dim(file) |
|
|
46 |
##### should make for all max |
|
|
47 |
ind = which(img > 0, arr.ind = TRUE) |
|
|
48 |
ind = ind[ (ind[, "dim3"] %in% c(1, dimg[3])) | |
|
|
49 |
(ind[, "dim1"] %in% c(1, dimg[1])) | |
|
|
50 |
(ind[, "dim2"] %in% c(1, dimg[2])) ,] |
|
|
51 |
nind = nrow(ind) |
|
|
52 |
|
|
|
53 |
is.wholenumber <- function(x, tol = .Machine$double.eps^0.5) { |
|
|
54 |
abs(x - round(x)) < tol |
|
|
55 |
} |
|
|
56 |
stopifnot(length(nvoxels) %in% c(1, 3)) |
|
|
57 |
if (length(nvoxels) == 1){ |
|
|
58 |
stopifnot(is.wholenumber(nvoxels)) |
|
|
59 |
n <- length((-nvoxels):nvoxels) |
|
|
60 |
kdim = c(n, n, n) |
|
|
61 |
} |
|
|
62 |
if (length(nvoxels) == 3){ |
|
|
63 |
kdim = (nvoxels*2) + 1 |
|
|
64 |
stopifnot(is.wholenumber(kdim)) |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
if (zeropad) { |
|
|
68 |
img = zero_pad(img, kdim = kdim) |
|
|
69 |
} |
|
|
70 |
dil = mean_image(img, nvoxels = nvoxels, verbose = verbose) > tol |
|
|
71 |
rm(img) |
|
|
72 |
dil = mean_image(1 - dil, nvoxels = nvoxels, verbose = verbose) |
|
|
73 |
dil = dil > tol |
|
|
74 |
dil = 1 - dil |
|
|
75 |
if (zeropad) { |
|
|
76 |
dil = zero_pad(dil, kdim = kdim, invert = TRUE) |
|
|
77 |
} |
|
|
78 |
dil = niftiarr(file, dil) |
|
|
79 |
rm(file) |
|
|
80 |
|
|
|
81 |
if (remove.ends) { |
|
|
82 |
#### making the ends correct - boundary problem |
|
|
83 |
dil@.Data[,,1] = array(0, dim=dimg[c(1,2)]) |
|
|
84 |
dil@.Data[,,dimg[3]] = array(0, dim=dimg[c(1,2)]) |
|
|
85 |
|
|
|
86 |
dil@.Data[,1,] = array(0, dim=dimg[c(1,3)]) |
|
|
87 |
dil@.Data[,dimg[2],] = array(0, dim=dimg[c(1,3)]) |
|
|
88 |
|
|
|
89 |
dil@.Data[1,,] = array(0, dim=dimg[c(2,3)]) |
|
|
90 |
dil@.Data[dimg[1],,] = array(0, dim=dimg[c(2,3)]) |
|
|
91 |
|
|
|
92 |
if (nind >0 ){ |
|
|
93 |
dil@.Data[ ind ] = 1 |
|
|
94 |
} |
|
|
95 |
} |
|
|
96 |
if (refill) { |
|
|
97 |
dil = fslfill(file = dil, |
|
|
98 |
retimg=TRUE, |
|
|
99 |
intern=intern, verbose = verbose) |
|
|
100 |
} |
|
|
101 |
dil = cal_img(dil) |
|
|
102 |
if (have.outfile){ |
|
|
103 |
gzipped = grepl("gz$", get.imgext()) |
|
|
104 |
writenii(dil, filename = outfile, gzipped = gzipped) |
|
|
105 |
} |
|
|
106 |
if (retimg){ |
|
|
107 |
return(dil) |
|
|
108 |
} |
|
|
109 |
return(outfile) |
|
|
110 |
} |