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         mFilters        = NULL;
00047         mFiducials      = NULL;
00048     mAngles = 0;
00049     mFreqs = 0;
00050     mRadius = 0;
00051 }
00052 
00053 // destructor: free up memory
00054 GaborJet::~GaborJet()
00055 {
00056         if ( mFilters != NULL )
00057         {
00058                 for ( int i = 0; i < mAngles; i++ ) delete[] mFilters[i];
00059                 delete[] mFilters;
00060         }
00061         if ( mFiducials != NULL ) delete[] mFiducials;  
00062 }
00063 
00064 
00065 // set up the filter
00066 void GaborJet::Initialize( int y, int x, int x0, int y0, int r, 
00067     float s, int f, float maxF, float minF, int a, char* file)
00068 {
00069         int             i, j;
00070         float   freq;
00071         
00072 // set internal variables
00073         mHeight         = y;
00074         mWidth          = x;
00075         mX                      = x0;
00076         mY                      = y0;
00077         float sigma     = (float)(s * M_PI * M_PI);
00078         mAngles         = a;
00079         mFreqs          = f;
00080         mRadius         = r;
00081         mFiducials = new float[mAngles * mFreqs];
00082         
00083 // allocate memory for filters (angles * freqs = total filters)
00084         mFilters = new GaborFilter * [mAngles];
00085         for ( i = 0; i < mAngles; i++ )
00086         {
00087         // calculate angle
00088                 float angle = (float)((float)i * M_PI / (float)mAngles);
00089                 
00090         // allocate filters for this angle
00091                 mFilters[i] = new GaborFilter[mFreqs];  
00092                 
00093         // initialize each one  
00094                 for ( j = 0; j < mFreqs; j++ )
00095                 {
00096                 // calculate frequency
00097                         freq = minF + ( j * ( maxF - minF ) ) / (float)mFreqs;
00098                         
00099                 // initialize filter
00100                         mFilters[i][j].Initialize( mRadius, angle, freq, sigma );
00101             if (file!=NULL && strlen(file)>0)
00102             {
00103                 mFilters[i][j].Save(file, i, j);
00104             };
00105                 }
00106         }       
00107 }
00108 
00109 
00110 // process an image
00111 void GaborJet::Filter( float** image, int* len )
00112 {       
00113         int                     x, y;           // iterating over location
00114         int                     gx, gy;         // iterating over filters
00115         int                     a, f;           // iterating over angles and frequencies
00116         int                     h, i, j;        // iterating over filter field
00117         float           sumI, sumR;     // sum of imaginary and of real parts
00118         
00119         if ( kVerbosity ) cerr << "convoluting..." << endl;
00120 
00121 // convolve at center of filter location
00122         // collect responses over angles and frequencies
00123         h = 0;
00124         for ( a = 0; a < mAngles; a++ )
00125         {
00126                 for ( f = 0; f < mFreqs; f++ )
00127                 {
00128                         sumR = 0.0;
00129                         sumI = 0.0;
00130 
00131                 // start from bottom-left corner of filter location
00132                         y = mY - mRadius;
00133                         for ( gy = y; gy < y + 2 * mRadius; gy++ )
00134                         {
00135                         // make sure we are not out of bounds
00136                                 if ( gy < 0 || gy >= mHeight ) break;
00137                                 
00138                         // offset to local coordinates of filter
00139                                 i = gy - y;
00140                                 
00141                                 x = mX - mRadius;
00142                                 for ( gx = x; gx < x + 2 * mRadius; gx++ )
00143                                 {
00144                                 // make sure we are not out of bounds
00145                                         if ( gx < 0 || gx >= mWidth ) break;
00146 
00147                                 // offset to local coordinates of filter
00148                                         j = gx - x;
00149 
00150                                         sumR += image[gy][gx] * mFilters[a][f].GetReal(i,j);
00151                                         sumI += image[gy][gx] * mFilters[a][f].GetImaginary(i,j);
00152                                 }
00153                         }
00154                         mFiducials[h] = sqrt( sumR*sumR + sumI*sumI );
00155                         h++;
00156                 } // f
00157         } // a
00158 
00159         *len = mAngles * mFreqs;
00160 }
00161 
00162 }; // namespace

Generated on 1 Sep 2015 for Hugintrunk by  doxygen 1.4.7