/* * mp_GaborKernels * * Author: Andrew Salamon * Date: Mon Apr 24 2006 * * Copyright (c) 2006 Machine Perception Laboratory * University of California San Diego. * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ #ifndef __MP_GABORKERNELS_H__ #define __MP_GABORKERNELS_H__ #include #include #include #include "kernels-96x96/kernels96x96.h" /** A class for loading and storing Gabor kernel data. * Currently we only support 96x96 kernels. * Currently it uses data in header and object files that are compiled into the application * but this will probably be changed to have them read from files. */ template< typename T > class MP_GaborKernels { public: typedef typename std::complex mpComplex; MP_GaborKernels( int M_, int N_ ); /// Create a GaborKernel object with specified width and height ~MP_GaborKernels(); const mpComplex *getKernel( int ni, int mu ); private: int M; int N; typedef std::pair mpKernelIndex; typedef std::map< mpKernelIndex, mpComplex * > kernelMap; // a map of currently loaded kernels kernelMap kernels; const mpComplex *loadKernel( int ni, int mu ); }; template< typename T > MP_GaborKernels::MP_GaborKernels( int M_, int N_ ) : M(M_), N(N_), kernels() { } template< typename T > MP_GaborKernels::~MP_GaborKernels() { // walk through all of the loaded weights and free them for( typename kernelMap::iterator iter = kernels.begin(); iter != kernels.end(); iter++ ) { free((void *)iter->second); } } template< typename T > const typename MP_GaborKernels::mpComplex * MP_GaborKernels::getKernel( int ni, int mu ) { mpKernelIndex index(ni,mu); if( kernels.find( index ) == kernels.end() ) return loadKernel( ni, mu ); return kernels[ index ]; } template< typename T > const typename MP_GaborKernels::mpComplex * MP_GaborKernels::loadKernel( int ni, int mu ) { const int minNI = -2; const int countMU = 8; int index = (ni-minNI)*countMU + mu; // double *tmpKernel = kernels96x96[index]; // double *test = kernels96x96_ker_n1_0; // Grab the correct function for this ni-mu combo. // The kernelAccess function will return all of the data for the chosen kernel // I used access functions rather than trying to get a pointer to eac kernel array directly // because I could not get that method to work. // We still need to make it possible to select based on M and N, rather than hard coding them kernelAccess kernFunc = kernelFuncs96x96[index]; //typedef double (*kernelAccess)(int); //extern kernelAccess kernelFuncs[]; // now load into our complex array int count=M*N*2; mpComplex *comp = (mpComplex *)malloc( sizeof(mpComplex) * count ); for( int x=0, cmp=0; x < count; x+=2, ++cmp ) comp[cmp] = mpComplex( kernFunc(x), kernFunc(x+1) ); // comp[x] = mpComplex( tmpKernel[x], tmpKernel[x+1] ); mpKernelIndex kindex(ni,mu); kernels[ kindex ] = comp; return comp; } #endif