Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial (non-profit) 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

    Please read the Pointers For Use on my home page to properly install and customize my scripts.

SPACE2


Adaptively enhances the contrast and brightness of an image.

Download Script

last modified: December 15, 2018



USAGE: space2 [-a autolevel] [-b bri] [-c con] [-s sat] [-m maxgain ] [-d] [-w window ] infile outfile
USAGE: space2 [-h or -help]

-a ... autolevel ... autolevel as preprocessing; options are:
.................... off (o), together (t), separately (s);
.................... default=together
-b ... bri ......... brightness adjustment factor; float>0;
.................... default=1 (no change)
-c ... con ......... contrast adjustment factor; float>0;
.................... default=1 (no change)
-s ... sat ......... saturation adjustment percent; float;
.................... -45<=sat<=45; default is no change
-m ... maxgain ..... maximum gain; controls amount of detail;
.................... float>0; default=2.5
.................... larger values will allow more detail;
-d ................. set the desired standard deviation (dstd)
.................... to an internally set constant value;
.................... default is to use the image's actual std
-w ... window ... .. windowing size as percentage of image;
.................... nominally between 5 and 20; 2<=float<=50;
.................... default=8
-h or -help ........ get help

PURPOSE: To change the contrast and/or brightness in an image adaptively.

DESCRIPTION: SPACE2 is an adaptive technique to enhance an image's brightness and contrast. SPACE is an abbreviation for SPacially Adaptive Contrast Enhancement.

The adaptive formula R = M + G*(I-M). Here R is the resulting image. I is the input image. M is a mean image, which is a low pass filtered version of the input image. It is generated by a block resize of the input image to some fraction of the input size. The resize amount is computed from the window size. This image is then re-expanded to its original size. The resizing technique is fast way to apply a large block size moving window average. The term (I-M) is a high pass filtered version of the input image. G is a gain image, which involves S, the standard deviation of the input image, generated by the same resizing technique as used to create the mean image, M. G also includes the desired standard deviation (dstd) and a max gain factor. The gain image, G, is used to set the amount of detail (sharpness) in the output as well as to limit runaway gain. Max gain is typically on the order of 1-10, with a default of 2.5. The block window size typically is on the order of 5-20% of the image size and nominally 8%. There are times when one may want to push the max gain value higher than the default, for example, when trying to pull information out of a hazy picture. The algorithm has options to control the brightness, contrast and saturation. The brightness and contrast have automatically computed defaults that are image dependent. They are applied via sigmoidal non-linear functions. The saturation is a linear adjustment in LAB colorspace and the default is no change.

ARGUMENTS:

-a autolevel --- autolevel as preprocessing step. The options are: off (o), together (t), separately (s). The default=together. Note, for some images, especially with haze, the option separately can cause severe color shifts.

-b bri --- bri is a brightness adjustment factor multiplied by the automatically computed default. It is a float>0. Values larger then 1 will increase the brightness and values smaller than 1 will decrease the brightness. The default is 1 or no change from the automatically computed value.

-c con --- con is a contrast adjustment factor multiplied by the automatically computed default. It is a float>0. Values larger then 1 will increase the contrast and values smaller than 1 will decrease the contrast. The default is 1 or no change from the automatically computed value.

-s sat --- sat is a saturation factor. It is used in a post processing step. It is a float in the range of -45 to 45 (percent). The default is 0 or no change and skips saturation processing.

-m maxgain --- maxgain is the maximum gain. It controls the amount of detail (sharpness). Values are floats>0. The default is 2.5. Larger values allow more detail to show and may be useful for very hazy images. Too large a value may over-exaggerate detail, especially when there is no haze.

-d --- set the desired standard deviation (dstd) to an internally set constant value. The default is to use the image's actual std for the dstd. Both results are close. It becomes a user's preference. The results are image dependent. Some images become slightly sharper with one method and other images are sharper with the other method.

-w window --- the moving window size as a percentage of the input image for the local averaging and standard deviation operations. Values are 2<=float<=50. Typical values are between 5 and 20. The default is 8 (%).

REQUIREMENTS: If not using HDRI mode, this script can be slow due to the use of -fx. Also IM 6.5.5-1 or higher due to the use of -auto-level. For best results use IM 6.7.9-5 or higher due to changes in the operation of -sigmoidal-contrast.

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

Original Image
(http://3.bp.blogspot.com/-iOOnD5rmkxk/Ta7Vi7ePTsI/AAAAAAAAANM/EgDnXlR0HHU/s1600/Chloe%2BBell%2B045.JPG)

 

Arguments:
(defaults)


Original Image
(provided by Anthony Thyssen)

 

Arguments:
(defaults)


Original Image
(http://www.asknlearn.com/EduWiz/help/Chapter9/Chapter9.htm)

 

Arguments:
(defaults)


Original Image
(http://enhance.pho.to/)

 

Arguments:
(defaults)


Original Image
(http://enhance.pho.to/)

 

Arguments:
(defaults)


Original Image
(http://warrencampdesign.com/warren/images/photoEdits/basics/focusContrast/lions_before.jpg)

 

Arguments:
(defaults)


Original Image
(http://digital-photography-school.com/turn-ho-hum-color-into-wow-with-photoshop)

 

Arguments:
(defaults)


Original Image

Arguments:
-a t (default)

Arguments:
-a s



Original Image
(source)

Arguments:
(defaults)



Original Image
(source)

Arguments:
(defaults)

Arguments:
-m 10



Original Image
(source)

Arguments:
-m 2.5 (defaults)



Original Image
(source)

Arguments:
-a t (default)

Arguments:
-a s



What the script does is as follows:

  • Adjust the brightness and contrast using a non-linear method
  • Computes using -resize both a mean and standard deviation reduced
    size images according to the window dimension
  • Expands both using -resize to their original size
  • Subtracts the mean image from the original image as a difference image
  • Computes a gain image based upon the standard deviation
    image and the desired standard deviation
  • Combines the mean image with the product of the gain and difference images