snibgo's ImageMagick pages

Noise

+noise adds noise. Here are some illustrations.

Scripts on this page assume that the version of ImageMagick in %IMDEV% has been built with various process modules. See Process modules.

Built-in noise

We can demonstrate the noise generated by the "+noise" operator.

set ns_seed=1234

call %PICTBAT%demoNoise . . %ns_seed%

The script builds a table. The "clipped" number is the proportion of pixels (from 0.0 to 1.0) that have either 0 or 100% in any channel.

no attenuate -attenuate 2.5
+noise image histogram statistics image histogram statistics
Gaussian ns__nse_Gaussian.png ns__hist_Gaussian_glc.png min=0.182821
mean=0.50001
max=0.803895
sd=0.0781191
clipped=0
ns__nse_a_Gaussian.png ns__hist_a_Gaussian_glc.png min=0
mean=0.500058
max=1
sd=0.193454
clipped=0.010025
Impulse ns__nse_Impulse.png ns__hist_Impulse_glc.png min=0
mean=0.499688
max=1
sd=0.158412
clipped=0.100375
ns__nse_a_Impulse.png ns__hist_a_Impulse_glc.png min=0
mean=0.500375
max=1
sd=0.251399
clipped=0.2528
Laplacian ns__nse_Laplacian.png ns__hist_Laplacian_glc.png min=0.137968
mean=0.499882
max=0.903822
sd=0.0552343
clipped=0
ns__nse_a_Laplacian.png ns__hist_a_Laplacian_glc.png min=0
mean=0.499733
max=1
sd=0.135617
clipped=0.006225
Multiplicative ns__nse_Multiplicative.png ns__hist_Multiplicative_glc.png min=0.0237648
mean=0.501006
max=0.971936
sd=0.124775
clipped=0
ns__nse_a_Multiplicative.png ns__hist_a_Multiplicative_glc.png min=0
mean=0.502408
max=1
sd=0.281936
clipped=0.110825
Poisson ns__nse_Poisson.png ns__hist_Poisson_glc.png min=0
mean=0.498394
max=1
sd=0.197133
clipped=0.0141
ns__nse_a_Poisson.png ns__hist_a_Poisson_glc.png min=0.064
mean=0.499039
max=1
sd=0.126596
clipped=0.0001
Random ns__nse_Random.png ns__hist_Random_glc.png min=4.71976e-05
mean=0.499203
max=0.999984
sd=0.289233
clipped=0
ns__nse_a_Random.png ns__hist_a_Random_glc.png min=0.000117994
mean=0.799022
max=1
sd=0.30601
clipped=0.597425
Uniform ns__nse_Uniform.png ns__hist_Uniform_glc.png min=0.492188
mean=0.499988
max=0.507812
sd=0.00451927
clipped=0
ns__nse_a_Uniform.png ns__hist_a_Uniform_glc.png min=0.480471
mean=0.499969
max=0.519531
sd=0.0112982
clipped=0

We repeat with a colour image:

%IM%convert ^
  toes.png ^
  -resize 200x200 ^
  -gravity Center ^
  -crop 200x200+0+0 +repage ^
  ns_toes.png
