ImageMagick Examples --
Fourier Multiply/Divide
- Index
- ImageMagick Examples Preface and Index
- Fourier Transforms
- ImageMagick Examples Preface and Index
- DIY FFT Mathematics Introduction
- FFT Multiply
- FFT Division
- FFT Multiply
DIY FFT Mathematics Introduction
Below are examples of a number of techniques for doing multiplication and Division with the two different styles of Fast Fourier Transform images. Their are basically two ways of doing the mathematics. Using FX math, and using Composite Maths....- FX Math
- Using FX, DIY Operator applies the formula directly. But as it is interpreted, it is very slow. However the formulas are applied all in a single expression, avoiding the need for intermedite images. For non-HDRI versions of IM this will reduce Quantum Rounding effects.
- Composite math
- This uses Image Composition to perform mathematical operations, which is much faster than 'FX Math'. However you can only do one mathematical operation at a time, which means after every operation the image must be saved into another in-memory image. For non-HDRI versions this will produce extra 'rounding effects' between operations.
When saving intermediate HDRI images to
a disk file, you will need to use one of the very few floating point image
file formats. These formats include NetPBM PFM file format, TIFF, or
the MIFF file format with the special
setting "-define quantum:format=floating-point ".
|
Here is the convolution kernel image we will be using...
-roll -64-64
" needs to be applied
immediately the kernel image is read in.
The kernel image also needs to be divided by its mean so as to preserve the
intensity of the image it is being FFT multiplied or divided against. This is
a major complication with FFT multiplication and division.
As non-HDRI images can not be normalized in this way this is generally done
after the FFT conversion has been applied, and before multiplication.
For HDRI version of IM you can do this at any time.
FFT Multiplication   ( )
FFT multiply of Magnitude/Phase Images, Using IM Q16
R = A ⊗ B ( FFT Multiply ) Rm = Am × Bm Rp = mod( Ap + Bp +0.5, 1.0) mean = the DC value (center pixel) of the magnitude imageUsing FX Math,
(note value v.p{64x64} is the DC, or mean of the convolve kernel)
# non-HDRI magick convolve_kernel.png -roll -64-64 -fft \ \( cameraman_sm.png -fft \) \ \ \( -clone 0,2 -fx 'u*v / p{64,64} ' \) \ \( -clone 1,3 -fx 'mod(u + v + 0.5, 1.0)' \) \ \ -delete 0-3 -ift cameraman_convolve_1.png |
# non-HDRI magick convolve_kernel.png -roll -64-64 -fft \ \( -clone 0 -crop 1x1+64+64 +repage -scale 128x128 \ -clone 0 -compose divide -composite \) -swap 0 +delete \ \( cameraman_sm.png -fft \) \ \ \( -clone 0,2 -compose multiply -composite \) \ \( -clone 1,3 -compose add -background gray50 -flatten \) \ -delete 0-3 -ift cameraman_convolve_2.png |
Divide with mean from original image
# non-HDRI magick convolve_kernel.png \( -clone 0 -roll -64-64 -fft \) \ \( -clone 0 -scale 1x1 -scale 128x128 \ -clone 1 -compose divide -composite \) \ -delete 0,1 +swap \ \( cameraman_sm.png -fft \) \ \ \( -clone 0,2 -compose multiply -composite \) \ \( -clone 1,3 -compose add -background gray50 -flatten \) \ -delete 0-3 -ift cameraman_convolve_2b.png |
Note that the DC value divided by DC value ==> 1.0 As DC value is the largest value in a magnitude spectrum, why not just normalise! EG:
-auto-level
"
This can only be done to a magnitude image, and will not work
with real/imaginary pairs as they have to be stretched by the same amount.
# non-HDRI magick convolve_kernel.png -roll -64-64 -fft \ \( -clone 0 -auto-level \) -swap 0 +delete \ \( cameraman_sm.png -fft \) \ \ \( -clone 0,2 -compose multiply -composite \) \ \( -clone 1,3 -compose add -background gray50 -flatten \) \ \ -delete 0-3 -ift cameraman_convolve_2c.png |
FFT multiply of Real/Imaginary Images, Using IM HDRI
R = A ⊗ B ( FFT Multiply ) Rr = Ar×Br - Ai×Bi Ri = Ar×Bi + Ai×Br mean = the DC value (center pixel) of the real imageUsing FX...
5 images involved, the 5'th being scaled mean - FX does the division
|
using the DC value for the mean (no 5'th image)
|
With the scale image mean!
|
Mean from image scaling
|
Use the DC value for mean...
|
Apply DC value mean, AFTER the FFT multiply...
|
FFT Division   ( )
Here use use division to remove or de-convolve (for want of a better word) the blur that was added to the above image. It is basically exactly the same except that the 'normalized' convolution kernel is divided from the main image. Each Example uses the image generated, using the same technique as above.FFT divide of Magnitude/Phase, Using IM Q16
R = B ø A ( FFT Divide ) Rm = Bm / Am Rp = mod( -Ap + Bp +1.5, 1.0) mean = the DC value (center pixel) of the magnitude imageNOTE: A minimum of QuantumScale is added to the denominator to avoid perfect division by zero. This is also used later to remove noise from images. Using FX Math...
|
|
|
|
FFT divide of Real/Imaginary, Using HDRI version of IM
R = B ø A ( FFT Divide ) Denom = Ar×Ar + Ai×Ai + noise Rr = ( Ar×Br + Ai×Bi ) / Denom Ri = ( Ar×Bi - Ai×Br ) / Denom mean = the DC value (center pixel) of the real imageUsing FX...
|
|
|
|
|
|
|
|
UPDATE: the use of the color 'gray50' in the above should be change to 'gray(50%)' to generate a more accurite 50% gray value. The former 'named' color is actually only an 8-bit color, and as such not very accurite. Also with the new colorspace handling, gray50 is in sRGB colorspace while gray(50%) is linear colorspace, which is what should be used in the above calculations. The examples also needs to be checked with respect to the use a linear gray-scale colorspace.