Example Discrete Fourier Transform

From BoofCV
Revision as of 13:23, 25 December 2013 by Peter (talk | contribs) (Created page with " frame|center|Left: Magnitude of DCF. Middle: Phase of DCF. Right: Input image Discrete Fourier Transform (DCF) is widely in image processing....")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Left: Magnitude of DCF. Middle: Phase of DCF. Right: Input image

Discrete Fourier Transform (DCF) is widely in image processing. The fast fourier transform (FFT) allows the DCF to be used in real time and runs much faster if the width and height are both powers of two. BoofCV provides operators for manipulating the DCF and for visualizating the results, as this example shows.

Example Code:


  • Fourier Transform
  • Frequency Domain Filtering

Relevant Applets:

Example Code

 * Example demonstrating how to compute the Discrete Fourier Transform, visualize the transform, and apply
 * a filter frequency domain.
 * @author Peter Abeles
public class ExampleFourierTransform {

	 * Demonstration of how to apply a box filter in the frequency domain and compares the results
	 * to a box filter which has been applied in the spatial domain
	public static void applyBoxFilter( ImageFloat32 input ) {

		// declare storage
		ImageFloat32 boxImage = new ImageFloat32(input.width, input.height);
		InterleavedF32 boxTransform = new InterleavedF32(input.width,input.height,2);
		InterleavedF32 transform = new InterleavedF32(input.width,input.height,2);
		ImageFloat32 blurredImage = new ImageFloat32(input.width, input.height);
		ImageFloat32 spatialBlur = new ImageFloat32(input.width, input.height);

		DiscreteFourierTransform<ImageFloat32,InterleavedF32> dft =

		// Make the image scaled from 0 to 1 to reduce overflow issues

		// compute the Fourier Transform

		// create the box filter which is centered around the pixel.  Note that the filter gets wrapped around
		// the image edges
		for( int y = 0; y < 15; y++ ) {
			int yy = y-7 < 0 ? boxImage.height+(y-7) : y - 7;
			for( int x = 0; x < 15; x++ ) {
				int xx = x-7 < 0 ? boxImage.width+(x-7) : x - 7;
				// Set the value such that it doesn't change the image intensity
		// compute the DFT for the box filter

		// Visualize the Fourier Transform for the input image and the box filter
		displayTransform(transform,"Input Image");
		displayTransform(boxTransform,"Box Filter");

		// apply the filter. convolution in spacial domain is the same as multiplication in the frequency domain

		// convert the image back and display the results
		// undo change of scale

		// For sake of comparison, let's compute the box blur filter in the spatial domain
		// NOTE: The image border will be different since the frequency domain wraps around and this implementation
		// of the spacial domain adapts the kernel size

		// Convert to BufferedImage for output
		BufferedImage originOut = ConvertBufferedImage.convertTo(input, null);
		BufferedImage spacialOut = ConvertBufferedImage.convertTo(spatialBlur, null);
		BufferedImage blurredOut = ConvertBufferedImage.convertTo(blurredImage, null);

		ListDisplayPanel listPanel = new ListDisplayPanel();
		listPanel.addImage(originOut,"Original Image");
		listPanel.addImage(spacialOut,"Spacial Domain Box");
		listPanel.addImage(blurredOut,"Frequency Domain Box");

		ShowImages.showWindow(listPanel,"Box Blur in Spacial and Frequency Domain of Input Image");

	 * Display the fourier transform's magnitude and phase.
	public static void displayTransform( InterleavedF32 transform , String name ) {

		// declare storage
		ImageFloat32 magnitude = new ImageFloat32(transform.width,transform.height);
		ImageFloat32 phase = new ImageFloat32(transform.width,transform.height);

		// Make a copy so that you don't modify the input
		transform = transform.clone();

		// shift the zero-frequency into the image center, as is standard in image processing

		// Compute the transform's magnitude and phase
		DiscreteFourierTransformOps.phase(transform, phase);

		// Convert it to a log scale for visibility

		// Display the results
		BufferedImage visualMag = VisualizeImageData.grayMagnitude(magnitude, null, -1);
		BufferedImage visualPhase = VisualizeImageData.colorizeSign(phase, null, Math.PI);

		ImageGridPanel dual = new ImageGridPanel(1,2,visualMag,visualPhase);
		ShowImages.showWindow(dual,"Magnitude and Phase of "+name);

	public static void main( String args[] ) {

		ImageFloat32 input = UtilImageIO.loadImage("../data/evaluation/standard/lena512.bmp", ImageFloat32.class);