ns_toes.png
call %PICTBAT%demoNoise ns_toes.png ns_toes.htm %ns_seed%
no attenuate -attenuate 2.5
+noise image histogram statistics image histogram statistics
Gaussian ns_ns_toes_nse_Gaussian.png ns_ns_toes_hist_Gaussian_glc.png min=0
mean=0.488095
max=1
sd=0.171889
clipped=0.0128571
ns_ns_toes_nse_a_Gaussian.png ns_ns_toes_hist_a_Gaussian_glc.png min=0
mean=0.488156
max=1
sd=0.238817
clipped=0.128143
Impulse ns_ns_toes_nse_Impulse.png ns_ns_toes_hist_Impulse_glc.png min=0
mean=0.489431
max=1
sd=0.21553
clipped=0.271571
ns_ns_toes_nse_a_Impulse.png ns_ns_toes_hist_a_Impulse_glc.png min=0
mean=0.490985
max=1
sd=0.28467
clipped=0.582371
Laplacian ns_ns_toes_nse_Laplacian.png ns_ns_toes_hist_Laplacian_glc.png min=0
mean=0.488087
max=1
sd=0.162563
clipped=0.00628571
ns_ns_toes_nse_a_Laplacian.png ns_ns_toes_hist_a_Laplacian_glc.png min=0
mean=0.487912
max=1
sd=0.200418
clipped=0.0594286
Multiplicative ns_ns_toes_nse_Multiplicative.png ns_ns_toes_hist_Multiplicative_glc.png min=0
mean=0.486713
max=1
sd=0.19367
clipped=0.0484
ns_ns_toes_nse_a_Multiplicative.png ns_ns_toes_hist_a_Multiplicative_glc.png min=0
mean=0.475967
max=1
sd=0.295047
clipped=0.340057
Poisson ns_ns_toes_nse_Poisson.png ns_ns_toes_hist_Poisson_glc.png min=0
mean=0.482444
max=1
sd=0.236522
clipped=0.1252
ns_ns_toes_nse_a_Poisson.png ns_ns_toes_hist_a_Poisson_glc.png min=0
mean=0.486822
max=1
sd=0.19439
clipped=0.0356571
Random ns_ns_toes_nse_Random.png ns_ns_toes_hist_Random_glc.png min=4.43735e-06
mean=0.499892
max=0.999988
sd=0.289271
clipped=0
ns_ns_toes_nse_a_Random.png ns_ns_toes_hist_a_Random_glc.png min=1.10934e-05
mean=0.799182
max=1
sd=0.306316
clipped=0.937657
Uniform ns_ns_toes_nse_Uniform.png ns_ns_toes_hist_Uniform_glc.png min=0.0277124
mean=0.488158
max=1
sd=0.153218
clipped=2.85714e-05
ns_ns_toes_nse_a_Uniform.png ns_ns_toes_hist_a_Uniform_glc.png min=0.0235727
mean=0.488155
max=1
sd=0.153556
clipped=0.000342857

Processed noise

Anthony gives many interesting examples of processing noise in the official Canvas Creation: Random Images. Here are some others.

"+noise Random" is distributed evenly. All values are equally likely. It is "equalised". The distribution is a horizontal line, more or less. We can create an image with +noise Random, and show the image and its histogram.

%IM%convert ^
  -size 256x256 xc:gray(50%%) ^
  -seed %ns_seed% ^
  +noise Random ^
  ns_unidist.png

%IMDEV%convert ^
  ns_unidist.png ^
  -process 'mkhisto capnumbuckets 256 norm' ^
  ns_unidist_h.png
call %PICTBAT%graphLineCol ns_unidist_h.png . . 0
ns_unidist.png ns_unidist_h_glc.png

We can change the distribution to any desired distribution by clutting with the inverse of the normalised cumulative of that distribution.

Make a clut image for Gaussian distribution, "zeroize" so the distribution is zero at each end:

%IMDEV%convert ^
  xc: ^
  -process 'mkgauss width 65536 sd 15%% zeroize cumul norm' ^
  -delete 0 ^
  -process 'mkhisto cumul norm' ^
  ns_gaus_cl.miff

call %PICTBAT%graphLineCol ns_gaus_cl.miff . . 0 . 256
ns_gaus_cl_glc.png

Make a clut image for Laplacian distribution "-auto-level" so the distribution is zero at each end:

%IMDEV%convert ^
  -size 1x32768 gradient: -rotate 90 ^
  -evaluate Exp -2 ^
  -auto-level ^
  ( +clone -flop ) ^
  +swap +append +repage ^
  -process 'cumulhisto norm' ^
  -process 'mkhisto cumul norm' ^
  ns_lap_cl.miff

call %PICTBAT%graphLineCol ns_lap_cl.miff . . 0 . 256
ns_lap_cl_glc.png

Transformations to make distributions Gaussian or Laplacian are now very simple: we clut with these files.

%IM%convert ^
  ns_unidist.png ^
  ns_gaus_cl.miff ^
  -clut ^
  ns_gaus_nse.png

%IMDEV%convert ^
  ns_gaus_nse.png ^
  -process 'mkhisto capnumbuckets 256 norm' ^
  ns_gaus_nse_h.png
call %PICTBAT%graphLineCol ns_gaus_nse_h.png . . 0
ns_gaus_nse.png ns_gaus_nse_h_glc.png
%IM%convert ^
  ns_unidist.png ^
  ns_lap_cl.miff ^
  -clut ^
  ns_lap_nse.png

