00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _VISUALTARGET_H_
00019 #define _VISUALTARGET_H_
00020
00021 #include "BufferedNode.h"
00022 #include <iostream>
00023 #include "Image.h"
00024 #include "cv.h"
00025 #include "Vector.h"
00026 #include "VisualFeatureDesc.h"
00027 #include "VisualROI.h"
00028
00029 namespace RobotFlow {
00030
00031 template <class FeatBaseType>
00032 class VisualTarget : public FD::Object
00033 {
00034 public:
00035 VisualTarget()
00036 : m_valid(false), m_id(0), m_activeAge(0), m_passiveAge(0), m_curDescIdx(0),
00037 m_refROI(NULL), m_numDesc(-1), m_targetDesc(NULL),
00038 m_cueWeights(NULL), m_tmpCueProb(NULL)
00039 {
00040
00041 }
00042
00043 VisualTarget(int i_id, VisualROI *i_roi,
00044 FD::Vector<VisualFeatureDesc<FeatBaseType> *> *i_targetDesc)
00045 : m_valid(true), m_id(i_id), m_activeAge(0), m_passiveAge(0), m_curDescIdx(0),
00046 m_targetDesc(NULL), m_cueWeights(NULL), m_tmpCueProb(NULL)
00047 {
00048 m_refROI = FD::RCPtr<VisualROI>(new VisualROI(*i_roi));
00049 m_numDesc = i_targetDesc->size();
00050 m_targetDesc = new FD::Vector<VisualFeatureDesc<FeatBaseType> *>(m_numDesc);
00051
00052 for (int i=0; i<m_numDesc; i++) {
00053 (*m_targetDesc)[i] = new VisualFeatureDesc<FeatBaseType>(*((*i_targetDesc)[i]));
00054 }
00055
00056 m_cueWeights = new double[m_numDesc];
00057 m_tmpCueProb = new double[m_numDesc];
00058 }
00059
00060 VisualTarget(const VisualTarget<FeatBaseType> & i_ref)
00061 :m_id(-1), m_activeAge(-1), m_passiveAge(-1), m_curDescIdx(0),
00062 m_numDesc(-1), m_targetDesc(NULL), m_cueWeights(NULL),
00063 m_tmpCueProb(NULL)
00064 {
00065 try {
00066 m_valid = i_ref.m_valid;
00067 m_id = i_ref.m_id;
00068 m_activeAge = i_ref.m_activeAge;
00069 m_passiveAge = i_ref.m_passiveAge;
00070 m_curDescIdx = i_ref.m_curDescIdx;
00071 if (!i_ref.m_refROI.isNil()) {
00072 if (!this->m_refROI.isNil()) {
00073 *(this->m_refROI) = *(i_ref.m_refROI);
00074 }
00075 else {
00076 this->m_refROI = FD::RCPtr<VisualROI>(new VisualROI(*(i_ref.m_refROI)));
00077 }
00078 }
00079 else {
00080 std::cout << "VisualTarget::VisualTarget: reference to copy has NULL ROI" << std::endl;
00081 }
00082 if (i_ref.m_targetDesc) {
00083 SetDescriptorsVec(i_ref.m_targetDesc);
00084 }
00085 else {
00086 this->m_targetDesc = NULL;
00087 this->m_numDesc = 0;
00088 this->m_cueWeights = NULL;
00089 this->m_tmpCueProb = NULL;
00090 }
00091 for (int i=0; i<m_numDesc; i++) {
00092 m_cueWeights[i] = i_ref.m_cueWeights[i];
00093 m_tmpCueProb[i] = i_ref.m_tmpCueProb[i];
00094 }
00095 }
00096 catch (FD::BaseException *e) {
00097 throw e->add(new FD::GeneralException("Exception caught in VisualTarget::VisualTarget:",__FILE__,__LINE__));
00098 }
00099 }
00100
00101 ~VisualTarget()
00102 {
00103 for (int i=0; i<m_numDesc; i++) {
00104 delete (*m_targetDesc)[i];
00105 }
00106 delete m_targetDesc;
00107 delete [] m_cueWeights;
00108 delete [] m_tmpCueProb;
00109 }
00110
00111 VisualTarget<FeatBaseType> & operator =(VisualTarget<FeatBaseType> &i_ref)
00112 {
00113 try {
00114
00115 if (&i_ref != this) {
00116 this->m_valid = i_ref.m_valid;
00117 this->m_id = i_ref.m_id;
00118 this->m_activeAge = i_ref.m_activeAge;
00119 this->m_passiveAge = i_ref.m_passiveAge;
00120 this->m_curDescIdx = i_ref.m_curDescIdx;
00121 if (!i_ref.m_refROI.isNil()) {
00122 if (!this->m_refROI.isNil()) {
00123 *(this->m_refROI) = *(i_ref.m_refROI);
00124 }
00125 else {
00126 this->m_refROI = FD::RCPtr<VisualROI>(new VisualROI(*(i_ref.m_refROI)));
00127 }
00128 }
00129 else {
00130 std::cout << "VisualTarget::VisualTarget: reference to copy has NULL ROI" << std::endl;
00131 }
00132 if (i_ref.m_targetDesc) {
00133 SetDescriptorsVec(i_ref.m_targetDesc);
00134 }
00135 else {
00136 this->m_targetDesc = NULL;
00137 this->m_cueWeights = NULL;
00138 this->m_tmpCueProb = NULL;
00139 this->m_numDesc = 0;
00140 }
00141 for (int i=0; i<m_numDesc; i++) {
00142 m_cueWeights[i] = i_ref.m_cueWeights[i];
00143 m_tmpCueProb[i] = i_ref.m_tmpCueProb[i];
00144 }
00145 }
00146
00147 return *this;
00148 }
00149 catch (FD::BaseException *e) {
00150 throw e->add(new FD::GeneralException("Exception caught in VisualTarget::operator=:",__FILE__,__LINE__));
00151 }
00152 }
00153
00154
00155 void printOn(std::ostream &out) const
00156 {
00157 throw new FD::GeneralException("Exception in VisualTarget::printOn: method not yet implemented.",__FILE__,__LINE__);
00158 }
00159
00160
00161 void readFrom(std::istream &in)
00162 {
00163 throw new FD::GeneralException("Exception in VisualTarget::readFrom: method not yet implemented.",__FILE__,__LINE__);
00164 }
00165
00166 void Adapt(FD::Vector<VisualFeatureDesc<FeatBaseType> *> *i_desc, double i_rate)
00167 {
00168 if (m_numDesc != i_desc->size()) {
00169 throw new FD::GeneralException("Exception in VisualTarget::Adapt: input descriptor vector size differs from current object's vector.",__FILE__,__LINE__);
00170 }
00171
00172 for (int i=0; i<m_numDesc; i++) {
00173 if ((*m_targetDesc)[i]->GetType() != (*i_desc)[i]->GetType()) {
00174 throw new FD::GeneralException("Exception in VisualTarget::Adapt: features descriptor must have the same type.",__FILE__,__LINE__);
00175 }
00176
00177 (*m_targetDesc)[i]->Adapt((*i_desc)[i]->GetCstFeatures(), (*i_desc)[i]->GetSize(), i_rate);
00178 }
00179 }
00180
00181 void Adapt(FD::Vector<VisualFeatureDesc<FeatBaseType> *> *i_desc, double *i_rate)
00182 {
00183 if (m_numDesc != i_desc->size()) {
00184 throw new FD::GeneralException("Exception in VisualTarget::Adapt: input descriptor vector size differs from current object's vector.",__FILE__,__LINE__);
00185 }
00186
00187 for (int i=0; i<m_numDesc; i++) {
00188 if ((*m_targetDesc)[i]->GetType() != (*i_desc)[i]->GetType()) {
00189 throw new FD::GeneralException("Exception in VisualTarget::Adapt: features descriptor must have the same type.",__FILE__,__LINE__);
00190 }
00191
00192 (*m_targetDesc)[i]->Adapt((*i_desc)[i]->GetCstFeatures(), (*i_desc)[i]->GetSize(), i_rate[i]);
00193 }
00194 }
00195
00196 double Similarity(FD::Vector<VisualFeatureDesc<FeatBaseType> *> *i_desc)
00197 {
00198 if (m_numDesc != i_desc->size()) {
00199 throw new FD::GeneralException("Exception in VisualTarget::Similarity: input descriptor vector size differs from current object's vector.",__FILE__,__LINE__);
00200 }
00201
00202 double sim = 0.0;
00203
00204 for (int i=0; i<m_numDesc; i++) {
00205 if ((*m_targetDesc)[i]->GetType() != (*i_desc)[i]->GetType()) {
00206 throw new FD::GeneralException("Exception in VisualTarget::Similarity: features descriptor must have the same type.",__FILE__,__LINE__);
00207 }
00208
00209 sim += m_cueWeights[i]*(*m_targetDesc)[i]->Similarity((*i_desc)[i]->GetCstFeatures(), (*i_desc)[i]->GetSize());
00210 }
00211
00212 return sim;
00213 }
00214
00215 double SimilarityWCueAdapt(FD::Vector<VisualFeatureDesc<FeatBaseType> *> *i_desc, double i_rate)
00216 {
00217 if (m_numDesc != i_desc->size()) {
00218 throw new FD::GeneralException("Exception in VisualTarget::SimilarityWCueAdapt: input descriptor vector size differs from current object's vector.",__FILE__,__LINE__);
00219 }
00220
00221 int i;
00222 double sim = 0.0, cueSim, sumCueSim = 0.0;
00223
00224 for (i=0; i<m_numDesc; i++) {
00225 if ((*m_targetDesc)[i]->GetType() != (*i_desc)[i]->GetType()) {
00226 throw new FD::GeneralException("Exception in VisualTarget::SimilarityWCueAdapt: features descriptor must have the same type.",__FILE__,__LINE__);
00227 }
00228
00229 m_tmpCueProb[i] = (*m_targetDesc)[i]->Similarity((*i_desc)[i]->GetCstFeatures(), (*i_desc)[i]->GetSize());
00230 sumCueSim += m_tmpCueProb[i];
00231 sim += m_cueWeights[i]*m_tmpCueProb[i];
00232 std::cout << "Cue Weights [" << i << "]=" << m_cueWeights[i] << " Current Cue Prob=" << m_tmpCueProb[i] << std::endl;
00233 }
00234
00235 if (sumCueSim == 0.0) {
00236 std::cerr << "WARNING: VisualTarget::SimilarityWCueAdapt: cannot adapt cue weights with a total probability of 0.0." << std::endl;
00237 return sim;
00238 }
00239
00240
00241 double rateInv = 1.0 - i_rate;
00242 double sumCueSimInv = 1.0/sumCueSim;
00243 sumCueSim = 0.0;
00244
00245 for (i=0; i<m_numDesc; i++) {
00246 m_cueWeights[i] = i_rate*m_tmpCueProb[i]*sumCueSimInv + rateInv*m_cueWeights[i];
00247 sumCueSim += m_cueWeights[i];
00248 }
00249
00250
00251 sumCueSimInv = 1.0/sumCueSim;
00252 if (sumCueSimInv == 0.0) {
00253 throw new FD::GeneralException("Exception in VisualTarget::SimilarityWCueAdapt: cannot have a target with all cue weights are zero.",__FILE__,__LINE__);
00254 }
00255 for (i=0; i<m_numDesc; i++) {
00256 m_cueWeights[i] *= sumCueSimInv;
00257 }
00258
00259 return sim;
00260 }
00261
00262 void AgeTarget(bool i_matched)
00263 {
00264 if (i_matched) {
00265 if (m_activeAge < 15) {
00266 m_activeAge++;
00267 }
00268 if (m_passiveAge > 0) {
00269 m_passiveAge--;
00270 }
00271 }
00272 else {
00273 if (m_activeAge < 15) {
00274 m_passiveAge++;
00275 }
00276 if (m_activeAge > 0) {
00277 m_activeAge--;
00278 }
00279 }
00280 }
00281
00282 bool IsValid() const
00283 {
00284 return m_valid;
00285 }
00286
00287 int GetID() const
00288 {
00289 return m_id;
00290 }
00291
00292 int GetActiveAge() const
00293 {
00294 return m_activeAge;
00295 }
00296
00297 int GetPassiveAge() const
00298 {
00299 return m_passiveAge;
00300 }
00301
00302 int GetCurrentAge() const
00303 {
00304 return m_activeAge - m_passiveAge;
00305 }
00306
00307 VisualROI *GetROI()
00308 {
00309 return m_refROI.get();
00310 }
00311
00312 const VisualROI *GetCstROI() const
00313 {
00314 return (const VisualROI *)(m_refROI.get());
00315 }
00316
00317 FD::RCPtr<VisualROI> GetROIRCPtr()
00318 {
00319 return m_refROI;
00320 }
00321
00322 int GetNumDescriptors() const
00323 {
00324 return m_numDesc;
00325 }
00326
00327 int GetCurDescIdx() const
00328 {
00329 return m_curDescIdx;
00330 }
00331
00332 FD::Vector<VisualFeatureDesc<FeatBaseType> *> *GetDescriptorsVec()
00333 {
00334 return m_targetDesc;
00335 }
00336
00337 const FD::Vector<VisualFeatureDesc<FeatBaseType> *> *GetCstDescriptorsVec() const
00338 {
00339 return (const FD::Vector<VisualFeatureDesc<FeatBaseType> *> *)m_targetDesc;
00340 }
00341
00342 VisualFeatureDesc<FeatBaseType> *GetDescriptor(int i_idx)
00343 {
00344 if (i_idx >= m_numDesc) {
00345 throw new FD::GeneralException("Exception in VisualTarget::GetDescriptor: descriptor index is greater than the actual number of descriptors in current vector.",__FILE__,__LINE__);
00346 }
00347
00348 return (*m_targetDesc)[i_idx];
00349 }
00350
00351 const VisualFeatureDesc<FeatBaseType> *GetCstDescriptor(int i_idx) const
00352 {
00353 if (i_idx >= m_numDesc) {
00354 throw new FD::GeneralException("Exception in VisualTarget::GetCstDescriptor: descriptor index is greater than the actual number of descriptors in current vector.",__FILE__,__LINE__);
00355 }
00356
00357 return (const VisualFeatureDesc<FeatBaseType> *)((*m_targetDesc)[i_idx]);
00358 }
00359
00360 double *GetCueWeights()
00361 {
00362 return m_cueWeights;
00363 }
00364
00365 const double *GetCueWeights() const
00366 {
00367 return (const double *)m_cueWeights;
00368 }
00369
00370 double GetCueWeight(int i_idx) const
00371 {
00372 if (i_idx >= m_numDesc) {
00373 throw new FD::GeneralException("Exception in VisualTarget::GetCueWeight: index is greater than the actual number of descriptors in current vector.",__FILE__,__LINE__);
00374 }
00375
00376 return m_cueWeights[i_idx];
00377 }
00378
00379 void SetValidity(bool i_valid)
00380 {
00381 m_valid = i_valid;
00382 }
00383
00384 void SetID(int i_id)
00385 {
00386 m_id = i_id;
00387 }
00388
00389 void InitAges()
00390 {
00391 m_activeAge = 0;
00392 m_passiveAge = 0;
00393 }
00394
00395 void SetActiveAge(int i_age)
00396 {
00397 m_activeAge = i_age;
00398 }
00399
00400 void SetPassiveAge(int i_age)
00401 {
00402 m_passiveAge = i_age;
00403 }
00404
00405 void SetROI(VisualROI *i_roi)
00406 {
00407 if (!m_refROI.isNil()) {
00408 *m_refROI = *i_roi;
00409 }
00410 else {
00411 m_refROI = FD::RCPtr<VisualROI>(new VisualROI(*i_roi));
00412 }
00413 }
00414
00415
00416 void SetNumDescriptors(int i_numDesc)
00417 {
00418 if (m_targetDesc) {
00419
00420 for (int i=0; i<m_numDesc; i++) {
00421 delete (*m_targetDesc)[i];
00422 }
00423 }
00424
00425 if (m_numDesc != i_numDesc) {
00426 delete m_targetDesc;
00427 delete [] m_cueWeights;
00428 delete [] m_tmpCueProb;
00429
00430
00431 m_numDesc = i_numDesc;
00432 m_targetDesc = new FD::Vector<VisualFeatureDesc<FeatBaseType> *>(m_numDesc);
00433 m_cueWeights = new double[m_numDesc];
00434 m_tmpCueProb = new double[m_numDesc];
00435 }
00436 }
00437
00438 void SetCurDescIdx(int i_idx)
00439 {
00440 if (i_idx >= m_numDesc) {
00441 throw new FD::GeneralException("Exception in VisualTarget::SetCurDescIdx: descriptor index is greater than the actual number of descriptors in current vector.",__FILE__,__LINE__);
00442 }
00443
00444 m_curDescIdx = i_idx;
00445 }
00446
00447 void SetDescriptorsVec(FD::Vector<VisualFeatureDesc<FeatBaseType> *> *i_descVec)
00448 {
00449
00450 if (i_descVec->size() > 0 ) {
00451 this->SetNumDescriptors(i_descVec->size());
00452 for (int i=0; i<m_numDesc; i++) {
00453 (*m_targetDesc)[i] = (*i_descVec)[i]->clone();
00454 }
00455 }
00456 else {
00457 throw new FD::GeneralException("Exception in VisualTarget::SetDescriptorsVec: input feature vector must have at least one element.",__FILE__,__LINE__);
00458 }
00459 }
00460
00461 void SetDescriptor(VisualFeatureDesc<FeatBaseType> *i_desc, int i_idx)
00462 {
00463 if (i_idx >= m_numDesc) {
00464 throw new FD::GeneralException("Exception in VisualTarget::SetDescriptor: descriptor index is greater than the actual number of descriptors in current vector.",__FILE__,__LINE__);
00465 }
00466
00467 *((*m_targetDesc)[i_idx]) = *i_desc;
00468 }
00469
00470 void InitCueWeights()
00471 {
00472 double initWeight = 1.0/(double)m_numDesc;
00473
00474 for (int i=0; i<m_numDesc; i++) {
00475 m_cueWeights[i] = initWeight;
00476 }
00477 }
00478
00479 void SetCueWeights(double *i_weight)
00480 {
00481 for (int i=0; i<m_numDesc; i++) {
00482 m_cueWeights[i] = i_weight[i];
00483 }
00484 }
00485
00486 void SetCueWeight(double i_weight, int i_idx)
00487 {
00488 if (i_idx >= m_numDesc) {
00489 throw new FD::GeneralException("Exception in VisualTarget::SetCueWeight: index is greater than the actual number of descriptors in current vector.",__FILE__,__LINE__);
00490 }
00491
00492 m_cueWeights[i_idx] = i_weight;
00493 }
00494
00495 private:
00496 bool m_valid;
00497 int m_id;
00498 int m_activeAge;
00499 int m_passiveAge;
00500 FD::RCPtr<VisualROI> m_refROI;
00501 int m_numDesc;
00502 int m_curDescIdx;
00503 FD::Vector<VisualFeatureDesc<FeatBaseType> *> *m_targetDesc;
00504 double *m_cueWeights;
00505 double *m_tmpCueProb;
00506 };
00507
00508 }
00509
00510 #endif