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/Geometry/InterestPointList.h"
00007 #include "Core/Geometry/InterestRectangleZ.h"
00008 #include "Util/TimeStats.h"
00009
00010 namespace Impala
00011 {
00012 namespace Core
00013 {
00014 namespace Feature
00015 {
00016
00017 void
00018 AddDSurfOptions(CmdOptions& options)
00019 {
00020 options.AddOption(0, "surf-haar-size", "int", "3");
00021 options.AddOption(0, "surf-subregion-size", "int", "2");
00022 options.AddOption(0, "surf-spaciality", "int", "4");
00023 }
00024
00025 void
00026 GetDSurfOptions(CmdOptions& options, int& haarSize, int& subregionSize, int& spaciality)
00027 {
00028 haarSize = options.GetInt("surf-haar-size");
00029 subregionSize = options.GetInt("surf-subregion-size");
00030 spaciality = options.GetInt("surf-spaciality");
00031 }
00032
00037 inline void
00038 ExtractFeatureIntoVector(std::vector<Real64>& vector,
00039 std::vector<Array::Array2dScalarReal64*>& features,
00040 int x, int y, int spaciality)
00041 {
00042 double length = 0.;
00043 int begin = vector.size();
00044 vector.reserve(begin + (features.size()*spaciality*spaciality));
00045 for(int f=0 ; f<features.size() ; ++f)
00046 {
00047 for(int j=y ; j<y+spaciality ; ++j)
00048 for(int i=x ; i<x+spaciality ; ++i)
00049 {
00050 double v = features[f]->Value(i,j);
00051 vector.push_back(v);
00052 length += v*v;
00053 }
00054 }
00055
00056
00057 if(length != 0)
00058 {
00059 length = sqrt(length);
00060 for(int i=begin ; i<vector.size() ; ++i)
00061 vector[i] /= length;
00062 }
00063 }
00064
00067 void
00068 DSurf(Array::Array2dScalarReal64* image,
00069 Geometry::InterestPointList& pointList,
00070 int sampleRate, int numberHaarSum, int spaciality)
00071 {
00072 ILOG_VAR(Impala.Core.Feature.DSurf);
00073 ILOG_DEBUG("image w="<< image->CW() <<", h="<< image->CH());
00074
00075
00076 Array::Array2dScalarReal64* reduced = 0;
00077 IntegrateReduce(reduced, image, sampleRate);
00078
00079 std::vector<Array::Array2dScalarReal64*> haar = HaarResponse(reduced);
00080 delete reduced;
00081
00082 std::vector<Array::Array2dScalarReal64*> components;
00083 int w = haar[0]->CW() / numberHaarSum;
00084 int h = haar[0]->CH() / numberHaarSum;
00085 for(int i=0 ; i<haar.size() ; ++i)
00086 {
00087 Array::Array2dScalarReal64* comp =
00088 new Array::Array2dScalarReal64(w, h, 0, 0);
00089 IntegrateReduceWeighted(comp, haar[i], numberHaarSum);
00090 components.push_back(comp);
00091 delete haar[i];
00092 }
00093 w = components[0]->CW();
00094 h = components[0]->CH();
00095
00096 int nrFeatures = w*h;
00097 int sampleSize = sampleRate * numberHaarSum;
00098 int featureSize = sampleSize * spaciality;
00099
00100
00101
00102 if(pointList.size() == 0)
00103 {
00104 for(int y=0 ; y<=h-spaciality ; ++y)
00105 {
00106 for(int x=0 ; x<=w-spaciality ; ++x)
00107 {
00108 int left=x*sampleSize;
00109 int top =y*sampleSize;
00110 Geometry::Rectangle r(left, top, left+featureSize, top+featureSize);
00111 Geometry::InterestRectangleZ* ir =
00112 new Geometry::InterestRectangleZ(r);
00113 pointList.push_back(ir);
00114 }
00115 }
00116 }
00117
00118
00119
00120
00121
00122
00123
00124 Geometry::InterestPointList::iterator it = pointList.begin();
00125 for(int y=0 ; y<=h-spaciality ; ++y)
00126 {
00127 for(int x=0 ; x<=w-spaciality ; ++x)
00128 {
00129 Geometry::InterestPoint* ir = *it;
00130 it++;
00131 ExtractFeatureIntoVector(ir->mDescriptor, components, x, y, spaciality);
00132 }
00133 }
00134 for(int i=0 ; i<components.size() ; ++i)
00135 delete components[i];
00136 }
00137
00138
00139 }
00140 }
00141 }
00142
00143 #endif