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

SHARPEDGE


Adaptively sharpens an image near edges.

Download Script

last modified: September 26, 2017



USAGE: sharpedge [-k kind] [-w width] [-m method] [-f factor] [-t threshold] [-b blur] infile outfile
USAGE: sharpedge [-st] infile
USAGE: sharpedge [-h or -help]

-k ............ kind .......... kind of filter; uniform, binomial, gaussian;
............................... default=uniform
-w ............ width ......... width for gaussian type filter only;
............................... See sigma in -blur; float > 0;
............................... default=1
-f ............ factor ........ sharpening gain factor;
............................... factor>=0; float; default=2
-m ............ method ........ method used to threshold edges;
............................... method=0 is grayscale thresholding;
............................... method=1 is binary thresholding;
............................... method=2 is no edge masking
............................... default=0
-t ............ threshold ..... percent threshold value; 0 to 100;
............................... default=25
-b ............ blur .......... edge blurring distance; float >= 0;
............................... default=1
-st ........................... get image statistics
-h or -help ................... get help

PURPOSE: To sharpen an image adaptively near edges.

DESCRIPTION: SHARP is an adaptive technique to sharpen (or blur) an image near edges. It applies an adaptive gain factor based upon the image's local standard deviation to a high pass filtered version of the image and adds that back to the original version of the image. The standard deviation is also thresholded to generate a mask image with is then used to composite the sharpened image near the edges with the original image.

The adaptive sharpening formula is R = I + G*H. Here R is the resulting image, I is the original image and H = (I - M) is the high pass filtered image, which is the original image minus the local mean image. The local mean image is generated by applying a (weighted) averaging convolution to the original image. G is the gain image, which is computed as G = (factor*std)/(S + (factor*std/mg)). Here std is the image's global standard deviation, mg is a maximum gain constant and S is the image's standard deviation in the convolution's local area. Once the sharpened image is extracted, an edge mask is generated from the standard deviation image and the original and sharpened images are composited using the edge mask so that the sharpening occurs only at edges.

ARGUMENTS:

-k kind ... KIND is the kind of weighted averaging filter to use. The choices are uniform (3x3 unweighted average), binomial (3x3 weighted average), gaussian (width determines the local area radius and the weighting coefficient roll-off). See -blur for details on radius and sigma for the gaussian case. The default is simple.

-w width ... WIDTH is the size of the Gaussian filter. It is the sigma for the -blur Gaussian profile convolution. The radius will be about 3 times as big. The default width is 1. In this case, the size of the filter will then be about 3x3 to be consistent with the other two kinds of filters.

-f factor ... FACTOR is the sharpening gain factor. It is a multiplier to the image's actual standard deviation. The value for factor must be greater than or equal to 0. A value of 0 leaves the image unchanged. A larger value sharpens the image. Factor is floating point number. The default=2.

-e edge ... EDGE is the method used to threshold the standard deviation image to get an edge mask image for use in compositing the original and sharpened image. Vaules may be 0, 1 or 2. A value of 0 indicates that thresholding will be done using -black-threshold so that any value below the threshold is black, but values above remain grayscale edges. A value of 1 indicates that thresholding will be done using -threshold to generate a binary edge mask image. A value of 2 indicates that no mask will be used. Thus sharpening will be done throughout the image. Value 0 produces the least edge sharpening for a given value of factor. Value 1 produces a greater amount of edge sharpening for the same value of factor. Value 2 sharpens throughout the image. The default is 0.

-t threshold ... THRESHOLD is the percentage threshold value to use. Values are integers between 0 and 100. The lower the threshold the more edges will be sharpened. Threshold is not needed or ignored for method=2. The default is 25.

-b blur ... BLUR is the edge blurring distance to spread the edges wider by a small amount. Values are floats >= 0. The default is 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


Adaptive Edge Sharpening - Variation In Edge Method

Original Image

Arguments:
-k uniform -e 0 -f 2 -t 25

Arguments:
-k uniform -e 1 -f 2 -t 25

Arguments:
-k uniform -e 2 -f 2



Adaptive Edge Sharpening - Variation In Gain Factor

Original Image

Arguments:
-k uniform -e 0 -f 1 -t 25

Arguments:
-k uniform -e 0 -f 2 -t 25

Arguments:
-k uniform -e 0 -f 3 -t 25



Adaptive Edge Sharpening - Variation Filter Kind

Original Image

Arguments:
-k uniform -e 0 -f 2 -t 25

Arguments:
-k binomial -e 0 -f 2 -t 25

Arguments:
-k gaussian -w 1 -e 0 -f 2 -t 25



What the script does is as follows:

  • Computes a local mean image using a (weighted) average convolution.
  • Computes a local standard deviation image using the input image
    and the local mean image.
  • Thresholds the local standard deviation image to create an edge mask image.
  • Computes a gain image using the local standard deviation image and
    the global standard deviation of the image.
  • Combines the local mean image and the product of the local gain
    image times the difference between the input image and the local
    mean image. This creates an image that is sharpened everywhere.
  • Composites the original image and the sharpened image using the edge
    mask image.

This is equivalent to the following IM commands for
the case of adaptive edge sharpening (-k uniform -e 0 -f 5 -t 25):

  • ave="1,1,1,1,1,1,1,1,1"
  • convert $infile -convolve "$ave" $tmpM
  • convert \( $infile $infile -compose multiply -composite -convolve "$ave" \) \
    \( $tmpM $tmpM -compose multiply -composite \) +swap \
    -compose minus -composite -fx "sqrt(u)" $tmpS
  • convert $tmpS -colorspace Gray -blur 0x1 -contrast-stretch 0% -black-threshold $thresh% $tmp0
  • std=`echo "$data" | sed -n '/^.*Standard.*[(]\([0-9.]*\).*$/{ s//\1/; p; q; }'`
  • std=`echo "scale=1; $std * 100 / 1" | bc`
  • dstd=`echo "scale=5; $factor * $std / 100" | bc`
  • dsdmg=`echo "scale=5; $dstd / $mg" | bc`
  • gain="gn=($dstd)/(u[2]+($dsdmg));"
  • convert $infile $tmpM $tmpS -fx "$gain (u+(gn*(u-v)))" $tmp2
  • convert $infile $tmp2 $tmp1 -composite $outfile