snibgo's ImageMagick pages

Noise

+noise adds noise. Here are some illustrations.

Scripts on this page assume that the version of ImageMagick in %IM7DEV% 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.13039063
mean=0.49972799
max=0.8317393
sd=0.077868539
clipped=0.5
ns__nse_a_Gaussian.png ns__hist_a_Gaussian_glc.png min=0
mean=0.49933085
max=1
sd=0.19282902
clipped=0.505175
Impulse ns__nse_Impulse.png ns__hist_Impulse_glc.png min=0
mean=0.498925
max=1
sd=0.15807267
clipped=0.549975
ns__nse_a_Impulse.png ns__hist_a_Impulse_glc.png min=0
mean=0.498375
max=1
sd=0.25017279
clipped=0.625175
Laplacian ns__nse_Laplacian.png ns__hist_Laplacian_glc.png min=0.081821789
mean=0.49938782
max=0.97055999
sd=0.055326065
clipped=0.5
ns__nse_a_Laplacian.png ns__hist_a_Laplacian_glc.png min=0
mean=0.49855224
max=1
sd=0.1355339
clipped=0.503025
Multiplicative ns__nse_Multiplicative.png ns__hist_Multiplicative_glc.png min=0
mean=0.50077936
max=1
sd=0.12591223
clipped=0.50005
ns__nse_a_Multiplicative.png ns__hist_a_Multiplicative_glc.png min=0
mean=0.50160645
max=1
sd=0.28362564
clipped=0.5557
Poisson ns__nse_Poisson.png ns__hist_Poisson_glc.png min=0
mean=0.498902
max=1
sd=0.19662411
clipped=0.5074125
ns__nse_a_Poisson.png ns__hist_a_Poisson_glc.png min=0.064
mean=0.4996478
max=1
sd=0.12664064
clipped=0.5000875
Random ns__nse_Random.png ns__hist_Random_glc.png min=1.1212185e-05
mean=0.49800473
max=0.99999707
sd=0.28905863
clipped=0.5
ns__nse_a_Random.png ns__hist_a_Random_glc.png min=2.8030463e-05
mean=0.79753384
max=1
sd=0.30724452
clipped=0.7992375
Uniform ns__nse_Uniform.png ns__hist_Uniform_glc.png min=0.49218768
mean=0.49996882
max=0.50781245
sd=0.004516541
clipped=0.5
ns__nse_a_Uniform.png ns__hist_a_Uniform_glc.png min=0.48046919
mean=0.49992206
max=0.51953114
sd=0.011291353
clipped=0.5

We repeat with a colour image:

%IMG7%magick ^
  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.48783879
max=1
sd=0.17166577
clipped=0.50598571
ns_ns_toes_nse_a_Gaussian.png ns_ns_toes_hist_a_Gaussian_glc.png min=0
mean=0.48740997
max=1
sd=0.23811242
clipped=0.5644
Impulse ns_ns_toes_nse_Impulse.png ns_ns_toes_hist_Impulse_glc.png min=0
mean=0.48909433
max=1
sd=0.21550449
clipped=0.6366
ns_ns_toes_nse_a_Impulse.png ns_ns_toes_hist_a_Impulse_glc.png min=0
mean=0.49042179
max=1
sd=0.28368107
clipped=0.78904286
Laplacian ns_ns_toes_nse_Laplacian.png ns_ns_toes_hist_Laplacian_glc.png min=0
mean=0.48808543
max=1
sd=0.16264722
clipped=0.50324286
ns_ns_toes_nse_a_Laplacian.png ns_ns_toes_hist_a_Laplacian_glc.png min=0
mean=0.48791408
max=1
sd=0.20037435
clipped=0.53168571
Multiplicative ns_ns_toes_nse_Multiplicative.png ns_ns_toes_hist_Multiplicative_glc.png min=0
mean=0.48636857
max=1
sd=0.1936074
clipped=0.52428571
ns_ns_toes_nse_a_Multiplicative.png ns_ns_toes_hist_a_Multiplicative_glc.png min=0
mean=0.47508481
max=1
sd=0.29515834
clipped=0.67094286
Poisson ns_ns_toes_nse_Poisson.png ns_ns_toes_hist_Poisson_glc.png min=0
mean=0.48312381
max=1
sd=0.23641495
clipped=0.56142857
ns_ns_toes_nse_a_Poisson.png ns_ns_toes_hist_a_Poisson_glc.png min=0
mean=0.48683215
max=1
sd=0.19433825
clipped=0.51818571
Random ns_ns_toes_nse_Random.png ns_ns_toes_hist_Random_glc.png min=9.354053e-06
mean=0.50020673
max=0.99999707
sd=0.2890121
clipped=0.5
ns_ns_toes_nse_a_Random.png ns_ns_toes_hist_a_Random_glc.png min=2.3385133e-05
mean=0.79969043
max=1
sd=0.30585469
clipped=0.9673
Uniform ns_ns_toes_nse_Uniform.png ns_ns_toes_hist_Uniform_glc.png min=0.027738612
mean=0.48816322
max=0.99827618
sd=0.15321613
clipped=0.5
ns_ns_toes_nse_a_Uniform.png ns_ns_toes_hist_a_Uniform_glc.png min=0.02363813
mean=0.48816789
max=1
sd=0.15355235
clipped=0.50008571

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.

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

%IM7DEV%magick ^
  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:

%IM7DEV%magick ^
  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:

%IM7DEV%magick ^
  -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.

%IMG7%magick ^
  ns_unidist.png ^
  ns_gaus_cl.miff ^
  -clut ^
  ns_gaus_nse.png

%IM7DEV%magick ^
  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
%IMG7%magick ^
  ns_unidist.png ^
  ns_lap_cl.miff ^
  -clut ^
  ns_lap_nse.png

%IM7DEV%magick ^
  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 magick command.

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.

%IM7DEV%magick ^
  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

%IM7DEV%magick ^
  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.

%IM7DEV%magick ^
  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

%IM7DEV%magick ^
  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]

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 (`%IM7DEV%magick -list noise`) do (
  echo %%N

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

  %IM7DEV%magick ^
    %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

  %IM7DEV%magick ^
    %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:

%IMG7%magick -version
Version: ImageMagick 7.1.0-42 Q16-HDRI x64 396d87c:20220709 https://imagemagick.org
Copyright: (C) 1999 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC HDRI OpenCL 
Delegates (built-in): bzlib cairo freetype gslib heic jng jp2 jpeg jxl lcms lqr lzma openexr pangocairo png ps raqm raw rsvg tiff webp xml zip zlib
Compiler: Visual Studio 2022 (193231332)

To improve internet download speeds, some images may have been automatically converted (by ImageMagick, of course) from PNG or TIFF or MIFF 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 08-Aug-2022 23:52:29.

Copyright © 2022 Alan Gibson.