Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sticker Segmentation #12

Open
mwarning opened this issue Sep 9, 2020 · 1 comment
Open

Sticker Segmentation #12

mwarning opened this issue Sep 9, 2020 · 1 comment

Comments

@mwarning
Copy link

mwarning commented Sep 9, 2020

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
@mwarning
Copy link
Author

mwarning commented Sep 9, 2020

Discussion: ImageMagick/ImageMagick#2537

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant