Fred's ImageMagick Scripts



 

 

DENOISE


Reduces noise in an image.

Download Script

last modified: November 27, 2011



USAGE: denoise [-m method] [-f filter] [-s subsection] [-n nstd] [-u unsharp] 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 none specified and to get
......................... value from subsection
-u .... unsharp ......... unsharp masking amount to apply after filtering;
......................... float>=0; default is no unsharp masking

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 (of std values in the range of 0 to 1). Thus values for nstd are 0<=float<=100. The default is no nstd provided. Either one or the other of nstd or subsection must be provided.

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

NOTE: This script will be a bit slow due to the use of -fx.

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



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