00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
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
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
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
00063 ApplyFilter( img, height, width );
00064 }
00065
00066
00067
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
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
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 };