forked from bglnelissen/slideToolkit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
slideMask
executable file
·384 lines (350 loc) · 12.5 KB
/
slideMask
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/bin/bash
#
# Description: Create scaled mask and macro from a virtual slide
#
# The MIT License (MIT)
# Copyright (c) 2014-2024, Bas G.L. Nelissen, Sander W. van der Laan,
# UMC Utrecht, Utrecht, the Netherlands.
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#
# Variables
SCRIPTNAME=$(basename $0)
DESCRIPTIONSHORT="Create scaled *.mask.png and *.macro.png from a virtual slide."
DEPENDENCIES=("convert")
SUPPORTED_FORMATS=("TIFF" "Aperio" "Hamamatsu" "iScan" "iScanHT" "Leica")
DEFAULT_MASKSUFFIX=".mask"
DEFAULT_MACROSUFFIX=".macro"
DEFAULT_MACRO_THUMBSIZE="2000x2000" # Maximum size of macro image
DEFAULT_SLURMARGS="--time 00:15:00 --mem=16G" # Maximum values: rt=runtime HH:MM:SS vmem=GB
DEFAULT_MASKARGS="-blur 40x40 -fuzz 4%"
# Errors go to stderr
err() {
echo "${SCRIPTNAME} ERROR: $@" >&2
}
# usage message
usage() {
cat <<- EOF
$SCRIPTNAME --help for more information.
EOF
}
# version info
version() {
cat << EOF
slideToolKit v1.2
($SCRIPTNAME is part of the slideToolKit)
The MIT License (MIT) <http://opensource.org/licenses/MIT>
Copyright (c) 2014-2024, Bas G.L. Nelissen, UMC Utrecht, the Netherlands.
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
There is NO WARRANTY, to the extent permitted by law.
Written by Bas G.L. Nelissen | https://github.com/bglnelissen.
Edits by Sander W. van der Laan | https://github.com/swvanderlaan.
EOF
}
# help message
helpMessage() {
cat <<- EOF
${SCRIPTNAME}: ${DESCRIPTIONSHORT}
usage:
$SCRIPTNAME [options] -f <filename>
options:
-f, --file <filename> virtual slide to create mask from
[--masksuffix] <text> mask suffix ["$DEFAULT_MASKSUFFIX"]
[--macrosuffix] <text> macro suffix ["$DEFAULT_MACROSUFFIX"]
[-l, --layer] <integer> force specific layer by layer id
[--mask-args] <arguments> mask arguments (imageMagick) ["$DEFAULT_MASKARGS"]
[--ignore-dependencies] ignores all dependencies, but gives warning
--help display this help
--version display version and license information
examples:
$SCRIPTNAME "file.tif"
$SCRIPTNAME --file="file.tif"
print a job list:
find "\$(pwd)" -iname "*.TIF" -exec $(command -v "$SCRIPTNAME") -f "{}" \;
print a job list for 'qsub':
find "\$(pwd)" -iname "*tif" -type f \\
-exec printf "sbatch $DEFAULT_SLURMARGS --chdir="\$(pwd)/" " \; \\
-exec printf "\$(command -v "$SCRIPTNAME") " \; \\
-exec echo "--file=\""{}"\" " \;
dependencies: ${DEPENDENCIES[@]}
supported formats: ${SUPPORTED_FORMATS[@]}
You can mask parts of your image that you do not wat to process. This
program will use 'convert' to create a mask for you which you can later
edit if needed. The mask is a layer within the original TIF file. The
slideToolKit excludes real black in further processing (color #000000).
Two new files will be created, one file.macro.png, and one masked macro
file (file.mask.png). Definitions of masking can be set with the
--mask-args flag. A blur creates space between the mask and the tissue.
A fuzz selects the background. A fuzz of 3% works well, but fails on
very light slides, a fuzz of 2% might be to easy on the selection, 5%
works on slides with a darkish background. Pipe the 'multiple folders at
once' output in 'parallel' for parallel processing. For some slides, the
virtual slide format is not known. For these instances you need to run
slideInfo to find the correct layer manually. When the mask layer is
identified (approximately 2000x2000 pixels), use the --layer flag.
The slideToolKit and all its tools are released under the terms of the MIT license.
The slideToolKit (C) 2014-2024, Bas G.L. Nelissen, UMC Utrecht, the Netherlands;
Sander W. van der Laan | s.w.vanderlaan [at] gmail [dot] com, UMC Utrecht,
the Netherlands.
Report issues at https://github.com/swvanderlaan/slideToolKit/issues.
EOF
}
# Menu
# Empty variables
FILE=""
LAYER=""
MASKSUFFIX=""
MACROSUFFIX=""
MASKARGS=""
# illegal option
illegalOption() {
cat <<- EOF
$SCRIPTNAME: illegal option $1
$(usage)
EOF
exit 1
}
# loop through options
while :
do
case $1 in
-h)
usage
exit 0 ;;
--help | -\?)
helpMessage
exit 0 ;;
--version )
version
exit 0 ;;
-f | --file)
FILE=$2
shift 2 ;;
--file=*)
FILE=${1#*=}
shift ;;
-l | --layer)
LAYER=$2
shift 2 ;;
--layer=*)
LAYER=${1#*=}
shift ;;
--masksuffix)
MASKSUFFIX=$2
shift 2 ;;
--masksuffix=*)
MASKSUFFIX=${1#*=}
shift ;;
--mask-args)
MASKARGS=$2
shift 2 ;;
--mask-args=*)
MASKARGS=${1#*=}
shift ;;
--macrosuffix)
MACROSUFFIX=$2
shift 2 ;;
--macrosuffix=*)
MACROSUFFIX=${1#*=}
shift ;;
--ignore-dependencies)
IGNOREDEPENDENCIES=TRUE
shift ;;
--) # End of all options
shift
break ;;
-*)
illegalOption "$1"
shift ;;
*) # no more options. Stop while loop
break ;;
esac
done
# DEFAULTS
# set FILE
if [ "$FILE" != "" ]; then
FILE="$FILE"
else
FILE="$1"
fi
# set MASKSUFFIX default
if [ "$MASKSUFFIX" != "" ]; then
MASKSUFFIX="$MASKSUFFIX"
else
MASKSUFFIX="$DEFAULT_MASKSUFFIX"
fi
# set MASKARGS default
if [ "$MASKARGS" != "" ]; then
MASKARGS="$MASKARGS"
else
MASKARGS="$DEFAULT_MASKARGS"
fi
# set MACROSUFFIX default
if [ "$MACROSUFFIX" != "" ]; then
MACROSUFFIX="$MACROSUFFIX"
else
MACROSUFFIX="$DEFAULT_MACROSUFFIX"
fi
# set IGNOREDEPENDENCIES
if [[ "$IGNOREDEPENDENCIES" =~ (true|TRUE|YES|yes) ]] ; then
IGNOREDEPENDENCIES=true
else
IGNOREDEPENDENCIES=false
fi
# requirements
checkRequirements() {
if ! [[ -f "$FILE" ]] ; then
err "No such file: $FILE"
usage
exit 1
fi
}
# Dependencies
checkDependencies(){
DEPS=""
DEPS_FAIL="0"
for DEP in ${DEPENDENCIES[@]}; do
if [[ 0 != "$(command -v "$DEP" >/dev/null ;echo "$?")" ]]; then
err "Missing dependency: $DEP"
DEPS_FAIL=$(($DEPS_FAIL + 1))
fi
done
if [[ "$DEPS_FAIL" > 0 ]]; then
# only warning when --ignore-dependencies is set.
if [[ "$IGNOREDEPENDENCIES" == "true" ]]; then
err "Ignoring missing dependencies (${DEPS_FAIL}), continue..."
else
err "Fix missing dependencies (${DEPS_FAIL}), exit."
usage
exit 1
fi
fi
}
# get mask function for each type of scanner
getMacro(){
# get thumb by layer id
LAYER="$LAYER"
convert "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC" "$MACRO"
}
getMacroiScanHT(){
# layer 8 contains the macro
LAYER=8
convert "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC" "$MACRO"
}
getMacroAperio(){
# the third to last layer contains the macro
NR_LAYERS=`identify "$FILE" | wc -l`
LAYER="$(($NR_LAYERS - 3))"
convert "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC" "$MACRO"
}
getMacroLeica(){
LAYER=5 # determined from example file (http://openslide.cs.cmu.edu/download/openslide-testdata/)
convert "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC" "$MACRO"
}
getMacroHamamatsu(){
LAYER=5 # determined from example file (http://openslide.cs.cmu.edu/download/openslide-testdata/)
convert "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC" "$MACRO"
}
getMacroiScan(){
LAYER=7 # determined from example file (http://openslide.cs.cmu.edu/download/openslide-testdata/)
convert "$FILEFULL[$LAYER]" -resize "${DEFAULT_MACRO_THUMBSIZE}>" -fuzz 1% -transparent "#ECECEC" "$MACRO"
}
# actual program
programOutput(){
# set variables
LAYER="$LAYER"
FILEFULL="$(echo "$(cd "$(dirname "$FILE")"; pwd)"/"$(basename "$FILE")")" # full path $FULL
BASENAME="$(basename "$FILEFULL")" # basename
EXTENSION="${BASENAME##*.}" # extension
FILEPATH="${FILEFULL%.*}" # full path, no extension
FILENAME="${BASENAME%.*}" # filename, no extension"
MASK="${FILEPATH}${MASKSUFFIX}.png"
MACRO="${FILEPATH}${MACROSUFFIX}.png"
# check metadata for scanner data
if [ "$LAYER" != "" ]; then
getMacro # layer is set, do it the easy way
elif [[ "0" < "$(tiffinfo "$FILEFULL" 2>&1 | strings | grep -i "iScanHT" | wc -l | awk '{print $1}' )" ]]; then
getMacroiScanHT # iScanHT found
elif [[ "0" < "$(tiffinfo "$FILEFULL" 2>&1 | strings | grep -i "Aperio" | wc -l | awk '{print $1}')" ]]; then
getMacroAperio # Aperio found
elif [[ "0" < "$(tiffinfo "$FILEFULL" 2>&1 | strings | grep -i "Leica" | wc -l | awk '{print $1}')" ]]; then
getMacroLeica # Leica found
elif [[ "0" < "$(tiffinfo "$FILEFULL" 2>&1 | strings | grep -i "Hamamatsu" | wc -l | awk '{print $1}')" ]]; then
getMacroHamamatsu # Hamamatsu found
elif [[ "0" < "$(tiffinfo "$FILEFULL" 2>&1 | strings | grep -i "iScan" | wc -l | awk '{print $1}')" ]]; then
getMacroiScan # Ventana iScan found
else err "Unknown virtual slide format: $FILEFULL"
err "You probably want to set --layer manually."
err "Run slideInfo for layer information."
usage
exit 1
fi
# what am I doing here?
# create mask, lay it ontop of the macro image.
# a fuzz of 4% works well, but fails on very light slides
# a fuzz of 2% is might by to easy on the selection
# a fuzz of 2.5% is possible (but still to stringend on some slides)
# - we add a temporary border to the image of 100 pixels.
# - The image is blurred, the border will make sure the blur is applied to the borders as well.
# A blur will fill small holes in the tissue and will get rid of dust speckles.
# - Using fuzz, the background is selected and made transparent.
# - the previous added border is removed
# - the alpha makes all transparent parts of the image black, and the rest white.
# - now everything white is converted to transparent.
# - this image is now put on-top of the original one.
# 20220315: There was an update to convert, having to do with the difference
# between IM6 and IM7 causing a (weird) behaviour where the non-masked area is
# completely whitened.
# Obviously we do not want this, this cause the tiling to go all wonky.
# Reference: https://imagemagick.org/script/porting.php
# IF this script is causing problems, it's probably due to this difference.
# THEORETICALLY slideToolKit in combination with CellProfiler 2.2.0, Java 8, Xcode,
# and homebrew should work out-of-the-box without `-channel RGB -negate`. No guarantee though.
#
# Edit to convert command because of change in color-channel usage between IM6 and IM7
# convert "$MACRO" \( "$MACRO" \
# -bordercolor white -border 100x100 \
# $MASKARGS -transparent white \
# -crop +100+100 -crop -100-100 +repage \
# -alpha extract \
# -transparent white \) \
# -flatten "$MASK"
convert "$MACRO" \( "$MACRO" \
-bordercolor white -border 100x100 \
$MASKARGS -transparent white \
-crop +100+100 -crop -100-100 +repage \
-alpha extract \
-transparent white -channel RGB -negate \) \
-flatten "$MASK"
}
# all check?
checkRequirements
checkDependencies
# lets go!
# actual program
programOutput