-
Notifications
You must be signed in to change notification settings - Fork 0
/
conncomp.R
134 lines (103 loc) · 3.13 KB
/
conncomp.R
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
#' Extract connected components from a 3D mask
#'
#' @export
#' @import purrr
#' @param mask a 3D binary array
#' @param connect the connectiivty constraint: "6-connect", "18-connect", or "26-connect"
#' @return a two-element list of the connected components (cluster \code{index} and cluster \code{size})
#' The first element \code{index} is a 3D array containing the cluster index of the connected component for each voxel.
#' The second element \code{size} is a 3D array consisting of the size of the connected component inhabited by each voxel.
#'
#' @examples
#'
#' dat <- array(as.logical(rnorm(10*10*10)>.5), c(10, 10, 10))
#' res1 <- conn_comp_3D(dat, connect="6-connect")
#' res2 <- conn_comp_3D(dat, connect="18-connect")
#' res3 <- conn_comp_3D(dat, connect="26-connect")
#'
conn_comp_3D <- function(mask,connect=c("26-connect", "18-connect", "6-connect")) {
stopifnot(length(dim(mask)) == 3 && is.logical(mask[1]))
connect <- match.arg(connect)
nodes <- numeric(length(mask)/9)
labels <- array(0, dim(mask))
DIM <- dim(mask)
xdim <- DIM[1]
ydim <- DIM[2]
zdim <- DIM[3]
local.mask <- if (connect == "6-connect") {
as.matrix(
rbind(expand.grid(x=c(-1,0,1), y=0, z=0),
expand.grid(x=0, y=c(-1, 1), z=0),
expand.grid(x=0, y=0, z=c(-1, 1)))
)
} else if (connect == "18-connect") {
as.matrix(rbind(
expand.grid(x=c(-1,0,1), y=0, z=0),
expand.grid(x=0, y=c(-1,1), z=0),
expand.grid(x=0, y=0, z=c(-1,1)),
expand.grid(x=c(-1,1), y=c(-1,1), z=0),
expand.grid(x=c(-1,1), y=0, z=c(-1,1)),
expand.grid(x=0, y=c(-1,1), z=c(-1,1)))
)
} else {
as.matrix(expand.grid(x=c(-1,0,1), y=c(-1,0,1), z=c(-1,0,1)))
}
dimnames(local.mask) <- NULL
local.mask <- local.mask[-(ceiling(nrow(local.mask)/2)),,drop=FALSE]
tlocal.mask <- t(local.mask)
neighbors <- function(vox) {
vox.hood <- t(tlocal.mask + vox)
if (any(vox == 1) || any(vox == DIM)) {
vox.hood <- vox.hood[apply(vox.hood, 1, function(coords) {
all(coords > 1 & coords <= DIM)
}),,drop=FALSE]
}
vox.hood[labels[vox.hood] != 0,,drop=F]
}
find <- function(i) {
while (nodes[i] != i) {
i <- nodes[i]
}
nodes[i]
}
nextlabel <- 1
grid <- .indexToGrid(which(mask>0), dim(mask))
for (i in 1:NROW(grid)) {
vox <- grid[i,]
nabes <- neighbors(vox)
if (nrow(nabes) == 0) {
nodes[nextlabel] <- nextlabel
labels[vox[1],vox[2],vox[3]] <- nextlabel
} else {
L <- labels[nabes]
ML <- min(L)
labels[vox[1],vox[2], vox[3]] <- ML
nodes[nextlabel] <- ML
for (lab in L) {
rootx <- find(lab)
nodes[rootx] <- find(ML)
}
}
nextlabel <- nextlabel + 1
}
## pass2
for (k in 1:zdim) {
for (j in 1:ydim) {
for (i in 1:xdim) {
if (labels[i,j,k] > 0) {
labels[i,j,k] <- find(labels[i,j,k])
}
}
}
}
labs <- labels[labels!=0]
forelabs <- labels > 0
clusters <- sort(table(labs), decreasing=TRUE)
SVol <- array(0, dim(mask))
SVol[forelabs] <- clusters[as.character(labs)]
indices <- 1:length(clusters)
names(indices) <- names(clusters)
IVol <- array(0, dim(mask))
IVol[forelabs] <- indices[as.character(labs)]
list(index=IVol, size=SVol)
}