ModelObject3D.cc
Go to the documentation of this file.
00001 
00008 #include <blort/Recognizer3D/ModelObject3D.hh>
00009 #include <stdexcept>
00010 
00011 
00012 
00013 
00014 namespace P 
00015 {
00016 
00017 
00018 /********************** ModelObject3D ************************
00019  * Constructor/Destructor
00020  */
00021 ModelObject3D::ModelObject3D()
00022 {
00023 }
00024 
00025 
00026 ModelObject3D::~ModelObject3D()
00027 {
00028 }
00029 
00033 void ModelObject3D::ComputeNewHnorm(P::Array<KeypointDescriptor*> &keys, double Hnorm[9])
00034 {
00035   if (keys.Size()==0)
00036     return;
00037 
00038   Hnorm[0] = 1., Hnorm[1] =  0., Hnorm[2] = 0.;
00039   Hnorm[3] = 0., Hnorm[4] =  1., Hnorm[5] = 0.;
00040   Hnorm[6] = 0., Hnorm[7] =  0., Hnorm[8] = 1.;
00041 
00042   for (unsigned i=0; i<keys.Size(); i++)
00043   {
00044     Hnorm[2] -= keys[i]->p.x;
00045     Hnorm[5] -= keys[i]->p.y;
00046   }
00047 
00048   Hnorm[2] /= (double)keys.Size();
00049   Hnorm[5] /= (double)keys.Size();
00050 }
00051 
00055 void ModelObject3D::InsertMeanShift(Array<KeypointDescriptor* > &keys, P::Array<CodebookEntry*> &codebook, double H[9])
00056 {
00057   double scale, angle;
00058   P::Vector2 center;
00059   KeypointDescriptor *occ;
00060   float sigma;
00061   bool inserted;
00062   Vector2 p;
00063 
00064   for (unsigned i=0; i<codebook.Size(); i++)
00065     codebook[i]->cntTime++;
00066   
00067   for (unsigned i=0; i<keys.Size(); i++)
00068   {
00069     inserted=false;
00070     for (unsigned j=0; j<codebook.Size(); j++)
00071     {
00072       if (((KeypointDescriptor*)keys[i])->Match(codebook[j]->model) != FLT_MAX)
00073       {
00074         sigma = codebook[j]->CombinedSqrSigma((KeypointDescriptor*)keys[i]);
00075         if (sigma < codebook[j]->model->GetThr()*Def::DO_CLUSTER_SIGMA_THR)
00076         {
00077           codebook[j]->Insert((KeypointDescriptor*)keys[i]);
00078           occ=codebook[j]->occurrences.Last();
00079           GetPosScaleAngle(occ->p.x, occ->p.y,H, center.x,center.y,scale,angle);
00080           p = occ->p;
00081           MapPoint(&p.x,H, &occ->p.x);
00082           occ->scale *= scale;
00083           occ->angle += ScaleAngle_mpi_pi(angle);
00084           if (codebook[j]->cntTime > Def::DO_TIME_MEAN) codebook[j]->cntTime = Def::DO_TIME_MEAN;
00085           inserted=true;
00086         }
00087       }
00088     }
00089     if (!inserted)
00090     {
00091       codebook.PushBack(new CodebookEntry((KeypointDescriptor*)keys[i]));
00092       occ = codebook.Last()->occurrences[0];
00093       GetPosScaleAngle(occ->p.x, occ->p.y,H, center.x,center.y,scale,angle);
00094       p = occ->p;
00095       MapPoint(&p.x,H, &occ->p.x);
00096       occ->scale *= scale;
00097       occ->angle += ScaleAngle_mpi_pi(angle);
00098     }
00099   }
00100 }
00101 
00102 
00103 
00104 
00105 
00106 
00107 /******************************** PUBLIC **************************/
00108 
00112 void ModelObject3D::AddToModel(Array<KeypointDescriptor *> &keys, Object3D &obj)
00113 {
00114   double H[9];
00115 
00116   ComputeNewHnorm(keys, H);
00117   InsertMeanShift(keys, obj.codebook, H);
00118 }
00119 
00120 
00124 void ModelObject3D::SaveModel(const char *filename, Object3D &obj)
00125 {
00126   CodebookEntry *cbe;
00127 
00128   ofstream out(filename);
00129 
00130   if(!out)
00131     throw Except(__HERE__,"Error opening file!");
00132 
00133   out<<obj.id<<'\n';
00134 
00135   out<<obj.codebook.Size()<<'\n';
00136   for (unsigned i=0; i<obj.codebook.Size(); i++)
00137   {
00138     cbe = obj.codebook[i];
00139     out<<cbe->sqr_sigma<<'\n';
00140     out<<cbe->cntGood<<'\n';
00141     out<<cbe->cntTime<<'\n';
00142     out<<cbe->reliability<<'\n';
00143 
00144     cbe->model->SaveAll(out,*cbe->model);
00145 
00146     out<<cbe->occurrences.Size()<<'\n';
00147     for (unsigned j=0; j<cbe->occurrences.Size(); j++)
00148       cbe->occurrences[j]->SaveAll(out,*cbe->occurrences[j]);
00149   }
00150 
00151   out.close();
00152 }
00153 
00157 bool ModelObject3D::LoadModel(const std::string filename, Object3D &obj)
00158 {
00159   unsigned cbSize, ocSize;
00160   KeypointDescriptor *oc;
00161   CodebookEntry *cbe;
00162 
00163   DeleteCodebook(obj.codebook);
00164 
00165   ifstream in(filename.c_str());
00166 
00167   if(!in){
00168     char errmsg[128];
00169     sprintf(errmsg, "[ModelObject3D::LoadModel] Cannot open file '%s'", filename.c_str());
00170     throw std::runtime_error(errmsg);
00171   }
00172 
00173   // store only the filename, strip the path
00174   obj.file_name = filename.substr(filename.find_last_of("\\/")+1);
00175   in>>obj.id;
00176 
00177   in>>cbSize;
00178   for (unsigned i=0; i<cbSize; i++)
00179   {
00180     cbe = new CodebookEntry();
00181 
00182     in>>cbe->sqr_sigma;
00183     in>>cbe->cntGood;
00184     in>>cbe->cntTime;
00185     in>>cbe->reliability;
00186 
00187     cbe->model = new KeypointDescriptor();
00188     cbe->model->LoadAll(in, *cbe->model);
00189 
00190     in>>ocSize;
00191 
00192     for (unsigned j=0; j<ocSize; j++)
00193     {
00194       oc = new KeypointDescriptor();
00195       oc->LoadAll(in, *oc);
00196       cbe->occurrences.PushBack(oc);
00197     }
00198 
00199     obj.codebook.PushBack(cbe);
00200   }
00201 
00202   in.close();
00203   return true;
00204 }
00205 
00206 
00207 
00208 
00209 
00210 
00211 /************************* DEBUGGING METHODES *****************************/
00212 
00213 
00214 
00215 
00216 }  // -- THE END --
00217 


blort
Author(s): Thomas Mörwald , Michael Zillich , Andreas Richtsfeld , Johann Prankl , Markus Vincze , Bence Magyar
autogenerated on Wed Aug 26 2015 15:24:12