GaborJet.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 GaborJet class
00024         Author:                 Adriaan Tijsseling (AGT)
00025         Copyright:              (c) Copyright 2002-3 Adriaan Tijsseling. All rights reserved.
00026 */
00027 
00028 #include "GaborJet.h"
00029 #include "CelesteGlobals.h"
00030 
00031 #ifndef M_PI
00032 #define M_PI 3.1415926535897932384626433832795
00033 #endif
00034 
00035 using namespace std; 
00036 
00037 namespace celeste
00038 {
00039 // default constructor just sets everything to default
00040 GaborJet::GaborJet()
00041 {
00042         mHeight         = 512;
00043         mWidth          = 512;
00044         mX                      = 128;
00045         mY                      = 128;
00046         mShowFilter = false;
00047         mFilters        = NULL;
00048         mFiducials      = NULL;
00049 }
00050 
00051 // destructor: free up memory
00052 GaborJet::~GaborJet()
00053 {
00054         if ( mFilters != NULL )
00055         {
00056                 for ( int i = 0; i < mAngles; i++ ) delete[] mFilters[i];
00057                 delete[] mFilters;
00058         }
00059         if ( mFiducials != NULL ) delete[] mFiducials;  
00060 }
00061 
00062 
00063 // set up the filter
00064 void GaborJet::Initialize( int y, int x, int x0, int y0, int r, 
00065                                                    float s, int f, float maxF, float minF, int a, bool save )
00066 {
00067         int             i, j;
00068         float   angle, freq;
00069         
00070 // set internal variables
00071         mHeight         = y;
00072         mWidth          = x;
00073         mX                      = x0;
00074         mY                      = y0;
00075         mSigma          = (float)(s * M_PI * M_PI);
00076         mAngles         = a;
00077         mFreqs          = f;
00078         mRadius         = r;
00079         mMinFreq        = minF;
00080         mMaxFreq        = maxF;
00081         mShowFilter = save;
00082         mFiducials = new float[mAngles * mFreqs];
00083         
00084 // allocate memory for filters (angles * freqs = total filters)
00085         mFilters = new GaborFilter * [mAngles];
00086         for ( i = 0; i < mAngles; i++ )
00087         {
00088         // calculate angle
00089                 angle = (float)((float)i * M_PI / (float)mAngles);
00090                 
00091         // allocate filters for this angle
00092                 mFilters[i] = new GaborFilter[mFreqs];  
00093                 
00094         // initialize each one  
00095                 for ( j = 0; j < mFreqs; j++ )
00096                 {
00097                 // calculate frequency
00098                         freq = minF + ( j * ( maxF - minF ) ) / (float)mFreqs;
00099                         
00100                 // initialize filter
00101                         mFilters[i][j].Initialize( mRadius, angle, freq, mSigma );
00102                         if ( mShowFilter ) mFilters[i][j].Save( mFile, i, j );
00103                 }
00104         }       
00105 }
00106 
00107 
00108 // process an image
00109 void GaborJet::Filter( float** image, int* len )
00110 {       
00111         int                     x, y;           // iterating over location
00112         int                     gx, gy;         // iterating over filters
00113         int                     a, f;           // iterating over angles and frequencies
00114         int                     h, i, j;        // iterating over filter field
00115         float           sumI, sumR;     // sum of imaginary and of real parts
00116         
00117         if ( kVerbosity ) cerr << "convoluting..." << endl;
00118 
00119 // convolve at center of filter location
00120         // collect responses over angles and frequencies
00121         h = 0;
00122         for ( a = 0; a < mAngles; a++ )
00123         {
00124                 for ( f = 0; f < mFreqs; f++ )
00125                 {
00126                         sumR = 0.0;
00127                         sumI = 0.0;
00128 
00129                 // start from bottom-left corner of filter location
00130                         y = mY - mRadius;
00131                         for ( gy = y; gy < y + 2 * mRadius; gy++ )
00132                         {
00133                         // make sure we are not out of bounds
00134                                 if ( gy < 0 || gy >= mHeight ) break;
00135                                 
00136                         // offset to local coordinates of filter
00137                                 i = gy - y;
00138                                 
00139                                 x = mX - mRadius;
00140                                 for ( gx = x; gx < x + 2 * mRadius; gx++ )
00141                                 {
00142                                 // make sure we are not out of bounds
00143                                         if ( gx < 0 || gx >= mWidth ) break;
00144 
00145                                 // offset to local coordinates of filter
00146                                         j = gx - x;
00147 
00148                                         sumR += image[gy][gx] * mFilters[a][f].GetReal(i,j);
00149                                         sumI += image[gy][gx] * mFilters[a][f].GetImaginary(i,j);
00150                                 }
00151                         }
00152                         mFiducials[h] = sqrt( sumR*sumR + sumI*sumI );
00153                         h++;
00154                 } // f
00155         } // a
00156 
00157         *len = mAngles * mFreqs;
00158 }
00159 
00160 }; // namespace

Generated on Tue Sep 30 01:25:29 2014 for Hugintrunk by  doxygen 1.3.9.1