GaborFilter.cpp

Go to the documentation of this file.
00001 /* Import from Gabor API
00002 
00003 Copyright (c) 2002-3 Adriaan Tijsseling
00004 
00005 
00006                              All Rights Reserved
00007 
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with this program. If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 /*
00023 Description:    Implementation for GaborFilter class
00024 Author:                 Adriaan Tijsseling (AGT)
00025 Copyright:              (c) Copyright 2002 Adriaan Tijsseling. All rights reserved.
00026 Change History (most recent first):
00027         18/04/2002 - AGT - initial version
00028 */
00029 
00030 #include <cstring>
00031 #include "GaborFilter.h"
00032 
00033 #ifndef M_PI
00034 #define M_PI 3.1415926535897932384626433832795
00035 #endif
00036 
00037 namespace celeste
00038 {
00039 // default constructor just sets everything to default
00040 GaborFilter::GaborFilter()
00041 {
00042         mRadius = 16;
00043         mSigma = (float)(M_PI);
00044         mAngle = 0;
00045         mPhase = 0;
00046         mFrequency = 1.0;
00047         mReal = NULL;
00048         mImaginary = NULL;
00049 }
00050 
00051 // destructor: free up memory
00052 GaborFilter::~GaborFilter()
00053 {
00054         int i;
00055         
00056         if ( mReal != NULL )
00057         {
00058                 for ( i = 0; i < mRadius; i++ ) delete[] mReal[i];
00059                 delete[] mReal;
00060         }
00061         if ( mImaginary != NULL )
00062         {
00063                 for ( i = 0; i < mRadius; i++ ) delete[] mImaginary[i];
00064                 delete[] mImaginary;
00065         }
00066 }
00067 
00068 
00069 // set up the filter
00070 void GaborFilter::Initialize( int radius, float a, float f, float s, float p )
00071 {
00072         float x, y, exponential, sincos;
00073         
00074 // set internal variables
00075         mRadius = 2 * radius;
00076         mXYO = radius;  // origin of filter
00077         mSigma = s;
00078         mAngle = a;
00079         mPhase = p;
00080         mFrequency = (float)(f * M_PI / 2.0);
00081         
00082 // allocate memory for this filter
00083         mReal           = new float*[mRadius];          // real part of filter
00084         mImaginary      = new float*[mRadius];          // imaginary part of filter
00085 
00086 // initialize values of filter
00087         for ( int i = 0; i < mRadius; i++ )
00088         {
00089                 mReal[i]          = new float[mRadius];
00090                 mImaginary[i] = new float[mRadius];
00091                 
00092                 for ( int j = 0; j < mRadius; j++ )
00093                 {
00094                 // offset from origin
00095                         y = (float)( i - mXYO );
00096                         x = (float)( j - mXYO );
00097                         
00098                 // calculate exponential part
00099                         exponential = exp( - ( x*x + y*y ) / mSigma );
00100                         
00101                 // calculate sin-cos sum
00102                         sincos = mFrequency * ( y * cos( mAngle ) - x * sin( mAngle ) );
00103                         mReal[i][j]      = exponential * sin( sincos );
00104                         mImaginary[i][j] = exponential * ( cos( sincos ) - exp( (float)((-1.0*M_PI*M_PI)/2.0f) ) );
00105                 }
00106         }
00107 }
00108 
00109 
00110 // save the filter image
00111 void GaborFilter::Save( char* file, int angle, int freq )
00112 {
00113         PGMImage        pgmImage;
00114         char            filename[256];
00115         char            suffix[32];
00116         
00117         strcpy( filename, file );
00118         sprintf( suffix, "gf_i_%d_%d.pgm", angle, freq );
00119         strcat( filename, suffix );
00120         pgmImage.WriteScaled( filename, mImaginary, mRadius, mRadius );
00121         strcpy( filename, file );
00122         sprintf( suffix, "gf_r_%d_%d.pgm", angle, freq );
00123         strcat( filename, suffix );
00124         pgmImage.WriteScaled( filename, mReal, mRadius, mRadius );
00125 }
00126 
00127 }; // namespace

Generated on 29 Nov 2014 for Hugintrunk by  doxygen 1.4.7