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

DSurf.h

Go to the documentation of this file.
00001 #ifndef Impala_Core_Feature_DSurf_h
00002 #define Impala_Core_Feature_DSurf_h
00003 
00004 #include "Core/Array/IntegrateReduce.h"
00005 #include "Core/Feature/HaarResponse.h"
00006 #include "Core/Feature/PointDescriptorTable.h"
00007 #include "Util/TimeStats.h"
00008 
00009 namespace Impala
00010 {
00011 namespace Core
00012 {
00013 namespace Feature
00014 {
00015 
00016 void
00017 AddDSurfOptions(CmdOptions& options)
00018 {
00019     options.AddOption(0, "surf-haar-size",      "int", "3");
00020     options.AddOption(0, "surf-subregion-size", "int", "2");
00021     options.AddOption(0, "surf-spaciality",     "int", "4");
00022 }
00023 
00024 void
00025 GetDSurfOptions(CmdOptions& options, int& haarSize, int& subregionSize, int& spatiality)
00026 {
00027     haarSize = options.GetInt("surf-haar-size");
00028     subregionSize = options.GetInt("surf-subregion-size");
00029     spatiality = options.GetInt("surf-spaciality");
00030 }
00031     
00036 inline void
00037 ExtractFeatureIntoVector(Real64* vector,
00038                          std::vector<Array::Array2dScalarReal64*>& features,
00039                          int x, int y, int spatiality)
00040 {
00041     double length = 0.;
00042     int begin = 0;
00043     for(int f=0 ; f<features.size() ; ++f)
00044     {
00045         for(int j=y ; j<y+spatiality ; ++j)
00046             for(int i=x ; i<x+spatiality ; ++i)
00047             {
00048                 double v = features[f]->Value(i,j);
00049                 vector[begin++] = v;
00050                 length += v*v;
00051             }
00052     }
00053     // normalize the vector that we just added
00054     if(length != 0)
00055     {
00056         length = sqrt(length);
00057         for(int i=0 ; i<features.size() * spatiality * spatiality; i++)
00058             vector[i] /= length;
00059     }
00060 }
00061     
00064 Matrix::Mat*
00065 DSurf(Array::Array2dScalarReal64* image,
00066       PointDescriptorTable* pointData,
00067       int sampleRate, int numberHaarSum, int spatiality)
00068 {
00069     using namespace Matrix;
00070 
00071     ILOG_VAR(Impala.Core.Feature.DSurf);
00072     ILOG_DEBUG("image w="<< image->CW() <<", h="<< image->CH());
00073     /* In order to speed up processing reduce the image so one pixel has the
00074        same 'energy' as a block of sampleRate^2 pixels. */
00075     Array::Array2dScalarReal64* reduced = 0;
00076     IntegrateReduce(reduced, image, sampleRate);
00077     // haar responses
00078     std::vector<Array::Array2dScalarReal64*> haar = HaarResponse(reduced);
00079     delete reduced;
00080     // sum the haar responses into component of the surf feature
00081     std::vector<Array::Array2dScalarReal64*> components;
00082     int w = haar[0]->CW() / numberHaarSum;
00083     int h = haar[0]->CH() / numberHaarSum;
00084     for(int i=0 ; i<haar.size() ; ++i)
00085     {
00086         Array::Array2dScalarReal64* comp =
00087             new Array::Array2dScalarReal64(w, h, 0, 0);
00088         IntegrateReduceWeighted(comp, haar[i], numberHaarSum);
00089         components.push_back(comp);
00090         delete haar[i];
00091     }
00092     w = components[0]->CW();
00093     h = components[0]->CH();
00094     //now combine the components into actual surf features
00095     int nrFeatures = w*h;
00096     int sampleSize = sampleRate * numberHaarSum;
00097     int featureSize = sampleSize * spatiality;
00098     /* because the DSurf implementation does it's own sampling we need this loop
00099        to create the interest points with correct dimensions (actually only the
00100        middle point is needed) */
00101     if(pointData->Size() == 0)
00102     {
00103         for(int y=0 ; y<=h-spatiality ; ++y)
00104         {
00105             for(int x=0 ; x<=w-spatiality ; ++x)
00106             {
00107                 int left=x*sampleSize;
00108                 int top =y*sampleSize;
00109                 int centerX = static_cast<int>(((featureSize) / 2.0 + left) + 0.5);
00110                 int centerY = static_cast<int>(((featureSize) / 2.0 + top) + 0.5);
00111                 // this gives the same output as the old way of doing things,
00112                 // though the featureSize entries are not needed *twice*
00113                 pointData->Add(centerX, centerY, featureSize+1, featureSize+1, 0);
00114                 //Geometry::Rectangle r(left, top, left+featureSize, top+featureSize);
00115             }
00116         }
00117     }
00118     // here comes the actual creation of surf features
00119     /* we might speed this up as follows:
00120        - first allocate a double array for all features.
00121        - then loop over the component images sepqrately and write into the buffer.
00122          This might be faster because the component images are are accessed once.
00123        - then normalize all features and copy vectors into the point list.
00124     */
00125     int it = 0;
00126     Mat* descriptors = MatCreate<Mat>(pointData->Size(), components.size() * spatiality * spatiality);
00127     for(int y=0 ; y<=h-spatiality ; ++y)
00128     {
00129         for(int x=0 ; x<=w-spatiality ; ++x)
00130         {
00131             ExtractFeatureIntoVector(MatE(descriptors, it, 0), components, x, y, spatiality);
00132             it++;
00133         }
00134     }
00135     for(int i=0 ; i<components.size() ; ++i)
00136         delete components[i];
00137     return descriptors;
00138 }
00139  
00140 
00141 } // namespace Koen
00142 } // namespace Sandbox
00143 } // namespace Impala
00144 
00145 #endif

Generated on Thu Jan 13 09:04:24 2011 for ImpalaSrc by  doxygen 1.5.1