awesome-stickers
awesome-stickers copied to clipboard
Sticker Segmentation
I was looking for a way to segment the stickers into separate images. This script works for https://github.com/securingdev/awesome-stickers/blob/master/raw/attrition_01.pdf
#!/bin/sh
input="$1"
# verbose
set -x
if [ ! -f "$input" ]; then
echo "File not found: $input"
exit 1
fi
background_color=$(convert "$input" -colors 32 -depth 8 -format "%c" histogram:info: | sort -r -n | awk '{print($4); exit;}')
# avoid white
if [ "$background_color" = "white" ]; then
background_color="rgb(254,254,254)"
fi
echo "background_color: $background_color"
convert "$input" +repage -alpha off \
-fuzz 5% -fill "$background_color" -opaque "$background_color" \
+dither -colors 32 \
-morphology dilate octagon \
-fuzz 0 -fill white +opaque "$background_color" -fill black +opaque white \
-type bilevel \
-define connected-components:exclude-header=true \
-define connected-components:verbose=true \
-define connected-components:area-threshold=5000 \
-connected-components 8 -auto-level lumps.png
greys=$(convert lumps.png -colors 255 -depth 8 -format "%c" histogram:info: | awk '!/#000000/{print($3)}')
mask=0
for grey in $greys; do
convert lumps.png -fuzz 1% -fill black +opaque "$grey" -fill white -opaque "$grey" mask_${mask}.png
mask=$((mask + 1))
done
seg=0
rm -f segment_*png
for f in mask_*png; do
convert "$input" "$f" -compose copy-opacity -composite -trim +repage segment_${seg}.png
seg=$((seg + 1))
done
Discussion: https://github.com/ImageMagick/ImageMagick/discussions/2537