-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstacking.R
162 lines (104 loc) · 4.5 KB
/
stacking.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
#######################################################################################
#' TODO
#' create a list of enfuse options , algo_01, algo_02,...
#' merge the slabs with each of these algorithms
#' The different algo can be added with an unlimitted number at the end of the general options
#'
#' create an all option to merge all pictures within the directory and all subdirectories
#'
#' create a "small" option that will create small sized version of all pictures
#' copy them into a directory and then apply the code on these small sized pictures
#' in order to have a quick check
#######################################################################################
setwd("/home/gilles/stats/Rprojects/201412_stacking/photos")
a <- Sys.time()
#' List the jpg or tif pictures in the working directory
pics <- list.files( pattern = "\\.(jpeg|jpg|tiff|tif)$", ignore.case = TRUE)
i <- pics[2]
#' Use imagemagik to convert the jpg pictures into tif
jpg <- pics[grep(x = pics, pattern = "\\.(jpg|jpeg)$", ignore.case = TRUE)]
for (i in jpg) {
system(paste0("convert -rotate 0 -compress none ", i, " z_", i, ".tif"))
}
#' Align the images
start <- Sys.time()
system("align_image_stack -v -m -s 2 -a aligned_ *.tif", show.output.on.console = TRUE)
end <- Sys.time()
end-start
#' Remove the tif files previously created
file.remove(paste0("z_", jpg, ".tif"))
#' Change the temp files location to the curent directory
#' (to avoid problems of disk space at the normal temp location)
system("export TMPDIR=./")
#' create a list of aligned pictures names for each slab
#'
aligned <- list.files(pattern ="^aligned_.*\\.tif$")
slab_size = 5
slab_overlap = 1
if(slab_overlap >= slab_size) {stop("slab_overlap must be < slab_size")}
pr <- 1:slab_size
slabs <- list(aligned[pr])
while(pr[slab_size] <= length(aligned)) {
pr <- (pr[length(pr) - slab_overlap] + 1) : (pr[length(pr) - slab_overlap] + slab_size)
slabs <- c(slabs,list(aligned[pr]))
}
#' Remove the NA values from the last slab
slabs[[length(slabs)]] <- slabs[[length(slabs)]][!is.na(slabs[[length(slabs)]])]
#' Remove the last slab if it contains only pictures already present in the previous slab
if(length(slabs[[length(slabs)]]) == slab_overlap) {
slabs <- slabs[[-length(slabs)]]
}
# slabs
#' Create the slabs
enfuse_opts <- paste("--verbose=6 -m 400",
"--exposure-weight=0.001",
"--saturation-weight=0.001",
"--contrast-weight=1",
"--hard-mask",
"--contrast-window-size=5", sep = " ")
for(i in 1:length(slabs)) {
system(paste("enfuse", enfuse_opts,
paste0("-o slab_", sprintf("%04.0f", i), ".tif"),
do.call(paste, as.list(slabs[[i]]))),
show.output.on.console = TRUE)
}
#' Merge the slabs with a basic contrast window algorithm
nr <- length(slabs)
enfuse_opts <- paste("--verbose=6 -m 400",
"--exposure-weight=0.001",
"--saturation-weight=0.001",
"--contrast-weight=1",
"--hard-mask",
"--contrast-window-size=5", sep = " ")
system(paste("enfuse", enfuse_opts,
paste0("-o final_", nr, "slabs.tif"),
"slab_*.tif", sep = " "), show.output.on.console = TRUE)
#' Merge the slabs with a Laplacian algorithm
enfuse_opts <- paste("--verbose=6 -m 400",
"--exposure-weight=0",
"--saturation-weight=0",
"--contrast-weight=1",
"--hard-mask",
"--contrast-window-size=5"
"--contrast-edge-scale=0.3",
"--contrast-min-curvature=0.5%", sep = " ")
system(paste("enfuse", enfuse_opts,
paste0("-o final_laplacian_", nr, "slabs.tif"),
"slab_*.tif", sep = " "), show.output.on.console = TRUE)
#' Merge the aligned pictures without slabbing
start <- Sys.time()
enfuse_opts <- paste("--verbose=6 -m 400",
"--exposure-weight=0.001",
"--saturation-weight=0.001",
"--contrast-weight=1",
"--hard-mask",
"--contrast-window-size=5", sep = " ")
system(paste("enfuse", enfuse_opts,
"-o final_.tif",
"aligned_*.tif", sep = " "), show.output.on.console = TRUE)
end <- Sys.time()
end-start
# Time consumed by the whole process
z <- Sys.time()
z - a
# file.remove(list.files(pattern ="^aligned_.*\\.tif$"))