-
Notifications
You must be signed in to change notification settings - Fork 3
/
morphology.sh
1027 lines (985 loc) · 30.3 KB
/
morphology.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# Modified by Fred Weinhaus 4/2/2009 ............ revised 4/25/2015
# Enhanced by Anthony Thyssen ................... revised 1/8/2009
# Developed by Fred Weinhaus 11/5/2007 .......... revised 4/25/2015
#
# ------------------------------------------------------------------------------
#
# Licensing:
#
# Copyright © Fred Weinhaus
#
# My scripts are available free of charge for non-commercial use, ONLY.
#
# For use of my scripts in commercial (for-profit) environments or
# non-free applications, please contact me (Fred Weinhaus) for
# licensing arrangements. My email address is fmw at alink dot net.
#
# If you: 1) redistribute, 2) incorporate any of these scripts into other
# free applications or 3) reprogram them in another scripting language,
# then you must contact me for permission, especially if the result might
# be used in a commercial or for-profit environment.
#
# My scripts are also subject, in a subordinate manner, to the ImageMagick
# license, which can be found at: http://www.imagemagick.org/script/license.php
#
# ------------------------------------------------------------------------------
#
####
#
# USAGE: morphology [-m mode] [-t type] [-i iterations] [-r ramp] infile outfile
# USAGE: morphology [-h or -help]
#
# OPTIONS:
# -m mode binary or grayscale morphology; default=binary
# -t type dilate, erode, open, close, majority, edgein,
# edgeout, gradient, feather, average, spread, tophat,
# bottomhat, thin, prune, thicken; default=close
# -i iterations number of iterations to perform; default=1 for
# all but thin, prune and thicken for which the
# default is to iterate until convergence.
# -r ramp ramp distance; for type=feather, the white areas
# of a binary image will be ramped linearly to
# black from ramp distance inside the white area up
# to the black edge; ramp>=0; default=0
#
###
#
# NAME: MORPHOLOGY
#
# PURPOSE: To perform binary or grayscale morphologic operations on an image,
# including dilate, erode, open and close.
#
# DESCRIPTION: MORPHOLOGY applies any one of the following morphologic
# operations to an image: dilate, erode, open or close, and other similar
# types of operations, depending on the values in a 3x3 neighborhood of each
# pixel in the image. The dilate operation returns the maximum value in the
# neighborhood. The erode operation returns the minimum value in the
# neighborhood. The open operation performs a dilate followed by an erode.
# The close operations performs an erode followed by a dilate. If the mode
# is set to binary, the script assumes the input image is binary
# (thresholded to black and white) and applies a much more efficient
# algorithm.
#
# OPTIONS:
#
# -m mode ... MODE specifies the algorithmic approach to use. The allowed
# values are binary and grayscale. If mode is set to binary, the script
# assumes the input image will be binary (thresholded to 0 and max quantum
# value) and will apply a more efficient algorithm. If mode is set to
# grayscale, the script will apply a slightly less efficient algorithm that
# can be used on grayscale or color images. The default is binary.
#
# -t type ... TYPE specifies the morphologic technique to apply to the image.
# The morphological operators: dilate, erode, open, close, tophat, bottomhat,
# and gradient can be used in any mode. For 'binary' mode you can also
# use edgein, edgeout, majority, feather, thin, prune and thicken.
# For 'grayscale' mode you can also use average and spread.
#
# The dilate operation returns the maximum value in the neighborhood.
#
# The erode operation returns the minimum value in the neighborhood.
#
# The open operation performs a dilate followed by an erode.
#
# The close operation performs an erode followed by a dilate.
#
# The tophat operation is the result produced from the open operation
# subtracted from the original image.
#
# The bottomhat operation is the result produced from the close operation
# subtracted by the original image.
#
# The gradient operation is the result produced by subtracting the erode
# result from the dilate result.
#
# The edgein operation return the interior edge at the transition between
# white and black. (Binary mode only)
#
# The edgeout operation return the exterior edge at the transition between
# white and black. (Binary mode only)
#
# The majority operation just selects the majority from all the neighborhood
# values.
#
# The thin operation strips the outer layers of the white areas and if
# iterated until convergence produces a skeleton. (Binary mode only)
#
# The prune operation is generally used to remove spurs left from the
# thinning operation. It is typically used only a specific number of iterations.
# If iterated until convergence, it will produce a totally black image unless
# the thinned white area is a closed curve. (Binary mode only)
#
# The thicken operation produces a 45 degree convex hull, if continued to
# convergence. (Binary mode only)
#
# The feather operation will linearly ramp from white to black, but just
# within the white area. Only one iteration is allowed in this case.
# (Binary mode only, but produces a grayscale result)
#
# The average operation generates a simple average of the neighborhood.
# (Grayscale mode only, but works on color images, also)
#
# The spread operation does the same thing as average, but assumes the input
# image has an alpha channel and resets pixels with a defined color back to
# their original color after each iteration. That is, it spreads the color
# in an average way out into the transparent areas of the image.
# (Grayscale mode only, but works on color images, also)
#
# When the input is a binary image, dilate/erode will add/remove white
# pixels at all transitions from white to black. Similarly, open/close will
# make interior black areas larger or smaller, but keep the overall outer
# shape about the same.
#
# When the input image is grayscale or color, dilate/erode and close/open
# will brighten/darken the color transitions.
#
# -i iterations ... ITERATIONS specifies the number of iterations to apply for
# the specified technique. Note that for open and close, the iterations are
# applied successively to the first erode or dilate and then repeated
# successively for the second dilate or erode. Iteration is ignored for
# binary mode edgein and edgeout. The default is 1 for all but thin, prune
# and thicken, which run until convergence as a default.
#
# -r ramp ... RAMP distance for type=feather only. The white areas of a
# binary image will be linearly ramped to black from ramp distance inside
# the white area up to the black edge. Ramp is a float>=0; The default=0.
#
# NOTE: The thin, prune and thicken operations can be slow due to each
# iteration containing 8 complex operations.
#
# References:
# http://homepages.inf.ed.ac.uk/rbf/HIPR2/morops.htm
# http://www.naun.org/journals/computers/ijcomputers-11.pdf
# http://www.ene.unb.br/~juliana/cursos/pimagens/chapter6.pdf
#
# CAVEAT: No guarantee that this script will work on all platforms, nor that
# trapping of inconsistent parameters is complete and foolproof. Use At Your
# Own Risk.
#
# ENHANCEMENTS:
# Anthony Thyssen 1/8/2009: speed and added types=average and spread
# Fred Weinhaus 4/2/2009: added type=feather
# Fred Weinhaus 4/14/2009: added type=tophat,bottomhat,thin,prune,thicken
# Fred Weinhaus 4/16/2009: added type=gradient
#
######
#
# set default values
mode="binary" # binary or grayscale
type="open" # dilate, erode, open, close, majority, edgein, edgeout, average, spread, tophat, bottomhat, thin, prune, thicken
iter="" # initialize and determine default of 1 or convergence later
ramp=0 # ramp distance for type=feather
#rmse=1000000 # init rmse so won't stop on first iteration
# set directory for temporary folder and files
dir="/tmp" # suggestions are dir="." or dir="/tmp"
# set up functions to report Usage and Usage with Description
PROGNAME=`type $0 | awk '{print $3}'` # search for executable on path
PROGDIR=`dirname $PROGNAME` # extract directory of program
PROGNAME=`basename $PROGNAME` # base name of program
usage()
{
echo >&2 ""
echo >&2 "$PROGNAME:" "$@"
sed >&2 -e '1,/^####/d; /^###/g; /^#/!q; s/^#//; s/^ //; 4,$p' "$PROGDIR/$PROGNAME"
}
usage_verbose()
{
echo >&2 ""
echo >&2 "$PROGNAME:" "$@"
sed >&2 -e '1,/^####/d; /^######/g; /^#/!q; s/^#*//; s/^ //; 4,$p' "$PROGDIR/$PROGNAME"
}
# function to report error messages
errMsg()
{
echo ""
echo $1
usage
exit 1
}
# function to test for minus at start of value of second part of option 1 or 2
checkMinus()
{
test=`echo "$1" | grep -c '^-.*$'` # returns 1 if match; 0 otherwise
[ $test -eq 1 ] && errMsg "$errorMsg"
}
# test for correct number of arguments and get values
if [ $# -eq 0 ]; then
# help information
echo ""
usage_verbose
exit 0
elif [ $# -gt 8 ]; then
errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---"
else
while [ $# -gt 0 ]; do
# get parameter values
case "$1" in
-h|-help) # help information
usage_verbose
exit 0
;;
-m) # mode
shift # to get the next parameter - mode
# test if parameter starts with minus sign
errorMsg="--- INVALID MODE SPECIFICATION ---"
checkMinus "$1"
# test mask values
mode="$1"
mode=`echo "$mode" | tr "[:upper:]" "[:lower:]"`
[ "$mode" != "binary" -a "$mode" != "grayscale" ] &&
errMsg "--- MODE=$mode IS NOT A VALID VALUE ---"
;;
-t) # type
shift # to get the next parameter - type
# test if parameter starts with minus sign
errorMsg="--- INVALID TYPE SPECIFICATION ---"
checkMinus "$1"
# test region values
type="$1"
type=`echo "$type" | tr "[:upper:]" "[:lower:]"`
case "$type" in
erode|dilate|open|close|majority|feather|edgein|edgeout|average|spread|tophat|bottomhat|thin|prune|thicken|gradient) ;;
*) errMsg "--- TYPE=$type IS NOT A VALID VALUE ---" ;;
esac
;;
-i) # get iterations
shift # to get the next parameter - opacity
# test if parameter starts with minus sign
errorMsg="--- INVALID ITERATIONS SPECIFICATION ---"
checkMinus "$1"
# test width values
iter=`expr "$1" : '\([0-9]*\)'`
[ "$iter" = "" ] &&
errMsg "ITERATIONS=$iter IS NOT A NON-NEGATIVE INTEGER"
itertest=`echo "$iter < 1" | bc`
[ $itertest -eq 1 ] &&
errMsg "--- ITERATIONS=$iter MUST BE A POSITIVE INTEGER ---"
;;
-r) # get ramp distance
shift # to get the next parameter - ramp
# test if parameter starts with minus sign
errorMsg="--- INVALID RAMP SPECIFICATION ---"
checkMinus "$1"
# test width values
ramp=`expr "$1" : '\([.0-9]*\)'`
[ "$ramp" = "" ] &&
errMsg "RAMP=$ramp IS NOT A NON-NEGATIVE INTEGER"
;;
-) # STDIN, end of arguments
break
;;
-*) # any other - argument
errMsg "--- UNKNOWN OPTION ---"
;;
*) # end of arguments
break
;;
esac
shift # next option
done
#
# get infile and outfile
infile="$1"
outfile="$2"
fi
# test that infile provided
[ "$infile" = "" ] && errMsg "NO INPUT FILE SPECIFIED"
# test that outfile provided
[ "$outfile" = "" ] && errMsg "NO OUTPUT FILE SPECIFIED"
tmpdir="$dir/$PROGNAME.$$"
mkdir "$tmpdir" || errMsg "Failed to create temporary file directory"
TMP="$tmpdir/morphology_$$.mpc"
TMPORIG="$tmpdir/morphology_orig_$$.mpc"
TMPOLD="$tmpdir/morphology_old_$$.mpc"
trap "rm -rf $tmpdir;" 0
trap "rm -ff $tmpdir; exit 1" 1 2 3 15
trap "rm -ff $tmpdir; exit 1" ERR
# read the input image into the TMP cached image.
convert -quiet "$infile" +repage "$TMP" ||
errMsg "--- FILE $infile NOT READABLE OR HAS ZERO SIZE ---"
# set default for iteration depending upon type
if [ "$iter" != "" ]; then
iter=$iter
elif [ "$iter" = "" -a "$type" != "thin" -a "$type" != "prune" -a "$type" != "thicken" ]; then
iter=1
else
iter=1000000
fi
# shifted images
shifted="$tmpdir/shifted"
shift1="$shifted-1.png"
shift2="$shifted-2.png"
shift3="$shifted-3.png"
shift4="$shifted-4.png"
shift5="$shifted-5.png"
shift6="$shifted-6.png"
shift7="$shifted-7.png"
shift8="$shifted-8.png"
# function to create 8 1-pixel shifted images in each direction from center
grav1="NorthWest"
grav2="North"
grav3="NorthEast"
grav4="West"
grav5="East"
grav6="SouthWest"
grav7="South"
grav8="SouthEast"
shift_size=`identify -format '%[fx:w-2]x%[fx:h-2]+1+1' $TMP`
create_shifted_images() {
# create 8 shifted images from the given image
for ((j=1; j<=8; j++)); do
eval grav=\$grav$j
eval img=\$shift$j
convert "$1" "$1"[${shift_size}] -gravity $grav -composite $img
done
}
# process image
if [ $iter -eq 1000000 ]; then
convert $TMP $TMPOLD
fi
if [ "$mode" = "binary" ]; then
# Binary images can be processed using a fast convolution method
if [ "$type" = "dilate" ]; then
for ((i=1; i<=$iter; i++)); do
convert $TMP -define convolve:scale=\! -convolve "1,1,1,1,1,1,1,1,1" -threshold 0 $TMP
done
elif [ "$type" = "erode" ]; then
for ((i=1; i<=$iter; i++)); do
convert $TMP -define convolve:scale=\! -convolve "1,1,1,1,1,1,1,1,1" -threshold 99% $TMP
done
elif [ "$type" = "close" -o "$type" = "bottomhat" ]; then
if [ "$type" = "bottomhat" ]; then
convert $TMP $TMPOLD
fi
for ((i=1; i<=$iter; i++)); do
convert $TMP -define convolve:scale=\! -convolve "1,1,1,1,1,1,1,1,1" -threshold 0 $TMP
done
for ((i=1; i<=$iter; i++)); do
convert $TMP -define convolve:scale=\! -convolve "1,1,1,1,1,1,1,1,1" -threshold 99% $TMP
done
if [ "$type" = "bottomhat" ]; then
convert $TMP $TMPOLD +swap -compose minus -composite $TMP
fi
elif [ "$type" = "open" -o "$type" = "tophat" ]; then
if [ "$type" = "tophat" ]; then
convert $TMP $TMPOLD
fi
for ((i=1; i<=$iter; i++)); do
convert $TMP -define convolve:scale=\! -convolve "1,1,1,1,1,1,1,1,1" -threshold 99% $TMP
done
for ((i=1; i<=$iter; i++)); do
convert $TMP -define convolve:scale=\! -convolve "1,1,1,1,1,1,1,1,1" -threshold 0 $TMP
done
if [ "$type" = "tophat" ]; then
convert $TMPOLD $TMP +swap -compose minus -composite $TMP
fi
elif [ "$type" = "gradient" ]; then
convert $TMP $TMPORIG
# dilate
for ((i=1; i<=$iter; i++)); do
convert $TMP -define convolve:scale=\! -convolve "1,1,1,1,1,1,1,1,1" -threshold 0 $TMP
done
convert $TMP $TMPOLD
convert $TMPORIG $TMP
# erode
for ((i=1; i<=$iter; i++)); do
convert $TMP -define convolve:scale=\! -convolve "1,1,1,1,1,1,1,1,1" -threshold 99% $TMP
done
# dilate - erode
convert $TMPOLD $TMP +swap -compose minus -composite $TMP
elif [ "$type" = "majority" ]; then
for ((i=1; i<=$iter; i++)); do
convert $TMP -define convolve:scale=\! -convolve "1,1,1,1,1,1,1,1,1" -threshold 50% $TMP
done
elif [ "$type" = "feather" ]; then
# note only one iteration allowed
if [ "$ramp" != "0" -a "$ramp" != "0.0" ]; then
convert $TMP -blur ${ramp}x65535 -black-threshold 50% $TMP
else
errMsg "--- RAMP FOR FEATHERING MUST BE GREATER THAN 0 ---"
fi
elif [ "$type" = "edgeout" ]; then
convert $TMP \( +clone -define convolve:scale=\! -convolve "1,1,1,1,1,1,1,1,1" -threshold 0 \) \
-compose difference -composite -threshold 99% $TMP
elif [ "$type" = "edgein" ]; then
convert $TMP \( +clone -define convolve:scale=\! -convolve "1,1,1,1,1,1,1,1,1" -threshold 99% \) \
-compose difference -composite -threshold 99% $TMP
elif [ "$type" = "thin" ]; then
echo ""
i=1
until [ "$rmse" = "0" -o $i -gt $iter ]; do
echo "iteration=$i"
# struct1
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 0 \) \
\( $shift2 -evaluate xor 0 \) \
\( $shift3 -evaluate xor 0 \) \
\( $TMP -evaluate xor 100% \) \
\( $shift6 -evaluate xor 100% \) \
\( $shift7 -evaluate xor 100% \) \
\( $shift8 -evaluate xor 100% \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
# struct2
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift2 -evaluate xor 0 \) \
\( $shift3 -evaluate xor 0 \) \
\( $shift4 -evaluate xor 100% \) \
\( $TMP -evaluate xor 100% \) \
\( $shift5 -evaluate xor 0 \) \
\( $shift7 -evaluate xor 100% \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
# struct3
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 100% \) \
\( $shift3 -evaluate xor 0 \) \
\( $shift4 -evaluate xor 100% \) \
\( $TMP -evaluate xor 100% \) \
\( $shift5 -evaluate xor 0 \) \
\( $shift6 -evaluate xor 100% \) \
\( $shift8 -evaluate xor 0 \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
# struct4
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift2 -evaluate xor 100% \) \
\( $shift4 -evaluate xor 100% \) \
\( $TMP -evaluate xor 100% \) \
\( $shift5 -evaluate xor 0 \) \
\( $shift7 -evaluate xor 0 \) \
\( $shift8 -evaluate xor 0 \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
# struct5
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 100% \) \
\( $shift2 -evaluate xor 100% \) \
\( $shift3 -evaluate xor 100% \) \
\( $TMP -evaluate xor 100% \) \
\( $shift6 -evaluate xor 0 \) \
\( $shift7 -evaluate xor 0 \) \
\( $shift8 -evaluate xor 0 \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
# struct6
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift2 -evaluate xor 100% \) \
\( $shift4 -evaluate xor 0 \) \
\( $TMP -evaluate xor 100% \) \
\( $shift5 -evaluate xor 100% \) \
\( $shift6 -evaluate xor 0 \) \
\( $shift7 -evaluate xor 0 \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
# struct7
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 0 \) \
\( $shift3 -evaluate xor 100% \) \
\( $shift4 -evaluate xor 0 \) \
\( $TMP -evaluate xor 100% \) \
\( $shift5 -evaluate xor 100% \) \
\( $shift6 -evaluate xor 0 \) \
\( $shift8 -evaluate xor 100% \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
# struct8
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 0 \) \
\( $shift2 -evaluate xor 0 \) \
\( $shift4 -evaluate xor 0 \) \
\( $TMP -evaluate xor 100% \) \
\( $shift5 -evaluate xor 100% \) \
\( $shift7 -evaluate xor 100% \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
if [ $iter -eq 1000000 ]; then
rmse=`compare -metric rmse $TMP $TMPOLD null: 2>&1`
rmse=`echo "$rmse" | cut -d\ -f1`
echo "rmse=$rmse"
convert $TMP $TMPOLD
fi
i=`expr $i + 1`
done
echo ""
elif [ "$type" = "prune" ]; then
echo ""
i=1
until [ "$rmse" = "0" -o $i -gt $iter ]; do
echo "iteration=$i"
# struct1
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 0 \) \
\( $shift2 -evaluate xor 0 \) \
\( $shift3 -evaluate xor 0 \) \
\( $shift4 -evaluate xor 0 \) \
\( $TMP -evaluate xor 100% \) \
\( $shift5 -evaluate xor 0 \) \
\( $shift6 -evaluate xor 0 \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
# struct2
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 0 \) \
\( $shift2 -evaluate xor 0 \) \
\( $shift3 -evaluate xor 0 \) \
\( $shift4 -evaluate xor 0 \) \
\( $TMP -evaluate xor 100% \) \
\( $shift5 -evaluate xor 0 \) \
\( $shift8 -evaluate xor 0 \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
# struct3
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 0 \) \
\( $shift2 -evaluate xor 0 \) \
\( $shift3 -evaluate xor 0 \) \
\( $TMP -evaluate xor 100% \) \
\( $shift5 -evaluate xor 0 \) \
\( $shift7 -evaluate xor 0 \) \
\( $shift8 -evaluate xor 0 \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
# struct4
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift2 -evaluate xor 0 \) \
\( $shift3 -evaluate xor 0 \) \
\( $TMP -evaluate xor 100% \) \
\( $shift5 -evaluate xor 0 \) \
\( $shift6 -evaluate xor 0 \) \
\( $shift7 -evaluate xor 0 \) \
\( $shift8 -evaluate xor 0 \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
# struct5
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift3 -evaluate xor 0 \) \
\( $shift4 -evaluate xor 0 \) \
\( $TMP -evaluate xor 100% \) \
\( $shift5 -evaluate xor 0 \) \
\( $shift6 -evaluate xor 0 \) \
\( $shift7 -evaluate xor 0 \) \
\( $shift8 -evaluate xor 0 \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
# struct6
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 0 \) \
\( $shift4 -evaluate xor 0 \) \
\( $TMP -evaluate xor 100% \) \
\( $shift5 -evaluate xor 0 \) \
\( $shift6 -evaluate xor 0 \) \
\( $shift7 -evaluate xor 0 \) \
\( $shift8 -evaluate xor 0 \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
# struct7
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 0 \) \
\( $shift2 -evaluate xor 0 \) \
\( $shift4 -evaluate xor 0 \) \
\( $TMP -evaluate xor 100% \) \
\( $shift6 -evaluate xor 0 \) \
\( $shift7 -evaluate xor 0 \) \
\( $shift8 -evaluate xor 0 \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
# struct8
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 0 \) \
\( $shift2 -evaluate xor 0 \) \
\( $shift3 -evaluate xor 0 \) \
\( $shift4 -evaluate xor 0 \) \
\( $TMP -evaluate xor 100% \) \
\( $shift6 -evaluate xor 0 \) \
\( $shift7 -evaluate xor 0 \) \
-compose plus -flatten \) \
-compose multiply -composite $TMP
if [ $iter -eq 1000000 ]; then
rmse=`compare -metric rmse $TMP $TMPOLD null: 2>&1`
rmse=`echo "$rmse" | cut -d\ -f1`
echo "rmse=$rmse"
convert $TMP $TMPOLD
fi
i=`expr $i + 1`
done
echo ""
elif [ "$type" = "thicken" ]; then
echo ""
i=1
until [ "$rmse" = "0" -o $i -gt $iter ]; do
echo "iteration=$i"
# struct1
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 100% \) \
\( $shift2 -evaluate xor 100% \) \
\( $shift4 -evaluate xor 100% \) \
\( $TMP -evaluate xor 0 \) \
\( $shift6 -evaluate xor 100% \) \
\( $shift8 -evaluate xor 0 \) \
-compose plus -flatten -negate \) \
-compose plus -composite $TMP
# struct2
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift2 -evaluate xor 100% \) \
\( $shift3 -evaluate xor 100% \) \
\( $TMP -evaluate xor 0 \) \
\( $shift5 -evaluate xor 100% \) \
\( $shift6 -evaluate xor 0 \) \
\( $shift8 -evaluate xor 100% \) \
-compose plus -flatten -negate \) \
-compose plus -composite $TMP
# struct3
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 100% \) \
\( $shift2 -evaluate xor 100% \) \
\( $shift3 -evaluate xor 100% \) \
\( $TMP -evaluate xor 0 \) \
\( $shift5 -evaluate xor 100% \) \
\( $shift6 -evaluate xor 0 \) \
-compose plus -flatten -negate \) \
-compose plus -composite $TMP
# struct4
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 0 \) \
\( $TMP -evaluate xor 0 \) \
\( $shift5 -evaluate xor 100% \) \
\( $shift6 -evaluate xor 100% \) \
\( $shift7 -evaluate xor 100% \) \
\( $shift8 -evaluate xor 100% \) \
-compose plus -flatten -negate \) \
-compose plus -composite $TMP
# struct5
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 0 \) \
\( $shift3 -evaluate xor 100% \) \
\( $TMP -evaluate xor 0 \) \
\( $shift5 -evaluate xor 100% \) \
\( $shift7 -evaluate xor 100% \) \
\( $shift8 -evaluate xor 100% \) \
-compose plus -flatten -negate \) \
-compose plus -composite $TMP
# struct6
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 100% \) \
\( $shift3 -evaluate xor 0 \) \
\( $shift4 -evaluate xor 100% \) \
\( $TMP -evaluate xor 0 \) \
\( $shift6 -evaluate xor 100% \) \
\( $shift7 -evaluate xor 100% \) \
-compose plus -flatten -negate \) \
-compose plus -composite $TMP
# struct7
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift3 -evaluate xor 0 \) \
\( $shift4 -evaluate xor 100% \) \
\( $TMP -evaluate xor 0 \) \
\( $shift6 -evaluate xor 100% \) \
\( $shift7 -evaluate xor 100% \) \
\( $shift8 -evaluate xor 100% \) \
-compose plus -flatten -negate \) \
-compose plus -composite $TMP
# struct8
create_shifted_images $TMP
convert $TMP \
\( -background black \
\( $shift1 -evaluate xor 100% \) \
\( $shift2 -evaluate xor 100% \) \
\( $shift3 -evaluate xor 100% \) \
\( $shift4 -evaluate xor 100% \) \
\( $TMP -evaluate xor 0 \) \
\( $shift8 -evaluate xor 0 \) \
-compose plus -flatten -negate \) \
-compose plus -composite $TMP
if [ $iter -eq 1000000 ]; then
rmse=`compare -metric rmse $TMP $TMPOLD null: 2>&1`
rmse=`echo "$rmse" | cut -d\ -f1`
echo "rmse=$rmse"
convert $TMP $TMPOLD
fi
i=`expr $i + 1`
done
echo ""
fi
elif [ "$mode" = "grayscale" ]; then
# Grayscale images (only some method types) requires a more complex
# comparsion convolution method
if [ "$type" = "dilate" ]; then
for ((i=1; i<=$iter; i++)); do
# DILATE: maximum value convolution - shift and compare
create_shifted_images $TMP
convert $TMP $shifted-* \
-background '#0000' -compose lighten -flatten $TMP
done
elif [ "$type" = "erode" ]; then
for ((i=1; i<=$iter; i++)); do
# ERODE: minimum value convolution - shift and compare
create_shifted_images $TMP
convert $TMP $shifted-* \
-background '#FFF0' -compose darken -flatten $TMP
done
elif [ "$type" = "close" -o "$type" = "bottomhat" ]; then
if [ "$type" = "bottomhat" ]; then
convert $TMP $TMPOLD
fi
for ((i=1; i<=$iter; i++)); do
# DILATE: maximum value convolution - shift and compare
create_shifted_images $TMP
convert $TMP $shifted-* \
-background '#0000' -compose lighten -flatten $TMP
done
for ((i=1; i<=$iter; i++)); do
# ERODE: minimum value convolution - shift and compare
create_shifted_images $TMP
convert $TMP $shifted-* \
-background '#FFF0' -compose darken -flatten $TMP
done
if [ "$type" = "bottomhat" ]; then
convert $TMP $TMPOLD -alpha off +swap -compose minus -composite $TMP
fi
elif [ "$type" = "open" -o "$type" = "tophat" ]; then
if [ "$type" = "tophat" ]; then
convert $TMP $TMPOLD
fi
for ((i=1; i<=$iter; i++)); do
# ERODE: minimum value convolution - shift and compare
create_shifted_images $TMP
convert $TMP $shifted-* \
-background '#FFF0' -compose darken -flatten $TMP
done
for ((i=1; i<=$iter; i++)); do
# DILATE: maximum value convolution - shift and compare
create_shifted_images $TMP
convert $TMP $shifted-* \
-background '#0000' -compose lighten -flatten $TMP
done
if [ "$type" = "tophat" ]; then
convert $TMPOLD $TMP -alpha off +swap -compose minus -composite $TMP
fi
elif [ "$type" = "gradient" ]; then
convert $TMP $TMPORIG
# dilate
for ((i=1; i<=$iter; i++)); do
# DILATE: maximum value convolution - shift and compare
create_shifted_images $TMP
convert $TMP $shifted-* \
-background '#0000' -compose lighten -flatten $TMP
done
convert $TMP $TMPOLD
convert $TMPORIG $TMP
# erode
for ((i=1; i<=$iter; i++)); do
# ERODE: minimum value convolution - shift and compare
create_shifted_images $TMP
convert $TMP $shifted-* \
-background '#FFF0' -compose darken -flatten $TMP
done
# dilate - erode
convert $TMPOLD $TMP -alpha off +swap -compose minus -composite $TMP
elif [ "$type" = "average" ]; then
for ((i=1; i<=$iter; i++)); do
# AVERAGE blur the pixels.
convert $TMP -channel RGBA -blur 2x65000 $TMP
done
elif [ "$type" = "spread" ]; then
for ((i=1; i<=$iter; i++)); do
# SPREAD the pixel colors from the image edges into the transparent parts
convert $TMP \( +clone -channel RGBA -blur 2x65000 \) \
-compose DstOver -composite $TMP
done
else
errMsg "--- INVALID TYPE FOR GRAYSCALE MODE ---"
fi
else
# This actually should be not possible, sanity check
errMsg "--- INVALID MODE (BINARY OR GRAYSCALE) ---"
fi
# Final Output
convert $TMP "$outfile"
exit 0
# Structure Elements
#
# Thin
#
# see http://homepages.inf.ed.ac.uk/rbf/HIPR2/thin.htm
# if pattern matches change center pixel to 0
#
# struct1
# 0 0 0
# x 1 x
# 1 1 1
#
# struct2
# x 0 0
# 1 1 0
# x 1 x
#
# struct3
# 1 x 0
# 1 1 0
# 1 x 0
#
# struct4
# x 1 x
# 1 1 0
# x 0 0
#
# struct5
# 1 1 1
# x 1 x
# 0 0 0
#
# struct6
# x 1 x
# 0 1 1
# 0 0 x
#
# struct7
# 0 x 1
# 0 1 1
# 0 x 1
#
# struct8
# 0 0 x
# 0 1 1
# x 1 x
#
#
# Prune
#
# see http://homepages.inf.ed.ac.uk/rbf/HIPR2/thin.htm
# if pattern matches change center pixel to 0
#
# struct1
# 0 0 0
# 0 1 0
# 0 x x
#
# struct2
# 0 0 0
# 0 1 0
# x x 0
#
# struct3
# 0 0 0
# x 1 0
# x 0 0
#
# struct4
# X 0 0
# X 1 0
# 0 0 0
#
# struct5
# x x 0
# 0 1 0
# 0 0 0
#
# struct6
# 0 x x
# 0 1 0
# 0 0 0
#
# struct7
# 0 0 x
# 0 1 x
# 0 0 0
#
# struct8
# 0 0 0
# 0 1 x
# 0 0 x
#
#
# Thicken
#
# see http://homepages.inf.ed.ac.uk/rbf/HIPR2/thick.htm
# if pattern matches change center pixel to 1
#
# struct1
# 1 1 x
# 1 0 x
# 1 x 0
#
# struct2
# x 1 1
# x 0 1
# 0 x 1
#
# struct3
# 1 1 1