Hugin trunk 0.1
Loading...
Searching...
No Matches
GaborFilter.cpp
Go to the documentation of this file.
1/* Import from Gabor API
2
3Copyright (c) 2002-3 Adriaan Tijsseling
4
5
6 All Rights Reserved
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22/*
23Description: Implementation for GaborFilter class
24Author: Adriaan Tijsseling (AGT)
25Copyright: (c) Copyright 2002 Adriaan Tijsseling. All rights reserved.
26Change History (most recent first):
27 18/04/2002 - AGT - initial version
28*/
29
30#include <cstring>
31#include "GaborFilter.h"
32
33#ifndef M_PI
34#define M_PI 3.1415926535897932384626433832795
35#endif
36
37namespace celeste
38{
39// default constructor just sets everything to default
41{
42 mRadius = 16;
43 mXYO = 8;
44 mSigma = (float)(M_PI);
45 mAngle = 0;
46 mPhase = 0;
47 mFrequency = 1.0;
48 mReal = NULL;
49 mImaginary = NULL;
50}
51
52// destructor: free up memory
54{
55 int i;
56
57 if ( mReal != NULL )
58 {
59 for ( i = 0; i < mRadius; i++ ) delete[] mReal[i];
60 delete[] mReal;
61 }
62 if ( mImaginary != NULL )
63 {
64 for ( i = 0; i < mRadius; i++ ) delete[] mImaginary[i];
65 delete[] mImaginary;
66 }
67}
68
69
70// set up the filter
71void GaborFilter::Initialize( int radius, float a, float f, float s, float p )
72{
73 float x, y, exponential, sincos;
74
75// set internal variables
76 mRadius = 2 * radius;
77 mXYO = radius; // origin of filter
78 mSigma = s;
79 mAngle = a;
80 mPhase = p;
81 mFrequency = (float)(f * M_PI / 2.0);
82
83// allocate memory for this filter
84 mReal = new float*[mRadius]; // real part of filter
85 mImaginary = new float*[mRadius]; // imaginary part of filter
86
87// initialize values of filter
88 for ( int i = 0; i < mRadius; i++ )
89 {
90 mReal[i] = new float[mRadius];
91 mImaginary[i] = new float[mRadius];
92
93 for ( int j = 0; j < mRadius; j++ )
94 {
95 // offset from origin
96 y = (float)( i - mXYO );
97 x = (float)( j - mXYO );
98
99 // calculate exponential part
100 exponential = exp( - ( x*x + y*y ) / mSigma );
101
102 // calculate sin-cos sum
103 sincos = mFrequency * ( y * cos( mAngle ) - x * sin( mAngle ) );
104 mReal[i][j] = exponential * sin( sincos );
105 mImaginary[i][j] = exponential * ( cos( sincos ) - exp( (float)((-1.0*M_PI*M_PI)/2.0f) ) );
106 }
107 }
108}
109
110
111// save the filter image
112void GaborFilter::Save( char* file, int angle, int freq )
113{
114 PGMImage pgmImage;
115 char filename[256];
116 char suffix[32];
117
118 strcpy( filename, file );
119 sprintf( suffix, "gf_i_%d_%d.pgm", angle, freq );
120 strcat( filename, suffix );
121 pgmImage.WriteScaled( filename, mImaginary, mRadius, mRadius );
122 strcpy( filename, file );
123 sprintf( suffix, "gf_r_%d_%d.pgm", angle, freq );
124 strcat( filename, suffix );
125 pgmImage.WriteScaled( filename, mReal, mRadius, mRadius );
126}
127
128}; // namespace
#define M_PI
void Initialize(int radius, float a, float f, float s, float p=0)
void Save(char *file, int angle, int freq)
void WriteScaled(char *filename, float **output, int height, int width)
Definition PGMImage.cpp:334