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
00019
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
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 in>>obj.id;
00174
00175 in>>cbSize;
00176 for (unsigned i=0; i<cbSize; i++)
00177 {
00178 cbe = new CodebookEntry();
00179
00180 in>>cbe->sqr_sigma;
00181 in>>cbe->cntGood;
00182 in>>cbe->cntTime;
00183 in>>cbe->reliability;
00184
00185 cbe->model = new KeypointDescriptor();
00186 cbe->model->LoadAll(in, *cbe->model);
00187
00188 in>>ocSize;
00189
00190 for (unsigned j=0; j<ocSize; j++)
00191 {
00192 oc = new KeypointDescriptor();
00193 oc->LoadAll(in, *oc);
00194 cbe->occurrences.PushBack(oc);
00195 }
00196
00197 obj.codebook.PushBack(cbe);
00198 }
00199
00200 in.close();
00201 return true;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 }
00215
blort
Author(s): Michael Zillich,
Thomas Mörwald,
Johann Prankl,
Andreas Richtsfeld,
Bence Magyar (ROS version)
autogenerated on Thu Jan 2 2014 11:38:25