View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.image.processing.algorithm;
31  
32  import org.openimaj.image.FImage;
33  
34  import edu.emory.mathcs.jtransforms.fft.FloatFFT_2D;
35  
36  /**
37   * Perform forward and inverse Fast Fourier Transforms on image data. This class
38   * computes the result of the transform in complex form. If you want the result
39   * in polar form (in terms of phase and magnitude) then use the
40   * {@link FourierTransform} instead.
41   *
42   * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
43   *
44   */
45  public class FourierTransformComplex {
46  	private FImage real;
47  	private FImage imaginary;
48  	private boolean centre;
49  
50  	/**
51  	 * Construct Fourier Transform by performing a forward transform on the
52  	 * given image. If the centre option is set, the FFT will be re-ordered so
53  	 * that the DC component is in the centre.
54  	 *
55  	 * @param image
56  	 *            the image to transform
57  	 * @param centre
58  	 *            should the FFT be reordered so the centre is DC component
59  	 */
60  	public FourierTransformComplex(FImage image, boolean centre) {
61  		this.centre = centre;
62  
63  		process(image);
64  	}
65  
66  	/**
67  	 * Construct Fourier Transform object from the given magnitude and phase
68  	 * images in the frequency domain. The resultant object can then be used to
69  	 * construct the image using the {@link #inverse()} method.
70  	 *
71  	 * @param real
72  	 *            the real image
73  	 * @param imaginary
74  	 *            the imaginary image
75  	 * @param centre
76  	 *            is the DC component in the image centre?
77  	 */
78  	public FourierTransformComplex(FImage real, FImage imaginary, boolean centre) {
79  		this.centre = centre;
80  		this.real = real;
81  		this.imaginary = imaginary;
82  	}
83  
84  	private void process(FImage image) {
85  		final int cs = image.getCols();
86  		final int rs = image.getRows();
87  
88  		real = new FImage(cs, rs);
89  		imaginary = new FImage(cs, rs);
90  
91  		final FloatFFT_2D fft = new FloatFFT_2D(rs, cs);
92  		final float[][] prepared = FourierTransform.prepareData(image.pixels, rs, cs, centre);
93  
94  		fft.complexForward(prepared);
95  
96  		for (int y = 0; y < rs; y++) {
97  			for (int x = 0; x < cs; x++) {
98  				real.pixels[y][x] = prepared[y][x * 2];
99  				imaginary.pixels[y][x] = prepared[y][1 + x * 2];
100 			}
101 		}
102 	}
103 
104 	/**
105 	 * Perform the inverse FFT using the underlying magnitude and phase images.
106 	 * The resultant reconstructed image may need normalisation.
107 	 *
108 	 * @return the reconstructed image
109 	 */
110 	public FImage inverse() {
111 		final int cs = real.getCols();
112 		final int rs = real.getRows();
113 
114 		final FloatFFT_2D fft = new FloatFFT_2D(rs, cs);
115 		final float[][] prepared = new float[rs][cs * 2];
116 		for (int y = 0; y < rs; y++) {
117 			for (int x = 0; x < cs; x++) {
118 				prepared[y][x * 2] = real.pixels[y][x];
119 				prepared[y][1 + x * 2] = imaginary.pixels[y][x];
120 			}
121 		}
122 
123 		fft.complexInverse(prepared, true);
124 
125 		final FImage image = new FImage(cs, rs);
126 		FourierTransform.unprepareData(prepared, image, centre);
127 
128 		return image;
129 	}
130 
131 	/**
132 	 * @return the real image
133 	 */
134 	public FImage getReal() {
135 		return real;
136 	}
137 
138 	/**
139 	 * @return the imaginary image
140 	 */
141 	public FImage getImaginary() {
142 		return imaginary;
143 	}
144 
145 	/**
146 	 * @return true if the DC component is in the centre; false otherwise
147 	 */
148 	public boolean isCentre() {
149 		return centre;
150 	}
151 }