-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathus_track.R
191 lines (166 loc) · 5.73 KB
/
us_track.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
us_track <- function(center.ini = NULL,
inputfile = NULL,
filtertype = "none",
filtersize = 5,
overlap = 50,
jump = 0,
kernel = 31) {
# ###################### SET UP ######################
if (!is.na(match("batch.simulation", ls(
all.names = TRUE, envir = .GlobalEnv
)))) {
# do nothing on batch run
} else {
# restart all variables
rm(list = ls(all.names = TRUE, envir = .GlobalEnv))
options(warn = -1)
}
# custom functions
round_2_odd <- function(x) {
2 * floor(x / 2) + 1
}
# dir for storing images
raw.dir <- file.path("www", "0 raw")
# delete folder if it already exists
if (dir.exists(raw.dir)) {
unlink(file.path(raw.dir), recursive = TRUE)
}
dir.create(file.path(raw.dir), showWarnings = FALSE)
shiny::addResourcePath(prefix = "www", directoryPath = raw.dir)
gray.dir <- file.path("www", "2 gray")
# delete folder if it already exists
if (dir.exists(gray.dir)) {
unlink(file.path(gray.dir), recursive = TRUE)
}
dir.create(file.path(gray.dir), showWarnings = FALSE)
shiny::addResourcePath(prefix = "www", directoryPath = gray.dir)
detrend.dir <- file.path("www", "3 detrend")
# delete folder if it already exists
if (dir.exists(detrend.dir)) {
unlink(file.path(detrend.dir), recursive = TRUE)
}
dir.create(file.path(detrend.dir), showWarnings = FALSE)
shiny::addResourcePath(prefix = "www", directoryPath = detrend.dir)
hist.dir <- file.path("www", "4 equaliz")
# delete folder if it already exists
if (dir.exists(hist.dir)) {
unlink(file.path(hist.dir), recursive = TRUE)
}
dir.create(file.path(hist.dir), showWarnings = FALSE)
shiny::addResourcePath(prefix = "www", directoryPath = hist.dir)
filter.dir <- file.path("www", "5 filter")
# delete folder if it already exists
if (dir.exists(filter.dir)) {
unlink(file.path(filter.dir), recursive = TRUE)
}
dir.create(file.path(filter.dir), showWarnings = FALSE)
shiny::addResourcePath(prefix = "www", directoryPath = filter.dir)
bin.dir <- file.path("www", "6 bin")
# delete folder if it already exists
if (dir.exists(bin.dir)) {
unlink(file.path(bin.dir), recursive = TRUE)
}
dir.create(file.path(bin.dir), showWarnings = FALSE)
shiny::addResourcePath(prefix = "www", directoryPath = bin.dir)
morph.dir <- file.path("www", "7 morph")
# delete folder if it already exists
if (dir.exists(morph.dir)) {
unlink(file.path(morph.dir), recursive = TRUE)
}
dir.create(file.path(morph.dir), showWarnings = FALSE)
shiny::addResourcePath(prefix = "www", directoryPath = morph.dir)
# dir for storing images AFTER prrocess
out.dir <- file.path("www", "8 output")
# delete folder if it already exists
if (dir.exists(out.dir)) {
unlink(file.path(out.dir), recursive = TRUE)
}
dir.create(file.path(out.dir), showWarnings = FALSE)
shiny::addResourcePath(prefix = "www", directoryPath = out.dir)
# dir for storing results
res.dir <- file.path("www", "CSV")
dir.create(file.path(res.dir), showWarnings = FALSE)
shiny::addResourcePath(prefix = "www", directoryPath = res.dir)
# color palette (grayscale)
pal <- grDevices::gray(seq(
from = 0,
to = 1,
length.out = 256
), alpha = NULL)
###################### DATA TO ANALYZE ######################
# video (.mp4) file names
if (!is.na(match("batch.simulation", ls(
all.names = TRUE, envir = .GlobalEnv
)))) {
inputfile <- "sim_video.mp4"
} else {
inputfile <- inputfile
}
# Get video info such as width, height, format, duration and framerate
info <- av::av_media_info(inputfile)
# ###################### TRACKING PARAMETERS ######################
# object size
kernel <- round_2_odd(kernel) # odd numbers only
if (!is.na(match("batch.simulation", ls(
all.names = TRUE, envir = .GlobalEnv
)))) {
filter.type <- track.config[1]
filter.size <- as.numeric(track.config[2])
overlap <- as.numeric(track.config[3])
jump <- as.numeric(track.config[4])
param <-
as.data.frame(matrix(
c(filter.type, filter.size, overlap, jump),
nrow = 1,
dimnames = list(c(), c(
"Filter type", "Filter size", "Overlap %", "Jump"
))
))
} else {
param <- list(
`Filter type:TEXT` = filtertype,
`Filter size:NUM` = filtersize,
`Overlap %:NUM` = overlap,
`Jump:NUM` = jump
)
}
roi <-
round_2_odd(kernel * (1 + as.numeric(param$`Overlap %`) / 100)) # odd numbers only
# ###################### CODE TO RUN ######################
# read 1:N images and process all frames
source("f_track_first.R", local = TRUE)
source("f_track_all.R", local = TRUE)
# show tracking
run_all <-
f_all_frames(
inputfile = inputfile,
info = info,
param = param,
track.by = c("cross-correlation", "blob")[1],
raw.dir = raw.dir,
gray.dir = gray.dir,
detrend.dir = detrend.dir,
hist.dir = hist.dir,
filter.dir = filter.dir,
bin.dir = bin.dir,
morph.dir = morph.dir,
out.dir = out.dir,
res.dir = res.dir,
pal = pal,
kernel = kernel,
roi = roi,
center.ini = center.ini,
plot.border = FALSE,
dsp = c("gray", "equalize", "mean", "threshold", "morphologic")
)
# Set output file
output_filename <- file.path("www", "outputvideo.mp4")
# ###################### READ DATA FOR ANALYSIS ######################
if (!is.na(match("batch.simulation", ls(all = TRUE, envir = .GlobalEnv)))) {
# do nothing on batch run
} else {
# open output video
# utils::browseURL(output_filename)
}
# ###################### END RUN ######################
}