Fred's ImageMagick Scripts



    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.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

FEATHER


Feathers (smoothes) the white-to-black transition in a binary mask image.

Download Script

last modified: September 26, 2017



USAGE: feather [-d distance] infile outfile
USAGE: feather [-h or -help]

-d ..... distance ..... feathering distance; distance>0; default=1

PURPOSE: To feather (smooth) the white-to-black transition in a binary mask image.

DESCRIPTION: FEATHER smoothes the white-to-black transition in a binary mask image so that it can be used to composite one image over another in an antialiased manner. The feathering process makes a ramped grayscale transition over a short distance just inside the white region at the white-to-black transition in the binary mask.

ARGUMENTS:

-d distance ... DISTANCE specifies the distance (in pixels) over which the smoothing occurs at the white-to-black transition in the binary mask. Typically a value of one or two will suffice to anti-alias the rough transition so that the composite formed by using the feathered mask does not show the jagged edge of the overlay image on the background image. The value for distance should be kept small to avoid removing too much from the boundary of the overlay image. The default value is distance=1.

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.


EXAMPLES


Feathering A Binary Mask

Binary Mask

arguments:
-d 1

arguments:
-d 2

arguments:
-d 3



Compositing With Mask On Checker Background
convert {background} {overlay} {mask} -composite {output}

Original Image

Using Binary Mask

Using Feathered Mask
-d 1

Using Feathered Mask
-d 3



Compositing With Mask On A Black Background
convert {background} {overlay} {mask} -composite {output}

Original Image

Using Binary Mask

Using Feathered Mask
-d 1

Using Feathered Mask
-d 3



Mask Creation
(There are many ways to do this - see, for example,
http://www.imagemagick.org/Usage/channels/#mask_creation)

I used two passes of fuzzy floodfill using my magicwand script
as the white background was not evenly white. Two passes were
needed as the area of interest touched the edge of the image.
Alternately, you could add a white border around the outside of
the image, then apply one pass, then remove the added exterior border.

Original Image

magicwand 0,0 -r outside -m binary \
cyclops.png cyclops_mask1.png

magicwand 99,99 -r outside -m binary \
cyclops.png cyclops_mask2.png

convert cyclops_mask1.png cyclops_mask2.png \
-compose multiply -composite cyclops_mask.png



Alternate Mask Creation
(There are many ways to do this - see, for example,
http://www.imagemagick.org/Usage/channels/#mask_creation)

Another way was mostly outline in the reference above using
a thresholded difference image, but followed by my morphology
script to fill interior holes.

Original Image

difference from background
convert cyclops.png \
\( +clone -fx 'p{0,0}' \) \
-compose Difference -composite \
-modulate 100,0 +matte \
cyclops_diff.png

thresholded difference
convert cyclops_diff.png \
-threshold 15% \
cyclops_diff_mask1.png

pad border to avoid bleeding
convert cyclops_diff_mask1.png \
-bordercolor black -border 10 \
cyclops_diff_mask2.png

morphologic close operation
morphology -t close \
cyclops_diff_mask2.png cyclops_diff_mask3.png

remove padding
convert cyclops_diff_mask3.png[100x100+10+10] \
cyclops_diff_mask.png



What the script does is as follows:

  • Blurs the image
  • Removes the half of the blur corresponding to
    where it overlaps into the black area

This is equivalent to the following IM commands for a feathering distance of 1.

  • convert $infile -blur 1 $tmp0
  • convert -size 10x100 gradient: -rotate 90 $tmp1
  • convert $tmp1 -fx "2*(u-.5)" $tmp1
  • convert $tmp0 $tmp1 -clut $outfile