001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.image.processing.algorithm; 031 032import org.openimaj.image.FImage; 033 034import edu.emory.mathcs.jtransforms.fft.FloatFFT_2D; 035 036/** 037 * Perform forward and inverse Fast Fourier Transforms on image data. This class 038 * computes the result of the transform in complex form. If you want the result 039 * in polar form (in terms of phase and magnitude) then use the 040 * {@link FourierTransform} instead. 041 * 042 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 043 * 044 */ 045public class FourierTransformComplex { 046 private FImage real; 047 private FImage imaginary; 048 private boolean centre; 049 050 /** 051 * Construct Fourier Transform by performing a forward transform on the 052 * given image. If the centre option is set, the FFT will be re-ordered so 053 * that the DC component is in the centre. 054 * 055 * @param image 056 * the image to transform 057 * @param centre 058 * should the FFT be reordered so the centre is DC component 059 */ 060 public FourierTransformComplex(FImage image, boolean centre) { 061 this.centre = centre; 062 063 process(image); 064 } 065 066 /** 067 * Construct Fourier Transform object from the given magnitude and phase 068 * images in the frequency domain. The resultant object can then be used to 069 * construct the image using the {@link #inverse()} method. 070 * 071 * @param real 072 * the real image 073 * @param imaginary 074 * the imaginary image 075 * @param centre 076 * is the DC component in the image centre? 077 */ 078 public FourierTransformComplex(FImage real, FImage imaginary, boolean centre) { 079 this.centre = centre; 080 this.real = real; 081 this.imaginary = imaginary; 082 } 083 084 private void process(FImage image) { 085 final int cs = image.getCols(); 086 final int rs = image.getRows(); 087 088 real = new FImage(cs, rs); 089 imaginary = new FImage(cs, rs); 090 091 final FloatFFT_2D fft = new FloatFFT_2D(rs, cs); 092 final float[][] prepared = FourierTransform.prepareData(image.pixels, rs, cs, centre); 093 094 fft.complexForward(prepared); 095 096 for (int y = 0; y < rs; y++) { 097 for (int x = 0; x < cs; x++) { 098 real.pixels[y][x] = prepared[y][x * 2]; 099 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}