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         mXYO = 8;
00044         mSigma = (float)(M_PI);
00045         mAngle = 0;
00046         mPhase = 0;
00047         mFrequency = 1.0;
00048         mReal = NULL;
00049         mImaginary = NULL;
00050 }
00051 
00052 // destructor: free up memory
00053 GaborFilter::~GaborFilter()
00054 {
00055         int i;
00056         
00057         if ( mReal != NULL )
00058         {
00059                 for ( i = 0; i < mRadius; i++ ) delete[] mReal[i];
00060                 delete[] mReal;
00061         }
00062         if ( mImaginary != NULL )
00063         {
00064                 for ( i = 0; i < mRadius; i++ ) delete[] mImaginary[i];
00065                 delete[] mImaginary;
00066         }
00067 }
00068 
00069 
00070 // set up the filter
00071 void GaborFilter::Initialize( int radius, float a, float f, float s, float p )
00072 {
00073         float x, y, exponential, sincos;
00074         
00075 // set internal variables
00076         mRadius = 2 * radius;
00077         mXYO = radius;  // origin of filter
00078         mSigma = s;
00079         mAngle = a;
00080         mPhase = p;
00081         mFrequency = (float)(f * M_PI / 2.0);
00082         
00083 // allocate memory for this filter
00084         mReal           = new float*[mRadius];          // real part of filter
00085         mImaginary      = new float*[mRadius];          // imaginary part of filter
00086 
00087 // initialize values of filter
00088         for ( int i = 0; i < mRadius; i++ )
00089         {
00090                 mReal[i]          = new float[mRadius];
00091                 mImaginary[i] = new float[mRadius];
00092                 
00093                 for ( int j = 0; j < mRadius; j++ )
00094                 {
00095                 // offset from origin
00096                         y = (float)( i - mXYO );
00097                         x = (float)( j - mXYO );
00098                         
00099                 // calculate exponential part
00100                         exponential = exp( - ( x*x + y*y ) / mSigma );
00101                         
00102                 // calculate sin-cos sum
00103                         sincos = mFrequency * ( y * cos( mAngle ) - x * sin( mAngle ) );
00104                         mReal[i][j]      = exponential * sin( sincos );
00105                         mImaginary[i][j] = exponential * ( cos( sincos ) - exp( (float)((-1.0*M_PI*M_PI)/2.0f) ) );
00106                 }
00107         }
00108 }
00109 
00110 
00111 // save the filter image
00112 void GaborFilter::Save( char* file, int angle, int freq )
00113 {
00114         PGMImage        pgmImage;
00115         char            filename[256];
00116         char            suffix[32];
00117         
00118         strcpy( filename, file );
00119         sprintf( suffix, "gf_i_%d_%d.pgm", angle, freq );
00120         strcat( filename, suffix );
00121         pgmImage.WriteScaled( filename, mImaginary, mRadius, mRadius );
00122         strcpy( filename, file );
00123         sprintf( suffix, "gf_r_%d_%d.pgm", angle, freq );
00124         strcat( filename, suffix );
00125         pgmImage.WriteScaled( filename, mReal, mRadius, mRadius );
00126 }
00127 
00128 }; // namespace

Generated on 2 Sep 2015 for Hugintrunk by  doxygen 1.4.7