00001 #ifndef Impala_Core_Feature_Computor_h
00002 #define Impala_Core_Feature_Computor_h
00003
00004 #include "Persistency/FeatureListRepository.h"
00005 #include "Persistency/FeatureTableRepository.h"
00006 #include "Persistency/LabelSetRepository.h"
00007 #include "Basis/CmdOptions.h"
00008 #include "Util/DatabaseWriteString.h"
00009 #include "Util/DatabaseReadString.h"
00010 #include "Core/Database/MakeRawDataSet.h"
00011 #include "Core/Geometry/RectanglePyramid.h"
00012 #include "Core/Geometry/RectanglePyramidPerDim.h"
00013 #include "Core/Table/Read.h"
00014 #include "Core/Table/Write.h"
00015 #include "Core/Feature/FeatureTableSet.h"
00016 #include "Core/Feature/LabelSet.h"
00017
00018 namespace Impala
00019 {
00020 namespace Core
00021 {
00022 namespace Feature
00023 {
00024
00025
00048 class Computor
00049 {
00050 public:
00051 typedef Persistency::FeatureLocator FeatureLocator;
00052
00053 Computor(String name, CmdOptions& options)
00054 {
00055 mName = name;
00056
00057 mPyramid = Geometry::RectanglePyramid
00058 (options.GetInt("borderWidth"),
00059 options.GetInt("nrRegionsPerDimMin"),
00060 options.GetInt("nrRegionsPerDimMax"),
00061 options.GetInt("nrRegionsStepSize"),
00062 -1, -1);
00063
00064 mLazebnikPyramid = Geometry::RectanglePyramidPerDim
00065 (options.GetInt("borderWidth"),
00066 options.GetInt("nrLazebnikRegionsPerDimMinX", 1),
00067 options.GetInt("nrLazebnikRegionsPerDimMaxX", 1),
00068 options.GetInt("nrLazebnikRegionsStepSizeX", 1),
00069 options.GetInt("nrLazebnikRegionsPerDimMinY", 1),
00070 options.GetInt("nrLazebnikRegionsPerDimMaxY", 1),
00071 options.GetInt("nrLazebnikRegionsStepSizeY", 1),
00072 -1, -1,
00073 options.GetDouble("overlapLazebnikRegions", 0));
00074
00075 mProtoDataFile = options.GetString("protoDataFile");
00076 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00077 #else // REPOSITORY_USED
00078 mProtoLabels = new LabelSet();
00079 #endif // REPOSITORY_USED
00080 mProtoLimit = -1;
00081
00082 mFeatureTableSet = new FeatureTableSet();
00083 mProtoFeatureTableSet = new FeatureTableSet();
00084 mRegionFeatureTableSet = new FeatureTableSet();
00085
00086 mPixelFeatureSetSize = -1;
00087 mRegionDescriptorLength = -1;
00088 }
00089
00090 virtual
00091 ~Computor()
00092 {
00093 mFeatureTableSet->Delete();
00094 mProtoFeatureTableSet->Delete();
00095 mRegionFeatureTableSet->Delete();
00096 delete mFeatureTableSet;
00097 delete mProtoFeatureTableSet;
00098 delete mRegionFeatureTableSet;
00099 }
00100
00101
00102
00103 String
00104 GetName() const
00105 {
00106 return mName;
00107 }
00108
00109 bool
00110 IsClusterBased() const
00111 {
00112 return mProtoDataFile == "clusters";
00113 }
00114
00115 String
00116 GetMaskSetBase() const
00117 {
00118 return mMaskSetBase;
00119 }
00120
00121 virtual int
00122 GetNrPixelFeatureSets() const
00123 {
00124 return mScale.size();
00125 }
00126
00127 int
00128 GetPixelFeatureSetSize() const
00129 {
00130 return mPixelFeatureSetSize;
00131 }
00132
00133 virtual int
00134 GetNrRegionFeatureSets() const
00135 {
00136 return mPyramid.NrLevels();
00137 }
00138
00139 virtual int
00140 GetRegionFeatureSetSize(int i) const
00141 {
00142 return mPyramid.NrRects(i);
00143 }
00144
00145 int
00146 GetRegionDescriptorLength()
00147 {
00148 return mRegionDescriptorLength;
00149 }
00150
00151 int
00152 GetRegionFeatureLength()
00153 {
00154 return GetPixelFeatureSetSize() * GetRegionDescriptorLength();
00155 }
00156
00157 Geometry::RectanglePyramid
00158 GetPyramid()
00159 {
00160 return mPyramid;
00161 }
00162
00163 String
00164 GetProtoDataFile()
00165 {
00166 return mProtoDataFile;
00167 }
00168
00169
00170
00171 FeatureTableSet*
00172 GetFeatureTableSet()
00173 {
00174 return mFeatureTableSet;
00175 }
00176
00177 FeatureTable*
00178 GetFeatureTable(FeatureDefinition def)
00179 {
00180 return mFeatureTableSet->GetTable(def);
00181 }
00182
00183 FeatureTable*
00184 GetFeatureTable(FeatureDefinition def, int regionFeatureSet)
00185 {
00186 if (IsClusterBased())
00187 return GetFeatureTable(def);
00188 def.AddParameter(GetRegionFeatureDefName(regionFeatureSet),
00189 GetRegionFeatureDefVal(regionFeatureSet));
00190 return GetFeatureTable(def);
00191 }
00192
00193 void
00194 AddFeatureTable(FeatureDefinition def, int tableSize, int vecLen)
00195 {
00196 mFeatureTableSet->Add(new FeatureTable(def, tableSize, vecLen));
00197 }
00198
00199 void
00200 MakeFeatureTableSet()
00201 {
00202 FeatureTableSet* protoSet = GetProtoFeatureTableSet();
00203 for (int i=0 ; i<protoSet->Size() ; i++)
00204 {
00205 FeatureTable* tab = protoSet->GetTable(i);
00206 if (IsClusterBased())
00207 {
00208
00209 int vecLen = 2 * tab->Size() * mLazebnikPyramid.TotalNrRects();
00210 AddFeatureTable(tab->GetFeatureDefinition(), 100, vecLen);
00211 }
00212 else
00213 {
00214
00215 int vecLen = 2 * 1 * mLazebnikPyramid.TotalNrRects();
00216 FeatureDefinition def = tab->GetFeatureDefinition();
00217 for (int r=0 ; r<GetNrRegionFeatureSets() ; r++)
00218 {
00219 FeatureDefinition def2 = def;
00220 def2.AddParameter(GetRegionFeatureDefName(r),
00221 GetRegionFeatureDefVal(r));
00222 AddFeatureTable(def2, 100, vecLen);
00223 }
00224 }
00225 }
00226 }
00227
00228
00229 virtual FeatureDefinition
00230 ExtendFeatureDefinition(FeatureDefinition def)
00231 {
00232 int limit = (mProtoLimit < 0) ? 0 : mProtoLimit;
00233 def.AddParameter("limit", limit);
00234 def.AddParameter("sp", mLazebnikPyramid.AsDefString());
00235 return def;
00236 }
00237
00238 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00239 void
00240 WriteFeatureDefinitions(String fName, Util::Database* db)
00241 {
00242 std::vector<String> l;
00243 for (int i=0 ; i<mFeatureTableSet->Size() ; i++)
00244 {
00245 FeatureDefinition def = mFeatureTableSet->GetFeatureDefinition(i);
00246 def = ExtendFeatureDefinition(def);
00247 l.push_back(def.AsString());
00248 }
00249 Util::DatabaseWriteString(fName, db, l.begin(), l.end());
00250 }
00251 #else // REPOSITORY_USED
00252 void
00253 WriteFeatureDefinitions(const Persistency::FeatureLocator& loc)
00254 {
00255 FeatureList l;
00256 for (int i=0 ; i<mFeatureTableSet->Size() ; i++)
00257 {
00258 FeatureDefinition def = mFeatureTableSet->GetFeatureDefinition(i);
00259 def = ExtendFeatureDefinition(def);
00260 l.push_back(def.AsString());
00261 }
00262 Persistency::FeatureListRepository().Add(loc, &l);
00263 }
00264 #endif // REPOSITORY_USED
00265
00266
00267
00268 FeatureTableSet*
00269 GetProtoFeatureTableSet()
00270 {
00271 return mProtoFeatureTableSet;
00272 }
00273
00274 FeatureTable*
00275 GetProtoFeatureTable(int pixelFeatureSet, int regionFeatureSet,
00276 String clusterType, String clusterVal)
00277 {
00278 String pName = GetPixelFeatureDefName(pixelFeatureSet);
00279 String pVal = GetPixelFeatureDefVal(pixelFeatureSet);
00280 String rName = GetRegionFeatureDefName(regionFeatureSet);
00281 String rVal = GetRegionFeatureDefVal(regionFeatureSet);
00282 return mProtoFeatureTableSet->GetTable(pName, pVal, rName, rVal,
00283 clusterType, clusterVal);
00284 }
00285
00286 FeatureTable*
00287 GetProtoFeatureTable(int pixelFeatureSet, String anno)
00288 {
00289 String pName = GetPixelFeatureDefName(pixelFeatureSet);
00290 String pVal = GetPixelFeatureDefVal(pixelFeatureSet);
00291 FeatureDefinition def(GetName());
00292 def.AddParameter("proto", FileNameBase(mProtoDataFile));
00293 def.AddParameter("anno", anno);
00294 def.AddParameter(pName, pVal);
00295 FeatureTable* res = GetProtoFeatureTableSet()->GetTable(def);
00296 if (res == 0)
00297 AddProtoFeatureTable(def, 1, GetRegionFeatureLength());
00298 return GetProtoFeatureTableSet()->GetTable(def);
00299 }
00300
00301 std::vector<FeatureTable*>
00302 GetProtoFeatureTables(int pixelFeatureSet)
00303 {
00304 String pName = GetPixelFeatureDefName(pixelFeatureSet);
00305 String pVal = GetPixelFeatureDefVal(pixelFeatureSet);
00306 return mProtoFeatureTableSet->GetTables(pName, pVal);
00307 }
00308
00309 std::vector<FeatureTable*>
00310 GetProtoFeatureTables(int pixelFeatureSet, int regionFeatureSet)
00311 {
00312 String pName = GetPixelFeatureDefName(pixelFeatureSet);
00313 String pVal = GetPixelFeatureDefVal(pixelFeatureSet);
00314 String rName = GetRegionFeatureDefName(regionFeatureSet);
00315 String rVal = GetRegionFeatureDefVal(regionFeatureSet);
00316 return mProtoFeatureTableSet->GetTables(pName, pVal, rName, rVal);
00317 }
00318
00319 void
00320 AddProtoFeatureTable(FeatureDefinition def, int tableSize, int vecLen)
00321 {
00322 mProtoFeatureTableSet->Add(new FeatureTable(def, tableSize, vecLen));
00323 }
00324
00325 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00326 void
00327 WriteProtoDefinitions(String fName, Util::Database* db)
00328 {
00329 std::vector<String> l;
00330 for (int i=0 ; i<mProtoFeatureTableSet->Size() ; i++)
00331 l.push_back(mProtoFeatureTableSet->GetDefinitionAsString(i));
00332 Util::DatabaseWriteString(fName, db, l.begin(), l.end());
00333 }
00334
00335 void
00336 WriteProtoFeatureTableSet(Database::RawDataSet* dataSet)
00337 {
00338 Util::Database* db = dataSet->GetDatabase();
00339 std::vector<String> l;
00340 String lName = dataSet->GetFilePathPrototype
00341 (GetProtoDataFile(), GetName(), GetName() + "_all.txt", false, true);
00342 if (! lName.empty())
00343 {
00344 Util::DatabaseReadString(std::back_inserter(l), lName, db, true);
00345 }
00346 else
00347 {
00348 lName = dataSet->GetFilePathPrototype
00349 (GetProtoDataFile(), GetName(), GetName() + "_all.txt", true,
00350 false);
00351 }
00352
00353 for (int i=0 ; i<mProtoFeatureTableSet->Size() ; i++)
00354 {
00355 FeatureDefinition def =
00356 mProtoFeatureTableSet->GetFeatureDefinition(i);
00357 l.push_back(def.AsString());
00358 String fName = dataSet->GetFilePathPrototype
00359 (GetProtoDataFile(), GetName(), def.AsString() + ".tab", true,
00360 false);
00361 Table::Write(mProtoFeatureTableSet->GetTable(i), fName, db, true);
00362 }
00363 Util::DatabaseWriteString(lName, db, l.begin(), l.end());
00364 lName = dataSet->GetFilePathPrototype
00365 (GetProtoDataFile(), GetName(), GetName()+"_label.txt", true, true);
00366 if (!lName.empty())
00367 mProtoLabels.Save(lName, db);
00368 }
00369 #else // REPOSITORY_USED
00370 void
00371 WriteProtoFeatureTableSet(Database::RawDataSet* dataSet)
00372 {
00373 FeatureList l;
00374 String container = mProtoDataFile + "/" + GetName();
00375 FeatureLocator loc(dataSet->GetLocator(), true, false, "",
00376 GetName()+"_all.txt", container);
00377 if (Persistency::FeatureListRepository().Exists(loc))
00378 l = *(Persistency::FeatureListRepository().Get(loc));
00379
00380 for (int i=0 ; i<mProtoFeatureTableSet->Size() ; i++)
00381 {
00382 FeatureDefinition def =
00383 mProtoFeatureTableSet->GetFeatureDefinition(i);
00384 l.push_back(def.AsString());
00385 FeatureLocator tLoc = loc;
00386 tLoc.SetFeatureDef(def);
00387 Persistency::FeatureTableRepository().Add
00388 (tLoc, mProtoFeatureTableSet->GetTable(i));
00389 }
00390 Persistency::FeatureListRepository().Add(loc, &l);
00391 loc.SetFeatureString(GetName()+"_label.txt");
00392 Persistency::LabelSetRepository().Add(loc, mProtoLabels);
00393 }
00394 #endif // REPOSITORY_USED
00395
00396 void
00397 ReadProtoFeatureTableSet(String protoSet, String maskSet)
00398 {
00399 mMaskSetBase = FileNameBase(maskSet);
00400 Database::RawDataSet* annoSet = Database::MakeRawDataSet(protoSet);
00401 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00402 String fName = annoSet->GetFilePathPrototype
00403 (mProtoDataFile, GetName(), GetName()+"_all.txt", false, false);
00404 if (!maskSet.empty())
00405 {
00406 maskSet = annoSet->GetFilePathPrototype(mProtoDataFile, GetName(),
00407 maskSet, false, false);
00408 }
00409 Util::Database* db = annoSet->GetDatabase();
00410 ReadFeatureTableSet(fName, db, mProtoFeatureTableSet, maskSet);
00411 fName = annoSet->GetFilePathPrototype
00412 (mProtoDataFile, GetName(), GetName()+"_label.txt", false, false);
00413 mProtoLabels.Load(fName, db);
00414 delete annoSet;
00415 #else // REPOSITORY_USED
00416 String container = mProtoDataFile + "/" + GetName();
00417 FeatureLocator loc(annoSet->GetLocator(), true, false, "",
00418 GetName()+"_all.txt", container);
00419 ReadFeatureTableSet(loc, mProtoFeatureTableSet, maskSet);
00420 loc.SetFeatureString(GetName()+"_label.txt");
00421 mProtoLabels = Persistency::LabelSetRepository().Get(loc);
00422 delete annoSet;
00423 #endif // REPOSITORY_USED
00424 }
00425
00427 void
00428 LimitProtoFeatureTableSize(int maxNr)
00429 {
00430 mProtoLimit = maxNr;
00431 for (int i=0 ; i<mProtoFeatureTableSet->Size() ; i++)
00432 {
00433 FeatureTable* t = mProtoFeatureTableSet->GetTable(i);
00434 if (t->Size() > maxNr)
00435 {
00436 ILOG_INFO("limiting " << t->GetFeatureDefinition().AsString()
00437 << " to " << maxNr);
00438 t->SetSize(maxNr);
00439 t->GetColumn2()->SetSize(maxNr);
00440 }
00441 }
00442 }
00443
00444
00445
00446 FeatureTableSet*
00447 GetRegionFeatureTableSet()
00448 {
00449 return mRegionFeatureTableSet;
00450 }
00451
00452 void
00453 AddRegionFeatureTable(FeatureDefinition def, int tableSize, int vecLen)
00454 {
00455 mRegionFeatureTableSet->Add(new FeatureTable(def, tableSize, vecLen));
00456 }
00457
00458 FeatureTable*
00459 GetRegionFeatureTable(int pixelFeatureSet, int regionFeatureSet)
00460 {
00461 String pName = GetPixelFeatureDefName(pixelFeatureSet);
00462 String pVal = GetPixelFeatureDefVal(pixelFeatureSet);
00463 String rName = GetRegionFeatureDefName(regionFeatureSet);
00464 String rVal = GetRegionFeatureDefVal(regionFeatureSet);
00465 return mRegionFeatureTableSet->GetTable(pName, pVal, rName, rVal);
00466 }
00467
00468 void
00469 MakeRegionFeatureTableSet()
00470 {
00471 int vecLen = GetRegionFeatureLength();
00472 for (int p=0 ; p<GetNrPixelFeatureSets() ; p++)
00473 {
00474 for (int r=0 ; r<GetNrRegionFeatureSets() ; r++)
00475 {
00476 String pName = GetPixelFeatureDefName(p);
00477 String pVal = GetPixelFeatureDefVal(p);
00478 String rName = GetRegionFeatureDefName(r);
00479 String rVal = GetRegionFeatureDefVal(r);
00480 FeatureDefinition def(GetName());
00481 def.AddParameter(pName, pVal);
00482 def.AddParameter(rName, rVal);
00483 AddRegionFeatureTable(def, GetRegionFeatureSetSize(r), vecLen);
00484 }
00485 }
00486 }
00487
00488
00489
00490 virtual String
00491 GetPixelFeatureDefName(int idx)
00492 {
00493 return "scale";
00494 }
00495
00496 virtual String
00497 GetPixelFeatureDefVal(int idx)
00498 {
00499 return MakeString(mScale[idx]);
00500 }
00501
00502 virtual String
00503 GetRegionFeatureDefName(int idx)
00504 {
00505 return "rpd";
00506 }
00507
00508 virtual String
00509 GetRegionFeatureDefVal(int idx)
00510 {
00511 return MakeString(GetPyramid().NrRectsPerDim(idx));
00512 }
00513
00514 virtual FeatureTable*
00515 MakeProtoFeatureTable(int pixelFeatureSet, int regionFeatureSet,
00516 String clusterType, String clusterVal) = 0;
00517
00518 virtual void
00519 ReadPrototypes(String annoSet, String maskSet, int maxNr) = 0;
00520
00521 virtual void
00522 ComputeRegionFeatures(Array::Array2dVec3UInt8* im, Quid quid) = 0;
00523
00524 virtual void
00525 ComputeFeatures(Array::Array2dVec3UInt8* im, Quid quid) = 0;
00526
00527 protected:
00528
00529 double
00530 GetScale(int i)
00531 {
00532 return mScale[i];
00533 }
00534
00535 void
00536 AddScale(double scale)
00537 {
00538 mScale.push_back(scale);
00539 }
00540
00541 void
00542 SetPixelFeatureSetSize(int nr)
00543 {
00544 mPixelFeatureSetSize = nr;
00545 }
00546
00547 void
00548 SetRegionDescriptorLength(int len)
00549 {
00550 mRegionDescriptorLength = len;
00551 }
00552
00553 String mName;
00554 std::vector<double> mScale;
00555 Geometry::RectanglePyramid mPyramid;
00556 Geometry::RectanglePyramidPerDim mLazebnikPyramid;
00557 String mMaskSetBase;
00558
00559 String mProtoDataFile;
00560 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00561 LabelSet mProtoLabels;
00562 #else // REPOSITORY_USED
00563 LabelSet* mProtoLabels;
00564 #endif // REPOSITORY_USED
00565 int mProtoLimit;
00566
00567 int mPixelFeatureSetSize;
00568 int mRegionDescriptorLength;
00569
00570 private:
00571
00572 #ifndef REPOSITORY_USED // Here comes the deprecated stuff
00573 void
00574 ReadFeatureTableSet(String listName, Util::Database* db,
00575 FeatureTableSet* tables, String maskSet)
00576 {
00577 std::vector<String> defList;
00578 Util::DatabaseReadString(std::back_inserter(defList), listName, db, true);
00579 std::vector<String> maskList;
00580 if (!maskSet.empty())
00581 Util::DatabaseReadString(std::back_inserter(maskList), maskSet, db,
00582 true);
00583 String dirName = FileNamePath(listName);
00584 for (int i=0 ; i<defList.size() ; i++)
00585 {
00586 if ((!maskSet.empty()) &&
00587 (std::find(maskList.begin(), maskList.end(), defList[i]) ==
00588 maskList.end()))
00589 {
00590 ILOG_INFO_ONCE("skipping " << defList[i]);
00591 continue;
00592 }
00593 FeatureDefinition def(defList[i]);
00594 String fName = dirName + "/" + def.AsString() + ".tab";
00595 FeatureTable* tab = tables->GetTable(def);
00596 if (!tab)
00597 {
00598 tab = new FeatureTable(def);
00599 tables->Add(tab);
00600 }
00601 ILOG_INFO_ONCE("reading " << fName);
00602 Table::Read(tab, fName, db);
00603 }
00604 if (tables->Size() == 0)
00605 ILOG_ERROR("No tables read");
00606 }
00607 #else // REPOSITORY_USED
00608 void
00609 ReadFeatureTableSet(const FeatureLocator& listLoc, FeatureTableSet* tables,
00610 String maskSet)
00611 {
00612 String listName = listLoc.GetFeatureString();
00613 FeatureLocator tLoc = listLoc;
00614 FeatureList defList = *(Persistency::FeatureListRepository().Get(tLoc));
00615 FeatureList maskList;
00616 if (!maskSet.empty())
00617 {
00618 tLoc.SetFeatureString(maskSet);
00619
00620
00621 maskList = *(Persistency::FeatureListRepository().Get(tLoc));
00622 }
00623
00624 for (int i=0 ; i<defList.size() ; i++)
00625 {
00626 if ((!maskSet.empty()) &&
00627 (std::find(maskList.begin(), maskList.end(), defList[i]) ==
00628 maskList.end()))
00629 {
00630 ILOG_DEBUG("skipping " << defList[i]);
00631 continue;
00632 }
00633 FeatureDefinition def(defList[i]);
00634
00635 FeatureTable* tab = tables->GetTable(def);
00636 if (!tab)
00637 {
00638
00639 tLoc.SetFeatureDef(def);
00640 ILOG_DEBUG("reading " << def.AsString());
00641 tab = Persistency::FeatureTableRepository().Get(tLoc);
00642 tables->Add(tab);
00643 }
00644 else
00645 {
00646
00647
00648 ILOG_INFO_ONCE("reading " << def.AsString());
00649 ILOG_ERROR("ReadFeatureTableSet: cannot do this yet");
00650 }
00651 }
00652 if (tables->Size() == 0)
00653 ILOG_ERROR("No tables read");
00654 }
00655 #endif // REPOSITORY_USED
00656
00657 FeatureTableSet* mFeatureTableSet;
00658 FeatureTableSet* mProtoFeatureTableSet;
00659 FeatureTableSet* mRegionFeatureTableSet;
00660
00661 ILOG_VAR_DEC;
00662
00663 };
00664
00665 ILOG_VAR_INIT(Computor, Impala.Core.Feature);
00666
00667 }
00668 }
00669 }
00670
00671 #endif