-
Notifications
You must be signed in to change notification settings - Fork 0
/
spectralize.sh
executable file
·179 lines (163 loc) · 6.82 KB
/
spectralize.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
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
#!/bin/sh
# Script for creating various multispectral measurements from flatfielded images. Requires imagemagick, teem/unu, and parallel.
# Assumes shots are numbered according to the VisCenter N-Shot Table Standard. See readme for more details and pre-processing information.
# Called by mstk.sh, or run standalone from inside the output folder created by applyflats.sh.
echo
echo -----------------------------------------------
echo Spectralize - Render Multispectral Measurements
echo -----------------------------------------------
echo
## Ask for spectralize.sh output formats
if [[ -z $flatpng_true ]]; then
while true; do
read -p "Keep PNG output of flatfielded images? (y/n) " flatpng_true
case $flatpng_true in
[YyNn] ) break;;
* ) echo "Please answer y or n.";;
esac
done
fi
if [[ -z $multijpg_true ]]; then
while true; do
read -p "Create JPG output of multispectral measurements? (y/n) " multijpg_true
case $multijpg_true in
[YyNn] ) break;;
* ) echo "Please answer y or n.";;
esac
done
fi
if [[ -z $multipng_true ]]; then
while true; do
read -p "Create PNG output of multispectral measurements? (y/n) " multipng_true
case $multipng_true in
[YyNn] ) break;;
* ) echo "Please answer y or n.";;
esac
done
fi
if [[ -z $keepnrrd ]]; then
while true; do
read -p "Keep nrrd files? (y/n) " keepnrrd
case $keepnrrd in
[YyNn] ) break;;
* ) echo "Please answer y or n.";;
esac
done
fi
if [ $multipng_true == "N" ] || [ $multipng_true == "n" ]; then
if [ $multijpg_true == "N" ] || [ $multijpg_true == "n" ]; then
if [ $keepnrrd == "N" ] || [ $keepnrrd == "n" ]; then
echo
echo "$(date +"%F") :: $(date +"%T") :: WARNING :: No outputs selected. Exiting..."
echo
exit 1
fi
echo
echo "$(date +"%F") :: $(date +"%T") :: WARNING :: No image outputs selected. Only nrrd's will be created."
echo
fi
fi
if [[ -z $measures ]]; then
measures="min max mean median variance skew intc slope error sd sum L1 L2 Linf"
fi
ROOT="$PWD"
# Go through each volume folder, then go through each page folder. If there's not a multispectral folder, make one and start processing
for i in */; do
cd $i
VOLUME="$PWD"
cd $VOLUME/png
for j in */; do
cd $j
PAGE=$PWD
folio="$(basename $j)"
if [[ ! -d $VOLUME/png/$folio ]]; then
echo
echo "$(date +"%F") :: $(date +"%T")" :: "$folio": No PNGs found
else
if [[ ! -d $VOLUME/multispectral/$folio ]]; then
echo
echo "$(date +"%F") :: $(date +"%T") :: Beginning work on $folio"
if [ $multipng_true == "Y" ] || [ $multipng_true == "y" ]; then
if [[ ! -d $VOLUME/multispectral/$folio ]]; then
mkdir -p $VOLUME/multispectral/$folio
fi
fi
if [ $multijpg_true == "Y" ] || [ $multijpg_true == "y" ]; then
if [[ ! -d $VOLUME/multispectral_jpg/$folio ]]; then
mkdir -p $VOLUME/multispectral_jpg/$folio
fi
fi
if [[ ! -d $VOLUME/nrrd/$folio ]]; then
mkdir -p $VOLUME/nrrd/$folio
fi
cd $VOLUME/png/$folio/
echo
echo "$(date +"%F") :: $(date +"%T")" :: Creating volume...
for k in *.png; do
WAVELENGTH="$(exiv2 -qpa $k | grep Exif.Photo.SpectralSensitivity | awk '{print $4}' | sed 's/(\([0-9A-Za-z]*\)nm,/\1/')"
if [[ "$WAVELENGTH" != "non" ]]; then
echo "$WAVELENGTH $k">>sort.txt
fi
done
# Make the nrrd volume for the page
unu join -a 2 -i $(sort sort.txt | awk '{print $2}') -o $VOLUME/nrrd/$folio/$folio.nrrd
rm sort.txt
echo
echo "$(date +"%F") :: $(date +"%T")" :: Applying measures...
# Perform different measurements on the nrrd volume
# 'product' has been taken out of the list below. I've only had it produce null results and cause errors when quantizing.//SP
for l in $measures; do echo $l; done | parallel --eta -u unu project -a 2 -i $VOLUME/nrrd/$folio/$folio.nrrd -o $VOLUME/nrrd/$folio/$folio-f-m-{}.nrrd -m {}
echo
if [ $multipng_true == "Y" ] || [ $multipng_true == "y" ] || [ $multijpg_true == "Y" ] || [ $multijpg_true == "y" ]; then
echo "$(date +"%F") :: $(date +"%T")" :: Remapping and quantizing results...
# Download the color remapping file. Use curl if wget isn't installed. Important since OSX 10.8 doesn't come with wget
if [ ! -f $VOLUME/nrrd/darkhue.txt ]; then
if command -v wget >/dev/null; then
wget -nv -P $VOLUME/nrrd/ http://teem.sourceforge.net/nrrd/tutorial/darkhue.txt
else
echo "$(date +"%F") :: $(date +"%T")" :: wget not found. Using curl...
curl --progress-bar http://teem.sourceforge.net/nrrd/tutorial/darkhue.txt -o "$VOLUME/nrrd/darkhue.txt"
fi
fi
# Remap each measurement nrrd and output to jpg/png, then histogram equalize each nrrd, remap it, and output it to jpg/png
for m in $VOLUME/nrrd/$folio/*-f-m-*.nrrd; do
STRIPPEDM=$(basename $m | sed 's/\(.*\)\..*/\1/')
if [ $multipng_true == "Y" ] || [ $multipng_true == "y" ]; then
if [[ $noheq != "n" ]]; then
QUANTIZECOMMANDS+="unu rmap -m $VOLUME/nrrd/darkhue.txt -i $m | unu quantize -b 8 -o $VOLUME/multispectral/$folio/$STRIPPEDM-noheq.png\n"
fi
QUANTIZECOMMANDS+="unu heq -b 3000 -a 0.5 -i $m | unu rmap -m $VOLUME/nrrd/darkhue.txt | unu quantize -b 8 -o $VOLUME/multispectral/$folio/$STRIPPEDM-heq.png\n"
fi
if [ $multijpg_true == "Y" ] || [ $multijpg_true == "y" ]; then
if [[ $noheq != "n" ]]; then
QUANTIZECOMMANDS+="unu rmap -m $VOLUME/nrrd/darkhue.txt -i $m | unu quantize -b 8 -o $VOLUME/multispectral_jpg/$folio/$STRIPPEDM-noheq.ppm && cjpeg -q 95 -outfile $VOLUME/multispectral_jpg/$folio/$STRIPPEDM-noheq.jpg $VOLUME/multispectral_jpg/$folio/$STRIPPEDM-noheq.ppm && rm -f $VOLUME/multispectral_jpg/$folio/$STRIPPEDM-noheq.ppm\n"
fi
QUANTIZECOMMANDS+="unu heq -b 3000 -a 0.5 -i $m | unu rmap -m $VOLUME/nrrd/darkhue.txt | unu quantize -b 8 -o $VOLUME/multispectral_jpg/$folio/$STRIPPEDM-heq.ppm && cjpeg -q 95 -outfile $VOLUME/multispectral_jpg/$folio/$STRIPPEDM-heq.jpg $VOLUME/multispectral_jpg/$folio/$STRIPPEDM-heq.ppm && rm -f $VOLUME/multispectral_jpg/$folio/$STRIPPEDM-heq.ppm\n"
fi
done
# echo $QUANTIZECOMMANDS > $PWD/commands.txt
# Run the quantize/remapping commands for each page
echo $QUANTIZECOMMANDS | parallel --eta -u -j 8
QUANTIZECOMMANDS=""
# Remove intermediate nrrd's if we don't want to keep them. Saves space during processing.
if [ $keepnrrd == "N" ] || [ $keepnrrd == "n" ]; then
rm -rf $VOLUME/nrrd/$folio
fi
fi
echo
echo "$(date +"%F") :: $(date +"%T")" :: "$folio" done.
echo
fi
fi
cd $VOLUME/png
done
# Clean-up nrrd folder
if [ $keepnrrd == "N" ] || [ $keepnrrd == "n" ]; then
rm -rf $VOLUME/nrrd
fi
# Remove png folder if we don't want it...
if [[ "$flatpng_true" == "N" || "$flatpng_true" == "n" ]]; then
rm -rf $VOLUME/png
fi
cd $ROOT
done