-
Notifications
You must be signed in to change notification settings - Fork 7
/
slabs.sh
executable file
·86 lines (72 loc) · 2.19 KB
/
slabs.sh
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
#!/bin/bash
# Part of Macro-scripts package (a complete Open Source workflow for processing macro focus stacking photographs)
# Written by Sergey Mashchenko
# Focus stacking with enfuse using a two-stage process (first creating slabs, and then stacking the slabs)
# All files in the current directory with names OUT*.tif (output of Align.sh script) will be processed
# If any command line argument used, will skip the enfuse part (dry run; for debugging)
# Overlap on each side (fraction), for each slab:
over=0
# Overlap should be at least that many images:
Nover_min=0
# Creating the list of tiff images to process:
/usr/bin/ls -1 OUT*.tif 2>/dev/null > List
# Number of input files:
N=`cat List|wc -l`
if test $N -eq 0
then
echo "No files to process; exiting"
exit
fi
N1=$(($N-1))
# Size of each slab (not including the overlap)
Size=`echo $N| awk '{print int(sqrt($1))}'`
# N overlap is at least $Nover_min:
Nover=`echo $Size $over $Nover_min|awk '{A=int($1*$2); if (A<$3) print $3; else print A; fi}'`
# Last (usually smaller) slab size:
Slast=$(($N%$Size-$Nover))
# Number of complete slabs:
Nslabs=$(($N/$Size))
if test $Slast -lt 0
then
Nslabs=$(($Nslabs-1))
Slast=$(($N-$Nslabs*$Size-$Nover))
fi
echo "Size=$Size, Nslabs=$Nslabs, Slast=$Slast, Nover=$Nover"
echo
j=0
# First stage (creating multiple slabs)
for ((i=0; i<$Nslabs; i++))
do
# First file to include in the slab:
k1=$(($i*$Size+$j))
if test $j -lt $Slast
then
j=$(($j+1))
fi
# Last file to include in the slab:
k2=$((($i+1)*$Size+$j+$Nover-1))
if test $k2 -gt $N1
then
k2=$N1
fi
echo Slab=$i, range $k1 - $k2, $(($k2-$k1+1)) frames
K1=$(($k1+1))
K2=$(($k2+1))
if test $# -eq 0
then
enfuse --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --gray-projector=l-star --output=`printf '%04d' $i`.tif `cat List | sed -n ${K1},${K2}p`
fi
done
# Second stage - merging all slabs into final stacked photo
echo
echo "Second stage: stacking the slabs"
if test $# -eq 0
then
enfuse --exposure-weight=0 --saturation-weight=0 --contrast-weight=1 --hard-mask --gray-projector=l-star --output=output.tif ????.tif
if test $?
then
echo
echo "Success!"
echo
fi
fi