Home || Architecture || Video Search || Visual Search || Scripts || Applications || Important Messages || OGL || Src

Octaves.h

Go to the documentation of this file.
00001 #ifndef Impala_Core_Array_Octaves_h
00002 #define Impala_Core_Array_Octaves_h
00003 
00004 #include "Core/Array/Set.h"
00005 #include "Core/Array/Sub.h"
00006 #include "Core/Array/Add.h"
00007 #include "Core/Array/LocalMinMax.h"
00008 #include "Core/Array/Scale.h"
00009 #include "Core/Array/Rotate.h"
00010 #include "Core/Array/PixMinMax.h"
00011 #include "Core/Array/Mul.h"
00012 #include "Core/Array/MulVal.h"
00013 #include "Core/Array/AddVal.h"
00014 #include "Core/Array/WritePng.h"
00015 #include "Core/Array/GaussDerivative.h"
00016 #include "Core/Array/RecGauss.h"
00017 #include "Core/Array/MakeRoi.h"
00018 #include "Core/Array/MakeGaussian2d.h"
00019 #include "Core/Array/Array2dTem.h"
00020 #include "Core/Array/PrintData.h"
00021 
00022 #include "Core/Feature/FeatureTable.h"
00023 
00024 #include "Core/Histogram/Histogram1dTem.h"
00025 #include "Core/Histogram/MakeHistogram1d.h"
00026 
00027 #include "Core/Array/RecGauss.h"
00028 #include "Core/Matrix/MatMulVec.h"
00029 
00030 //#include "Core/Feature/SiftFeatureTable.h"
00031 //
00032 #include "Core/Table/TableTem.h"
00033 #include "Core/Column/ColumnTem.h"
00034 #include <math.h>
00035 
00036 #ifndef PI
00037 #define PI 3.14159265358979324
00038 #endif
00039 
00040 //using namespace Impala::Core::Matrix;
00041 //using namespace Impala::Core::Feature;
00042 
00043 namespace Impala{
00044 namespace Core{
00045 namespace Array{
00047 //This class represents the multiresolution octaves of the given array.
00048 //
00049 //Given a number of octaves, and number of scales at each octave, with an
00050 //initial sigma value, It calculates the necessary scalings of the sigma 
00051 //parameter to be used for gaussian blurring
00052 //
00053 //
00054 class Octaves{
00055 public:
00056 
00057 private:
00058     ILOG_VAR_DEC;
00059     Impala::Real64 mSigma0;
00060     Impala::Real64 mSigmaN;
00061     int mOctaves;
00062     int mMinOctave;
00063     int mLevels;
00064     int mScales;
00065     double * mSigmas;
00066     double mSigmaK;
00067     Array2dVec3Real64* mSrcVec3;
00068     Array2dScalarReal64* mSrcScalar;
00069     Array2dScalarReal64** mOctaveArray;
00070     Array2dScalarReal64** mSrcArray;
00071 
00072 
00073     bool mRecGauss;
00074     bool mResample;
00075     bool mRecursive;
00076 
00077 public:
00078     Octaves(Array2dScalarUInt8* src, int Octaves, int Levels, int MinOctave=0)
00079     : mOctaves(Octaves),
00080     mMinOctave(MinOctave),
00081     mLevels(Levels),
00082     mRecGauss(true),
00083     mRecursive(true),
00084     mSrcScalar(0)
00085     {
00086         CmdOptions& options = CmdOptions::GetInstance();
00087         mRecGauss=options.GetBool("recGauss",false);
00088         mResample=options.GetBool("resample",false);
00089         mRecursive=options.GetBool("recursive",false);
00090 
00091 
00092         ILOG_INFO("Using RecGauss  : "<<(mRecGauss?"yes":"no"));
00093         ILOG_INFO("Using Resampling: "<<(mResample?"yes":"no"));
00094         ILOG_INFO("All Recursive   : "<<(mRecursive?"yes":"no"));
00095         //Copy the input array to our Real64 array
00096         Real64 min,max;
00097         Set(mSrcScalar,src);
00098         PixMinMax(src,&min,&max);
00099         ILOG_INFO("Input image Min-Max:"<<min<<" "<<max);
00100         Normalize(mSrcScalar);
00101         PixMinMax(mSrcScalar,&min,&max);
00102         ILOG_INFO("Input image Min-Max:"<<min<<" "<<max);
00103         Init();
00104     }
00105     ~Octaves(){
00106         delete [] mSigmas;
00107         
00108         for(int i=0;i<mScales*mOctaves;i++)
00109             delete mOctaveArray[i];
00110         delete [] mOctaveArray;
00111 
00112         for(int i=0;i<mOctaves;i++)
00113             delete mSrcArray[i];
00114         delete [] mSrcArray;
00115 
00116 
00117     }
00118     void Normalize(Array2dScalarReal64* s){
00119         Real64 min,max;
00120         PixMinMax(s,&min,&max);
00121         AddVal(s,s,-min);
00122         MulVal(s,s,1/(max-min));
00123     }
00124 
00125     int GetOctaveCount(){
00126         return mOctaves;
00127     }
00128     
00129     int GetLevelCount(){
00130         return mLevels;
00131     }
00132 
00133     int GetScaleCount(){
00134         return mScales;
00135     }
00136 
00137     void Init(){
00139         //If a negative count is given for the octaves, we calculate the max
00140         //possible, deducing it from the image size. A minimum size of 8x8 
00141         //array is produced from the original array
00142         if(mOctaves<1)
00143         {
00144             mOctaves = Max(1,
00145             int(std::floor(log2(Min(mSrcScalar->CW(),mSrcScalar->CH()))) 
00146                            - mMinOctave - 3));
00147         }
00149         //We need levels+3 in each octave, since they will first be subtracted,
00150         //and then the subtration will be used for extrema localization
00151         //within +/- 1 scales
00152         mScales=mLevels+3;
00153 
00154         ILOG_INFO("Initializing "<<mOctaves
00155                 <<" octaves, each with "<<mLevels<<" levels and "
00156                 <<mScales<<" scales.");
00157         ILOG_INFO((mResample?"Resampling Enabled":"Resampling Disabled"));
00158 
00159         
00160         mSigmas = new double[mOctaves*mScales];
00162         mOctaveArray = new  Array2dScalarReal64*[mOctaves*mScales];
00163         for(int i=0;i<mOctaves*mScales;i++)
00164             mOctaveArray[i]=0;
00165 
00167         //mSrcArray holds the images that will be gaussian smoothed,
00168         //for first octave it's the original image
00169         //for octaves below, it is calculated from the gaussian smoothing with
00170         //2*sigma
00171         mSrcArray = new  Array2dScalarReal64*[mOctaves];
00172         for(int i=0;i<mOctaves;i++)
00173             mSrcArray[i]=0;
00174 
00175         
00176         ILOG_INFO("Octave structures constructed!");
00177         BuildOctave();
00178         ILOG_INFO("Octave structures initialized!");
00179 
00180         
00181     }
00182 
00183     void BuildOctave(){
00185         //Calculate sigmas for the first levels of each octave
00186         //Each sigma is double its counterpart in the octave below
00187         //
00188         //This was 0.5 before, for prior smoothing
00189         mSigmas[0] = 1;
00190 
00191         for(int o=1;o<mOctaves;o++)
00192         {
00193             mSigmas[o*mScales] = 2 * mSigmas[(o-1)*mScales];
00194         }
00195 
00197         //mSigmaK is the ratio of sigmas between successive levels
00198         mSigmaK = powf(2.0,1.0/mLevels);
00199 
00201         //Calculate the sigma values of other levels.
00202         //Depending on the number of levels in each octave, we divide the sigmas
00203         //logarithmically, each scaled by 2^(1/mLevels)
00204         for(int o=0;o<mOctaves;o++){
00205             std::ostringstream oss;
00206             oss<<"Octave "<<o<<" :";
00207             for(int l=0;l<mScales;l++)
00208             {
00209                 mSigmas[o*mScales+l] = mSigmas[o*mScales] * powf(mSigmaK,l);
00210                 oss<<" "<< mSigmas[o*mScales+l];
00211             }
00212             ILOG_INFO(oss.str());
00213         }
00214 
00216         //Calculate the gaussian smoothed versions of the image, 
00217         //given the sigma values for each level
00218         for(int o=0;o<mOctaves;o++)
00219         {
00220             //Calculate the initial image, 
00221 
00222             //If it's not the first octave, the image is resampled from 2*sigma
00223             //of above octaves first level.
00224             if(mResample)
00225             {
00226                 if((o==0))
00227                 {
00228                     //The first array is the linear upsampling of the original
00229                     mSrcArray[o] = 0;
00230                     Scale(mSrcArray[o], mSrcScalar,2,2,Geometry::LINEAR);
00231 
00232                 }else{
00233                     //resample the scale that has double the sigma of 
00234                     //previous octave's first scale
00235                     Array2dScalarReal64* src=mOctaveArray[(o-1)*mScales+mLevels];
00236                     mSrcArray[o] = 0;
00237                     Scale(mSrcArray[o],src,.5,.5,Geometry::NEAREST);
00238                 }
00239             }
00240             else
00241             {
00243                 //Don't resample the image from source, use directly
00244                 //This causes all the analysis to be done in the same resolution
00245                 if(o==0){
00246                     mSrcArray[o] = 0;
00247                     Set(mSrcArray[o], mSrcScalar);
00248                 }else{
00249                     mSrcArray[o] = 0;
00250                     Set(mSrcArray[o],mOctaveArray[(o-1)*mScales+mLevels]);
00251                 }
00252 
00253             }
00255             //The source image for this octave is ready
00256             //Do any prior smoothing
00257             
00258 
00259             for(int l=0;l<mScales;l++)
00260             {
00261                 //ILOG_DEBUG("Octave "<<o<<" Level "<<l<<" Src:"<<mSrcArray[o]);
00262                 if(mRecGauss)
00263                 {
00264                     if(mRecursive)
00265                     {
00266                         //Calculate each gaussian smoothing recursively
00267                         Array2dScalarReal64* src;
00269                         //If it's the first level in the octave, 
00270                         //we should use the array we calculated in the previous
00271                         //step, else we use the previous level in the octave
00272                         if(l==0)
00273                             src = mSrcArray[o];
00274                         else
00275                             src = mOctaveArray[o*mScales+l-1];
00276 
00277                         RecGauss(mOctaveArray[o*mScales+l],
00278                                  src,
00279                                  mSigmaK,
00280                                  mSigmaK,0,0,3);
00281                     }else{
00282                         //Calculate each gaussian smoothing directly using
00283                         //recursive gauss
00284                         RecGauss(mOctaveArray[o*mScales+l],
00285                              mSrcArray[o],
00286                              mSigmas[l],
00287                              mSigmas[l],
00288                              0,0,3);
00289 
00290                     }
00291                 }
00292                 else
00293                 {
00294                     if(mRecursive)
00295                     {
00296                         Array2dScalarReal64* src; 
00297                         if(l==0)
00298                             src = mSrcArray[o];
00299                         else
00300                             src = mOctaveArray[o*mScales+l-1];
00301                         
00302                         GaussDerivative(mOctaveArray[o*mScales+l],
00303                              src,
00304                              mSigmaK,0,0,3);
00305 
00306                     }else{
00307                         GaussDerivative(mOctaveArray[o*mScales+l],
00308                              mSrcArray[o],
00309                              mSigmas[l],
00310                              0,0,3);
00311                     }
00312                 }
00313                 Real64 min=0;
00314                 Real64 max=0;
00315                 PixMinMax(mOctaveArray[o*mScales+l],&min,&max);
00316                 //ILOG_DEBUG("Gaussian @ "<<o<<"x"<<l<<" : ("<<min<<","<<max<<")");
00317             }
00318         }
00319     }
00320 
00321     Array2dScalarReal64* GetOctave(int o,int l)
00322     {
00323         return mOctaveArray[o*mScales+l];
00324     }
00325     
00326 
00327     double GetSigma(int o, int l){
00328         return mSigmas[o*mScales+l];
00329     }
00330     bool IsResamplingEnabled(){
00331         return mResample;    
00332     }
00333 
00334 };
00335 ILOG_VAR_INIT(Octaves, Impala.Core.Array);
00336 
00337 }}}//Namespace
00338 
00339 
00340 
00341 #endif
00342 
00343 

Generated on Fri Mar 19 09:30:49 2010 for ImpalaSrc by  doxygen 1.5.1