%IMDEV%convert ^
  ns_lap_nse.png ^
  -process 'mkhisto capnumbuckets 256 norm' ^
  ns_lap_nse_h.png
call %PICTBAT%graphLineCol ns_lap_nse_h.png . . 0
ns_lap_nse.png ns_lap_nse_h_glc.png

If we are making one-off images rather than animations, it is faster to do both operations in the same convert.

A distribution can follow any line for which we can make a clut image, such as those from the Clut cookbook. To any clut from there, apply -process 'cumulhisto norm' -process 'mkhisto cumul norm' and use the result as a clut.

Flattened middle.

%IMDEV%convert ^
  ns_unidist.png ^
  ( -size 1x10000 gradient: -rotate 90 ^
    -fuzz 20%% -fill gray(15%%) -opaque gray(50%%) ^
    -process 'cumulhisto norm' ^
    -process 'mkhisto cumul norm' ^
  ) ^
  -clut ^
  ns_flmid_nse.png

%IMDEV%convert ^
  ns_flmid_nse.png ^
  -process 'mkhisto capnumbuckets 256 norm' ^
  ns_flmid_nse_h.png

call %PICTBAT%graphLineCol ns_flmid_nse_h.png . . 0
ns_flmid_nse.png ns_flmid_nse_h_glc.png

Quarter of a circle.

%IMDEV%convert ^
  ns_unidist.png ^
  ( -size 1x10000 gradient: -rotate 90 ^
    -function Polynomial -1,2,0 -evaluate Pow 0.5 ^
    -process 'cumulhisto norm' ^
    -process 'mkhisto cumul norm' ^
  ) ^
  -clut ^
  ns_qcirc_nse.png

%IMDEV%convert ^
  ns_qcirc_nse.png ^
  -process 'mkhisto capnumbuckets 256 norm' ^
  ns_qcirc_nse_h.png
call %PICTBAT%graphLineCol ns_qcirc_nse_h.png . . 0
ns_qcirc_nse.png ns_qcirc_nse_h_glc.png

See also Fractal noise.

Scripts

For convenience, .bat scripts are also available in a single zip file. See Zipped BAT files.

demoNoise.bat

The script builds the images shown in tables above, and the tables themselves.

It uses my own programs such as cEcho.exe and expAt.exe. I don't provide the source or binaries for these.

rem From image %1, perhaps 200x200 pixels,
rem makes %2 with HTML of a table with images and statistics.
rem %3 is optional seed.


echo 1: [%1]
echo 2: [%2]
echo 3: [%3]

if "%IM16i%"=="" call %PICTBAT%setim8

set INFILE=%1
if "%INFILE%"=="." set INFILE=
if "%INFILE%"=="" set INFILE=-size 200x200 xc:gray^^^(50%%%%^^^)

set TAB_FILE=%2
if "%TAB_FILE%"=="." set TAB_FILE=
if "%TAB_FILE%"=="" set TAB_FILE=ns_table.htm

if "%3"=="" (
  set SEED=
) else (
  set SEED=-seed %3
)
if "%SEED%"=="." set SEED=

echo INFILE=%INFILE% TAB_FILE=%TAB_FILE% SEED=%SEED%


set ATTEN=-attenuate 2.5

if "%INFILE%"=="%1" (
  set PREFIX=ns_%~n1
) else (
  set PREFIX=ns_
)

echo ATTEN=%ATTEN% PREFIX=%PREFIX%
rem exit /B 1

cecho /o%TAB_FILE% /X /s\(table\)
cecho /O%TAB_FILE% /X "/s<tr><td></td><th colspan=\q3\q>no attenuate</th><th colspan=\q3\q><tt>%ATTEN%</tt></th></tr>"

cecho /O%TAB_FILE% /X "/s<tr><th><tt>+noise</tt></th><th>image</th><th>histogram</th><th>statistics</th>"
cecho /O%TAB_FILE% /X "/s<th>image</th><th>histogram</th><th>statistics</th></tr>"

