Debian Codegraph @

scripts/mia-segment-class-from-stack

raw
1 #!/bin/bash
2 #
3 #
4 # This file is part of MIA - a toolbox for medical image analysis
5 # Copyright (c) Leipzig, Madrid 1999-2015 Gert Wollny
6 #
7 # MIA is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #
20
21
22 out_image_type="png"
23 blob_thresh=100
24 class=2
25 nclasses=3
26 prob_thresh=0.96
27 hist_thresh=1
28 verbosity=message
29
30 print_help() {
31 echo "This script runs the segmentation of one class from a series of gray "
32 echo "scale images. Parameters are"
33 echo " "
34 echo "Usage: mia-segment-class-from-stack [options]"
35 echo " "
36 echo "File IO:"
37 echo " --input Input images (give like e.g. input0000.png), Input images"
38 echo " are expected to be consecutively numbered"
39 echo " --output Output image base name"
40 echo " --output-type Output file type, (default=$out_image_type)"
41 echo " "
42 echo "Parameters:"
43 echo " --blop-thresh threshold for minimum size of blobs to be accepted (default=$blob_thresh)"
44 echo " --prob-thresh class probability to accept as class member (default=$prob_thresh)"
45 echo " --hist-thresh Percentage of pixels to merge into the extreme bins (default=$hist_thresh)"
46 echo " --class class Id to segment"
47 echo " --n-classes number of classes to use in the fuzzy c-means classification (default=$nclasses)"
48 echo " "
49 echo "Info:"
50 echo " --verbose -V verbosity of the output (info|message|warning|error|fatal)"
51 echo " "
52 exit 0
53 }
54
55 #
56 # read the command line
57 #
58 while [ -n "$1" ] ; do
59
60 case "$1" in
61 --blop-thresh)
62 blop_thresh="$2"
63 shift
64 ;;
65
66 --class)
67 class="$2"
68 shift
69 ;;
70
71 --n-classes)
72 nclasses="$2"
73 shift
74 ;;
75
76 --prob-thresh)
77 prob_thresh="$2"
78 shift
79 ;;
80
81 --hist-thresh)
82 hist_thresh="$2"
83 shift
84 ;;
85
86 --input)
87 in_images="$2"
88 shift
89 ;;
90
91 --output)
92 out_images="$2"
93 shift
94 ;;
95
96 --output-type)
97 out_image_type="$2"
98 shift
99 ;;
100
101 --verbose,-V)
102 verbosity="$2"
103 shift
104 ;;
105
106 --help)
107 print_help
108 exit
109 ;;
110
111 *)
112 echo "unknown option '$1' given, run with option --help to see supported options"
113 exit
114 ;;
115
116 esac
117 shift
118 done
119
120 datestr=$(date +%y-%m-%d-%H-%m)
121 temp_dir=$(mktemp -d stackprocess-${datestr}.XXX)
122
123 echo mia-2dstack-cmeans-presegment -i $in_images -o "$temp_dir/class" \
124 -T $hist_thresh \
125 -S $prob_thresh \
126 -C "kmeans:nc=$nclasses" \
127 -L $class -V $verbosity
128
129
130 mia-2dstack-cmeans-presegment -i $in_images -o "$temp_dir/class" \
131 -T $hist_thresh \
132 -S $prob_thresh \
133 -C "kmeans:nc=$nclasses" \
134 -L $class -V $verbosity
135
136 number_pattern=$(mia-filenumberpattern -i "$in_images")
137
138 # label the images
139 mia-2dstackfilter -i "$temp_dir/class${number_pattern}.png" -t v -o "$temp_dir/labeled"\
140 "label:map=$temp_dir/labelmap.txt" -V $verbosity
141
142 #relabel joined labels
143 mia-2dimagefilterstack -i "$temp_dir/labeled${number_pattern}.v" -o $temp_dir/relabeled -t v \
144 "labelmap:map=$temp_dir/labelmap.txt" -V $verbosity
145
146 # evaluate the histogram
147 mia-multihist -i "$temp_dir/relabeled${number_pattern}.v" -o "$temp_dir/labelcount.txt" --max 1000000 --bins 1000000 -V $verbosity
148
149
150 # this can be done with awk
151 echo "MiaLabelmap" > "$temp_dir/labelbinarizationmap.txt"
152 awk -v thresh=$blob_thresh <"$temp_dir/labelcount.txt" \
153 '{if ($2 > thresh){ print $1 " 1"}else{ print $1 " 0"} }' \
154 >> "$temp_dir/labelbinarizationmap.txt"
155
156 mia-2dimagefilterstack -i "$temp_dir/relabeled${number_pattern}.v" -o "$temp_dir/inverse" -t v \
157 "labelmap:map=$temp_dir/labelbinarizationmap.txt" \
158 binarize:min=1 invert -V $verbosity
159
160 mia-2dstackfilter -i "$temp_dir/inverse${number_pattern}.v" -t v \
161 -o "$temp_dir/ilabeled" "label:map=$temp_dir/ilabelmap.txt" -V $verbosity
162
163 mia-2dimagefilterstack -i "$temp_dir/ilabeled${number_pattern}.v" -o $temp_dir/irelabeled -t v \
164 "labelmap:map=$temp_dir/ilabelmap.txt" -V $verbosity
165
166 mia-multihist -i "$temp_dir/irelabeled${number_pattern}.v" -o "$temp_dir/ilabelcount.txt" \
167 -V $verbosity --max 1000000 --bins 1000000
168
169 echo "MiaLabelmap" > "$temp_dir/ilabelbinarizationmap.txt"
170 awk -v thresh=$thresh <"$temp_dir/ilabelcount.txt" \
171 '{if ($2 > thresh){ print $1 " 1"}else{ print $1 " 0"} }'\
172 >> "$temp_dir/ilabelbinarizationmap.txt"
173
174 mia-2dimagefilterstack -i "$temp_dir/irelabeled${number_pattern}.v" -o "$out_images" -t png \
175 "labelmap:map=$temp_dir/ilabelbinarizationmap.txt" binarize:min=1 invert -V $verbosity
176
177 #rm -rf $temp_dir
178
179