LogPolar.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:            class for log polar filter
00024         Original Author:        Takio Kurita
00025         Ported by:                      Adriaan Tijsseling (AGT)
00026 */
00027 
00028 #include "LogPolar.h"
00029 #include "CelesteGlobals.h"
00030 
00031 #ifndef M_PI
00032 #define M_PI 3.1415926535897932384626433832795
00033 #endif
00034 
00035 namespace celeste
00036 {
00037 // construct class and apply filter
00038 LogPolar::LogPolar( float** img, int height, int width, int minS, int ry, int rx )
00039 {
00040         mImgHeight = height;
00041         mImgWidth = width;
00042         mHeight = ry;
00043         mWidth = rx;
00044         mMinHW = minS;
00045         
00046         // allocate output image
00047         mPolarized = new float*[mHeight];
00048         for ( int i = 0; i < mHeight; i++ )
00049         {
00050                 mPolarized[i] = new float[mWidth];
00051                 for ( int j = 0; j < mWidth; j++ )
00052                         mPolarized[i][j] = 0.0;
00053         }
00054         // allocate coordinates image
00055         mCoords = new float*[mImgHeight];
00056         for ( int i = 0; i < mImgHeight; i++ )
00057         {
00058                 mCoords[i] = new float[mImgWidth];
00059                 for ( int j = 0; j < mImgWidth; j++ )
00060                         mCoords[i][j] = 0.0;
00061         }
00062         // apply filter
00063         ApplyFilter( img, height, width );
00064 }
00065 
00066 
00067 // free memory
00068 LogPolar::~LogPolar()
00069 {
00070         if( mCoords != NULL )
00071         {
00072                 for( int y = 0; y < mImgHeight; y++ )
00073                         delete[] mCoords[y];
00074                 delete[] mCoords;
00075         }
00076         if( mPolarized != NULL )
00077         {
00078                 for( int y = 0; y < mHeight; y++ )
00079                         delete[] mPolarized[y];
00080                 delete[] mPolarized;
00081         }
00082 }
00083 
00084 
00085 // apply filter to image
00086 void LogPolar::ApplyFilter( float** img, int height, int width )
00087 {
00088         float   rho, theta, x, y, sum;
00089         int             i, j, k, l, f, g;
00090 
00091         for( k = 0; k < mHeight; k++ )
00092         {
00093                 theta = (float)(2.0 * M_PI * (float)k / (float)mHeight);
00094 
00095                 for( l = 0; l < mWidth; l++ )
00096                 {
00097                         rho = exp( log( (float)((float)mMinHW / 2.0) ) * (float)l / (float)mWidth );
00098 
00099                         x = rho * cos( theta );   
00100                         y = rho * sin( theta );
00101 
00102                         if ( x >= 0.0 ) x += 0.5;
00103                         else x -= 0.5;
00104                         if ( y >= 0.0 ) y += 0.5;
00105                         else y -= 0.5;
00106 
00107                         f = (int)(y) + (int)height/2;  
00108                         g = (int)(x) + (int)width/2;
00109 
00110                         sum = 0.0;
00111                         for( i = f-1; i <= f+1; i++ )
00112                                 for( j = g-1; j <= g+1; j++ )
00113                                         sum += img[i][j];
00114                         
00115                         mPolarized[k][l] = sum / 9.0f;
00116                         mCoords[f][g] = 255.0f;
00117                 }
00118         }
00119 }
00120 
00121 
00122 // write out contrast data to pgm file
00123 void LogPolar::Save( void )
00124 {
00125     PGMImage    pgmI;
00126         char            tmpname[256];
00127         
00128         strcpy( tmpname, mFile );
00129         strcat( tmpname, "-lp-hist.pgm" );
00130         pgmI.WriteScaled( tmpname, mPolarized, mHeight, mWidth );
00131         if ( kSaveFilter )
00132         {
00133                 strcpy( tmpname, mFile );
00134                 strcat( tmpname, "-lp-img.pgm" );
00135                 pgmI.WriteScaled( tmpname, mCoords, mImgHeight, mImgWidth );
00136         }
00137 }
00138 
00139 }; // namespace

Generated on Wed Jul 16 01:25:29 2014 for Hugintrunk by  doxygen 1.3.9.1