for /F "usebackq" %%N in (`%IM%convert -list noise`) do (
  echo %%N

  cecho /O%TAB_FILE% /X "/s<tr><td><tt>%%N</tt></td>"

  %IMDEV%convert ^
    %INFILE% ^
    %SEED% ^
    +noise %%N ^
    -clamp ^
    -format "min=%%[fx:minima]<br />\nmean=%%[fx:mean]<br />\nmax=%%[fx:maxima]<br />\nsd=%%[fx:standard_deviation]<br />\n" ^
    +write info: ^
    ^( +clone ^
      -separate ^
      -fill White -opaque Black ^
      -fill Black +opaque White ^
      -background Black ^
      -compose Lighten -layers Merge ^
      -format "clipped=%%[fx:mean]" ^
      +write info: ^
      +delete ^
    ^) ^
    +write %PREFIX%_nse_%%N.png ^
    -process 'mkhisto capnumbuckets 256 norm' ^
    %PREFIX%_hist_%%N.png >%PREFIX%_nse_%%N.lis

  if ERRORLEVEL 1 exit /B 1

  call %PICTBAT%graphLineCol %PREFIX%_hist_%%N.png . . 0 %PREFIX%_hist_%%N_glc.png

  if ERRORLEVEL 1 exit /B 1

  %IMDEV%convert ^
    %INFILE% ^
    %ATTEN% ^
    %SEED% ^
    +noise %%N ^
    -clamp ^
    -format "min=%%[fx:minima]<br />\nmean=%%[fx:mean]<br />\nmax=%%[fx:maxima]<br />\nsd=%%[fx:standard_deviation]<br />\n" ^
    +write info: ^
    ^( +clone ^
      -separate ^
      -fill White -opaque Black ^
      -fill Black +opaque White ^
      -background Black ^
      -compose Lighten -layers Merge ^
      -format "clipped=%%[fx:mean]" ^
      +write info: ^
      +delete ^
    ^) ^
    +write %PREFIX%_nse_a_%%N.png ^
    -process 'mkhisto capnumbuckets 256 norm' ^
    %PREFIX%_hist_a_%%N.png >%PREFIX%_nse_a_%%N.lis

  if ERRORLEVEL 1 exit /B 1

  call %PICTBAT%graphLineCol %PREFIX%_hist_a_%%N.png . . 0 %PREFIX%_hist_a_%%N_glc.png

  if ERRORLEVEL 1 exit /B 1

  cecho /O%TAB_FILE% /X "/s<td class=\qnotwhite\q><img src=\q%PREFIX%_nse_%%N.png\q /></td>"
  cecho /O%TAB_FILE% /X "/s<td class=\qnotwhite\q><img src=\q%PREFIX%_hist_%%N_glc.png\q /></td>"
  cecho /O%TAB_FILE% /X "/s<td>\n@%PREFIX%_nse_%%N.lis\n</td>"

  cecho /O%TAB_FILE% /X "/s<td class=\qnotwhite\q><img src=\q%PREFIX%_nse_a_%%N.png\q /></td>"
  cecho /O%TAB_FILE% /X "/s<td class=\qnotwhite\q><img src=\q%PREFIX%_hist_a_%%N_glc.png\q /></td>"
  cecho /O%TAB_FILE% /X "/s<td>\n@%PREFIX%_nse_a_%%N.lis\n</td>"

  cecho /O%TAB_FILE% /X "/s</tr>"

)

cecho /O%TAB_FILE% /X /s\(/table\)


type %PREFIX%_nse*.lis

type %TAB_FILE%

expAt /i%TAB_FILE% /o%TAB_FILE%

All images on this page were created by the commands shown, using:

%IM%identify -version
Version: ImageMagick 6.9.9-50 Q16 x64 2018-06-02 http://www.imagemagick.org
Copyright: Copyright (C) 1999-2015 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Visual C++: 180040629
Features: Cipher DPC Modules OpenMP 
Delegates (built-in): bzlib cairo flif freetype gslib heic jng jp2 jpeg lcms lqr lzma openexr pangocairo png ps raw rsvg tiff webp xml zlib

To improve internet download speeds, some images may have been automatically converted (by ImageMagick, of course) from PNG to JPG.

Source file for this web page is noise.h1. To re-create this web page, run "procH1 noise".


This page, including the images, is my copyright. Anyone is permitted to use or adapt any of the code, scripts or images for any purpose, including commercial use.

Anyone is permitted to re-publish this page, but only for non-commercial use.

Anyone is permitted to link to this page, including for commercial use.


Page version v1.0 2-September-2015.

Page created 24-Aug-2018 22:36:19.

Copyright © 2018 Alan Gibson.