Fred's ImageMagick Scripts



 

 

FFTFILTER


Performs filtering on an image in the frequency domain.

Download Script

last modified: November 27, 2011



USAGE: fftfilter infile filtfile outfile
USAGE: fftfilter [-h or -help]

No options are required.

PURPOSE: To perform convolution on an image in the frequency domain.

DESCRIPTION: FFTFILTER performs filtering on an image in the frequency domain using a frequency domain filter image. Two inputs are required. The image and a grayscale frequency domain filter. The image is transformed to the frequency domain using -fft and the filter image is then multiplied with the Fourier transform of the image and the product is then returned to the spatial domain using -ift. Any alpha channel on the filter will be removed automatically before processing. If the image has an alpha channel it will not be processed, but simply copied from the input to the output.

ARGUMENTS:

None

The filter image must be appropriately centered and padded with black to the same size as the input image.

REQUIREMENTS: IM version 6.5.4-7 or higher. HDRI is not required, but Q8 compilations of IM are not recommended as it will not carry enough precision. Also requires the FFTW delegate library.

See Fourier Transform with ImageMagick, for more details.

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.


EXAMPLES


Gaussian Blurring - Low Pass Filtering

Input

Filter
convert -size 128x128 xc:black \
-fill white -draw "point 64,64" \
-alpha off -blur 0x8 \
-contrast-stretch 0 gaussian8.png

Arguments:
(none)



Gaussian Edge Detection - High Pass Filtering

Input

Filter
convert -size 128x128 xc:black \
-fill white -draw "point 64,64" \
-alpha off -blur 0x8 -contrast-stretch 0 \
-negate gaussian8_neg.png

Arguments:
(none)

Post Processing:
-normalize



Simple Binary Notch Filtering

Input

Spectrum:
(see script, spectrum)

Masked Spectrum:

Filter:
Binarized, Masked
Spectrum:
-fill white +opaque black

Arguments:
(none)



Notch Filtering With Tapering

Input

Spectrum:
(see script, spectrum)

Masked Spectrum:

Filter:
Binarize And Apply
Linear Taper
-fill white +opaque black \
-blur 5x65000

Arguments:
(none)



What the script does is as follows:

  • Computes the magnitude and phase of the Fourier transform
    of the image
  • Multiplies the filter by the magnitude component
  • Converts the magnitude product with the original phase back
    to the spatial domain

This is equivalent to the following IM commands.

  • width=`identify -ping -format "%w" $infile`
  • height=`identify -ping -format "%h" $infile`
  • cx=`convert xc: -format "%[fx:floor(($width+1)/2)]" info:`
  • cy=`convert xc: -format "%[fx:floor(($height+1)/2)]" info:`
  • convert \( $tmpA1 -fft \) \
    \( -clone 0 $tmpA2 -compose multiply -composite \) \
    -delete 0 +swap -ift \
    -crop ${width}x${height}+0+0 +repage $outfile