00001 /*!@file Image/FourierEngine.H Thin wrapper around the fftw3 library */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/FourierEngine.H $ 00035 // $Id: FourierEngine.H 7924 2007-02-15 18:13:05Z rjpeters $ 00036 // 00037 00038 #ifndef IMAGE_FOURIERENGINE_H_DEFINED 00039 #define IMAGE_FOURIERENGINE_H_DEFINED 00040 00041 #include "Image/Image.H" 00042 00043 #include <complex> 00044 #ifdef HAVE_FFTW3_H 00045 #include <fftw3.h> 00046 #endif 00047 00048 typedef std::complex<double> complexd; 00049 typedef std::complex<float> complexf; 00050 00051 /// Thin wrapper around the fftw3 library for the discrete fourier transform. 00052 /** The fftw library works by devising an optimized 'plan' for 00053 computing fourier transforms of arrays of a given size -- the 00054 'plan' is optimized at run-time for the particular machine 00055 architecture on which the code is being run. Therefore, it is 00056 necessary to use a separate FourierEngine object for each size of 00057 array for which one wishes to compute the fft. Typically this is 00058 not a significant imposition, since most applications are expected 00059 to compute the fft of a large number of identically-sized images 00060 (e.g. processing a video stream). */ 00061 template <class T> 00062 class FourierEngine 00063 { 00064 public: 00065 /// Set up an engine for computing the fft of images with given Dims. 00066 FourierEngine(const Dims& d); 00067 00068 /// Destructor. 00069 ~FourierEngine(); 00070 00071 /// Compute the fourier transform of x, returning the complex result. 00072 /** Since the full-size complex fft of a real image is symmetric, 00073 this function (following the fftw conventions) returns only half 00074 of the complex result. The full result could be reconstructed as 00075 needed. Also note that the result is not 'shifted' -- that is, 00076 the DC component will be at the upper-left corner of the 00077 returned image, rather than in the center. */ 00078 Image<std::complex<T> > fft(const Image<T>& x); 00079 00080 private: 00081 FourierEngine(const FourierEngine&); 00082 FourierEngine& operator=(const FourierEngine&); 00083 00084 const Dims itsInputDims; 00085 T* itsSrc; 00086 std::complex<T>* itsDst; 00087 void* itsPlan; 00088 }; 00089 00090 /// Thin wrapper around the fftw3 for computing inverse fourier transforms 00091 template <class T> 00092 class FourierInvEngine 00093 { 00094 public: 00095 /// Set up an engine for computing the inverse fft. 00096 /** Note that the dimensions passed here should be the dimensions of 00097 the expected OUTPUT array, not the dimensions of the input 00098 array. */ 00099 FourierInvEngine(const Dims& d); 00100 00101 /// Destructor. 00102 ~FourierInvEngine(); 00103 00104 /// Compute the complex->real inverse fourier transform of x. 00105 /** The input is expected to be in the format/size generated by 00106 FourierEngine::fft(). */ 00107 Image<T> ifft(const Image<std::complex<T> >& x); 00108 00109 private: 00110 FourierInvEngine(const FourierInvEngine&); 00111 FourierInvEngine& operator=(const FourierInvEngine&); 00112 00113 const Dims itsOutputDims; 00114 std::complex<T>* itsSrc; 00115 T* itsDst; 00116 void* itsPlan; 00117 }; 00118 00119 /// Complex magnitude. 00120 template <class T> 00121 Image<T> magnitude(const Image<std::complex<T> >& img); 00122 00123 /// Logarithm of complex magnitude. 00124 template <class T> 00125 Image<T> logmagnitude(const Image<std::complex<T> >& img); 00126 00127 /// Compute the complex phase of the fourier result. 00128 template <class T> 00129 Image<T> phase(const Image<std::complex<T> >& img); 00130 00131 /// Morph a fourier result image from polar to cartesian coordinates. 00132 template <class T> 00133 Image<T> cartesian(const Image<T>& in, 00134 const Dims& dims); 00135 00136 // ############################################################## 00137 /* So things look consistent in everyone's emacs... */ 00138 /* Local Variables: */ 00139 /* indent-tabs-mode: nil */ 00140 /* End: */ 00141 00142 #endif // IMAGE_FOURIERENGINE_H_DEFINED