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

DENOISE


Reduces noise in an image.

Download Script

last modified: April 29, 2015



USAGE: denoise [-m method] [-f filter] [-s subsection] [-n nstd] [-u unsharp] [-g gain] infile outfile
USAGE: denoise [-h or -help]

-m .... method .......... method of filtering; mean or median; default=mean
-f .... filter .......... filter size; float>0; default=2
-s .... subsection ...... subsection of image to compute noise standard deviation;
......................... WIDTHxHEIGHT+XOFF+YOFF; default is no subsection
......................... and get noise standard deviation from nstd argument
-n .... nstd ............ noise standard deviation estimate expressed as
......................... percentage of std value in range 0 to 1;
......................... 0<=float<=100; default is estimated automatically
-u .... unsharp ......... unsharp masking sigma to apply after filtering;
......................... float>=0; default is no unsharp masking
-g .... gain ............ unsharp masking gain to apply after filtering;
......................... float>=0; default=1

PURPOSE: To reduces the noise in an image. filter.

DESCRIPTION: DENOISE reduces the noise in an image. It uses the formula: input + gain*(input - mean), where mean is a local mean computed in the neighborhood of each pixel and gain=max(0,(std-nstd)/std), where std is the local standard deviation computed in the neighborhood of each pixel. This is an implementation of the Lee Filter. See Lee, J.S., 1981. Speckle Analysis and Smoothing of Synthetic Aperture Radar Images. Computer Graphics and Image Processing, Vol. 17:24-32.

ARGUMENTS:

-m method ... METHOD of filtering. Choices are: mean or median. Default=mean.

-f filter ... FILTER size. Values are floats>0. The default=2.

-s subsection ... Subsection is a rectangular homogeneous region where the noise standard deviation will be computed if provided. Otherwise, nstd, must be provided. The values are in the form of the usual IM subsection expressed as WIDTHxHEIGHT+XOFF+YOFF. The default is no subsection provided.

-n nstd ... NSTD is an estimate of the noise standard deviation in the image expressed as a percentage (100 times std values in the range of 0 to 1). Thus values for nstd are 0<=float<=100. The default is estimated automatically.

-u unsharp ... UNSHARP masking sigma to apply as a post processing step to sharpen the image. Values are floats>=0. The default=0 for no unsharp masking.

-g gain ... unsharp masking GAIN to apply as a post processing step to sharpen the image. Values are floats>=0. The default=1.

References:
Lee, J.S., 1981. Speckle Analysis and Smoothing of Synthetic Aperture Radar Images. Computer Graphics and Image Processing, Vol. 17:24-32.
http://www.isprs.org/proceedings/XXXVI/1-W41/makaleler/Rajabi_Specle_Noise.pdf
J. Immerkaer, "Fast Noise Variance Estimation", Comput. Vis. Image Understand., vol. 64, pp. 300-302, Sep. 1996
http://www.iaeng.org/IJCS/issues_v37/issue_1/IJCS_37_1_09.pdf

NOTE: This script may be a bit slow due to the use of -fx for versions of IM prior to 6.8.0.5.

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.


PROCESS NOISY IMAGE


Create Noisy Images

Clean Image

Noisy Image 1
mean=`convert lena.jpg -format "%[fx:mean]" info:`
mix=0.1
const=`convert xc: -format "%[fx:-$mix*$mean]" info:`
convert lena.jpg \
\( -size 256x256 xc: -attenuate 1 +noise random \) \
+swap -compose mathematics \
-set option:compose:args "0,1,$mix,$const" -composite \
lena_random_a1_m0p1.jpg

Noisy Image 2
mean=`convert lena.jpg -format "%[fx:mean]" info:`
mix=0.2
const=`convert xc: -format "%[fx:-$mix*$mean]" info:`
convert lena.jpg \
\( -size 256x256 xc: -attenuate 1 +noise random \) \
+swap -compose mathematics \
-set option:compose:args "0,1,$mix,$const" -composite \
lena_random_a1_m0p2.jpg



Technique To Measure Noise Standard Deviation

Noisy Image 2

Homogeneous Subsection
To Get Noise Standard Deviation
20x20+203+152

Computation of NSTD
convert lena_random_a1_m0p2.jpg[20x20+203+152] \
-format "%[fx:100*standard_deviation]" info:



EXAMPLES


Variation In Noise Amount

Noisy Image 1

Arguments:
-f 2 -s "20x20+203+152"

Noisy Image 2

Arguments:
-f 2 -s "20x20+203+152"



Variation In Filter Size

Noisy Image 2

Arguments:
-f 1 -s "20x20+203+152"

Arguments:
-f 2 -s "20x20+203+152"

Arguments:
-f 3 -s "20x20+203+152"



Variation In Estimate Of Noise Standard Deviation

Noisy Image 2

Arguments:
-f 1 -n 3

Arguments:
-f 2 -n 6
(approx. computed nstd)

Arguments:
-f 3 -n 12



Automatic Noise Estimation

Noisy Image 2

Arguments:
-f 2
(nstd estimated as 3.24476)



Comparison To Other Techniques

Noisy Image 2

-despeckle

-f 2 -s "20x20+203+152"

-enhance



Denoising Without And With Post-Sharpening

Noisy Image 2

-f 2 -s "20x20+203+152"

-f 2 -s "20x20+203+152" -u 1



Variation In Method: Mean vs Median

Noisy Image 2

-f 2 -s "20x20+203+152" -m mean

-f 2 -s "20x20+203+152" -m median



What the script does is as follows for a edge sharpened image:

  • Computes local mean and local standard deviation images.
  • Uses the nstd and the local standard deviation image to compute a gain image
  • Computes the output imagae as (input - mean)*gain + mean

This is equivalent to the following IM commands:

  • if [ "$nstd" = "" -a "$subsection" = "" ]; then
    errMsg "--- EITHER NSTD OR SUBSECTION MUST BE PROVIDED ---"
    elif [ "$subsection" != "" ]; then
    nstd=`convert $infile[$subsection] -format "%[fx:100*standard_deviation]" info:`
    fi
  • convert $infile \( -clone 0 -blur 0x$filter -write $tmpM1 \) \
    \( -clone 0 -clone 0 -compose multiply -composite -blur 0x$filter \) \
    \( -clone 1 -clone 1 -compose multiply -composite \) \
    -delete 0,1 +swap -compose minus -composite -gamma 2 $tmpS1
  • convert $tmpS1 \( +clone -evaluate subtract ${nstd}% \) \
    -compose divide -composite -evaluate max 0 $tmpG1
  • convert $infile $tmpM1 $tmpG1 \
    \( -clone 0 -clone 2 -compose multiply -composite \) \
    \( -clone 1 -clone 2 -compose multiply -composite \) \
    -delete 0,2 -swap 0,1 -swap 1,2 -monitor \
    -fx "u[0]-u[1]+u[2]" +monitor $outfile