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

MATCHIMAGE


Matches the brightness and contrast of one image to another image.

Download Script

last modified: January 14, 2017



USAGE: matchimage [-c colormode] infile1 [infile2] outfile
USAGE: matchimage [-h or -help]

-c ... colormode ... colorspace mode of processing; options are: rgb,
.................... hsi, lab or ycbcr; default=rgb
-m ... mean ........ desired mean in place of mean from infile2;
.................... comma separated triplet; 0<=floats<=1;
.................... default is no mean provided
-s ... std ......... desired std in place of std from infile2;
.................... comma separated triplet; 0<=floats<=1;
.................... default is no mean provided
.................... infile1 will be be processed to match that of infile2

PURPOSE: To match the brightness and contrast of one image to another image.

DESCRIPTION: MATCHIMAGE matches the brightness and contrast of one image (infile1) to that of another image (infile2). This is done via a linear transformation of each RGB channel or an intensity channel from another colorspace. The matching uses the mean and standard deviations from each image according to the equation: (I2-Mean2)/Std2 = (I1-Mean1)/Std1. This equation represents an normalized intensity such that it has zero mean and approximately the same range of values due to the division by the standard deviations. We solve this equation to form a linear transformation between I1 and I2 according to I2=A*I1+B, where A=(Std2/Std1) is the slope or gain and B=(Mean2-A*Mean1) is the intercept of bias. If no infile2 is provide and a (set of) mean(s) and standard deviation(s0 are provided in stead, then infile1 will be matched to the provided mean and standard deviation.

ARGUMENTS:

-c colormode ... COLORMODE is the colorspace mode of processing. The options are: rgb, hsi, lab or ycbcr. The default=rgb.

-m mean ... MEAN is the desired mean(s) in place of mean(s) from infile2. Values are optional comma separated triplets in range 0<=floats<=1. If only one value is provided (and no commas), it will be used for all channels or the other colorspaces' intensity channel. The default is no mean provided.

-s std ... STD is the desired standard deviation(s) in place of the standard deviation(s) from infile2. Values are optional comma separated triplets in range 0<=floats<=1. If only one value is provided (and no commas), it will be used for for all channels or the other colorspaces' intensity channel. The default is no mean provided.

NOTE: This script will not change colors, only the brightness and contrast. Therefore it is primarily to normalize similar images to each other as for example video sequences and not to color match completely different images.

CAUTION: This script may not be backward compatible before IM colorspace and grayscale changes prior to IM 6.8.6.0. I have not tested it for earlier releases. Let me know if anyone tries and it fails. See http://www.imagemagick.org/discourse-server/viewtopic.php?f=4&t=21269

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


Attempt To Increase Brightness And Contrast On Dark And Low Contrast Image

Dark, Low Contrast Image (infile1)

Reference Image (infile2)

Arguments:
-c rgb
(default)

Arguments:
-c hsi

Arguments:
-c lab

Arguments:
-c ycbcr



Attempt To Decrease Brightness And Contrast On Bright, High Contrast Image

Bright, High Contrast Image (infile1)

Reference Image (infile2)

Arguments:
-c rbg
(default)

Arguments:
-c hsi

Arguments:
-c lab

Arguments:
-c ycbcr



What the script does is as follows:

  • Converts both images to desired colorspace/channel
  • Gets the mean and standard deviation from those images
  • Computes the slope (gain) and intecept (bias) from the two sets
  • Applies -function polynomial on the first image

See the script for more details