From: Pierre M. <sid...@us...> - 2005-04-12 19:32:16
|
Update of /cvsroot/robotflow/RobotFlow/Vision/include In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4966 Added Files: ColorHistExtraction.h MeanShiftTracker.h VisualFeatureDesc.h VisualFeaturesExtraction.h VisualHistogramDesc.h VisualROI.h VisualTarget.h VisualTargetManager.h VisualTracker.h Log Message: --- NEW FILE: VisualFeaturesExtraction.h --- /* Copyright (C) 2005 Pierre Moisan (Pie...@US...) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _VISUALFEATURESEXTRACTION_H_ #define _VISUALFEATURESEXTRACTION_H_ #include "BufferedNode.h" #include <iostream> #include "Image.h" #include "cv.h" #include "VisualFeatureDesc.h" #include "VisualROI.h" template <class FeatBaseType> class VisualFeaturesExtraction : public BufferedNode { public: VisualFeaturesExtraction() { } VisualFeaturesExtraction(string nodeName, ParameterSet params) : BufferedNode(nodeName, params) { } virtual ~VisualFeaturesExtraction() { } // Default routine to print a VisualFeaturesExtraction object to an output stream virtual void printOn(ostream &out) const = 0; // Default routine to read a VisualFeaturesExtraction object from an input stream virtual void readFrom(istream &in) = 0; virtual void calculate(int output_id, int count, Buffer &out) = 0; virtual void ExtractFeatures(IplImage *i_frame, VisualROI *i_roi) = 0; virtual VisualFeatureDesc<FeatBaseType> *GetDescriptor() = 0; virtual const VisualFeatureDesc<FeatBaseType> *GetCstDescriptor() const = 0; }; #endif --- NEW FILE: MeanShiftTracker.h --- /* Copyright (C) 2005 Pierre Moisan (Pie...@US...) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _MEANSHIFTTRACKER_H_ #define _MEANSHIFTTRACKER_H_ #include "VisualTracker.h" class MeanShiftTracker : public VisualTracker { public: MeanShiftTracker(); MeanShiftTracker(int i_maxNumMSIter, double i_minMSDistEpsilon); MeanShiftTracker(string nodeName, ParameterSet params); virtual ~MeanShiftTracker(); // Default routine to print a MeanShiftTracker object to an output stream void printOn(ostream &out) const { throw new GeneralException("Exception in MeanShiftTracker::printOn: method not yet implemented.",__FILE__,__LINE__); } // Default routine to read a MeanShiftTracker object from an input stream void readFrom(istream &in) { throw new GeneralException("Exception in MeanShiftTracker::printOn: method not yet implemented.",__FILE__,__LINE__); } virtual void request(int output_id, const ParameterSet &req); void calculate(int output_id, int count, Buffer &out); private: // Input IDs (for BufferedNode) int m_imageInID; int m_targetInID; int m_msLocVecInID; int m_ppCompletedInID; // Output IDs (for BufferedNode) int m_finishedOutID; int m_imageOutID; int m_curTargetOutID; int m_targetOutID; int m_width; int m_height; int m_numChannels; int m_numPixels; int m_numBytesInFrame; int m_numIter; int m_maxNumMSIter; double m_minMSDistEpsilon; bool m_finished; bool m_initMS; VisualTarget<double> *m_curTarget; // Temporary image copy IplImage *m_curImage; }; #endif --- NEW FILE: VisualTarget.h --- /* Copyright (C) 2005 Pierre Moisan (Pie...@US...) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _VISUALTARGET_H_ #define _VISUALTARGET_H_ #include "BufferedNode.h" #include <iostream> #include "Image.h" #include "cv.h" #include "Vector.h" #include "VisualFeatureDesc.h" #include "VisualROI.h" template <class FeatBaseType> class VisualTarget : public Object { public: VisualTarget() : m_valid(false), m_id(0), m_activeAge(0), m_passiveAge(0), m_curDescIdx(0), m_roi(NULL), m_numDesc(-1), m_targetDesc(NULL), m_cueWeights(NULL), m_tmpCueProb(NULL) { } VisualTarget(int i_id, VisualROI *i_roi, Vector<VisualFeatureDesc<FeatBaseType> *> *i_targetDesc) : m_valid(true), m_id(i_id), m_activeAge(0), m_passiveAge(0), m_curDescIdx(0), m_roi(NULL), m_targetDesc(NULL), m_cueWeights(NULL), m_tmpCueProb(NULL) { m_roi = new VisualROI(*i_roi); m_numDesc = i_targetDesc->size(); m_targetDesc = new Vector<VisualFeatureDesc<FeatBaseType> *>(m_numDesc); for (int i=0; i<m_numDesc; i++) { (*m_targetDesc)[i] = new VisualFeatureDesc<FeatBaseType>(*((*i_targetDesc)[i])); } m_cueWeights = new double[m_numDesc]; m_tmpCueProb = new double[m_numDesc]; } VisualTarget::VisualTarget(const VisualTarget& i_ref) :m_id(-1), m_activeAge(-1), m_passiveAge(-1), m_curDescIdx(0), m_numDesc(-1), m_roi(NULL), m_targetDesc(NULL), m_cueWeights(NULL), m_tmpCueProb(NULL) { try { m_valid = i_ref.m_valid; m_id = i_ref.m_id; m_activeAge = i_ref.m_activeAge; m_passiveAge = i_ref.m_passiveAge; m_curDescIdx = i_ref.m_curDescIdx; if (i_ref.m_roi) { if (this->m_roi) { *(this->m_roi) = *(i_ref.m_roi); } else { this->m_roi = new VisualROI(*(i_ref.m_roi)); } } else { this->m_roi = NULL; } if (i_ref.m_targetDesc) { SetDescriptorsVec(i_ref.m_targetDesc); } else { this->m_targetDesc = NULL; this->m_numDesc = 0; this->m_cueWeights = NULL; this->m_tmpCueProb = NULL; } for (int i=0; i<m_numDesc; i++) { m_cueWeights[i] = i_ref.m_cueWeights[i]; m_tmpCueProb[i] = i_ref.m_tmpCueProb[i]; } } catch (BaseException *e) { throw e->add(new GeneralException("Exception caught in VisualTarget::VisualTarget:",__FILE__,__LINE__)); } } virtual ~VisualTarget() { delete m_roi; for (int i=0; i<m_numDesc; i++) { delete (*m_targetDesc)[i]; } delete m_targetDesc; delete [] m_cueWeights; delete [] m_tmpCueProb; } VisualTarget<FeatBaseType> & operator =(const VisualTarget<FeatBaseType> &i_ref) { try { // Avoid self assignment if (&i_ref != this) { this->m_valid = i_ref.m_valid; this->m_id = i_ref.m_id; this->m_activeAge = i_ref.m_activeAge; this->m_passiveAge = i_ref.m_passiveAge; this->m_curDescIdx = i_ref.m_curDescIdx; if (i_ref.m_roi) { if (this->m_roi) { *(this->m_roi) = *(i_ref.m_roi); } else { this->m_roi = new VisualROI(*(i_ref.m_roi)); } } else { this->m_roi = NULL; } if (i_ref.m_targetDesc) { SetDescriptorsVec(i_ref.m_targetDesc); } else { this->m_targetDesc = NULL; this->m_cueWeights = NULL; this->m_tmpCueProb = NULL; this->m_numDesc = 0; } for (int i=0; i<m_numDesc; i++) { m_cueWeights[i] = i_ref.m_cueWeights[i]; m_tmpCueProb[i] = i_ref.m_tmpCueProb[i]; } } return *this; } catch (BaseException *e) { throw e->add(new GeneralException("Exception caught in VisualTarget::operator=:",__FILE__,__LINE__)); } } // Default routine to print a VisualTarget object to an output stream void printOn(ostream &out) const { throw new GeneralException("Exception in VisualTarget::printOn: method not yet implemented.",__FILE__,__LINE__); } // Default routine to read a VisualTarget object from an input stream void readFrom(istream &in) { throw new GeneralException("Exception in VisualTarget::readFrom: method not yet implemented.",__FILE__,__LINE__); } void Adapt(Vector<VisualFeatureDesc<FeatBaseType> *> *i_desc, double i_rate) { if (m_numDesc != i_desc->size()) { throw new GeneralException("Exception in VisualTarget::Adapt: input descriptor vector size differs from current object's vector.",__FILE__,__LINE__); } for (int i=0; i<m_numDesc; i++) { if ((*m_targetDesc)[i]->GetType() != (*i_desc)[i]->GetType()) { throw new GeneralException("Exception in VisualTarget::Adapt: features descriptor must have the same type.",__FILE__,__LINE__); } (*m_targetDesc)[i]->Adapt((*i_desc)[i]->GetCstFeatures(), (*i_desc)[i]->GetSize(), i_rate); } } void Adapt(Vector<VisualFeatureDesc<FeatBaseType> *> *i_desc, double *i_rate) { if (m_numDesc != i_desc->size()) { throw new GeneralException("Exception in VisualTarget::Adapt: input descriptor vector size differs from current object's vector.",__FILE__,__LINE__); } for (int i=0; i<m_numDesc; i++) { if ((*m_targetDesc)[i]->GetType() != (*i_desc)[i]->GetType()) { throw new GeneralException("Exception in VisualTarget::Adapt: features descriptor must have the same type.",__FILE__,__LINE__); } (*m_targetDesc)[i]->Adapt((*i_desc)[i]->GetCstFeatures(), (*i_desc)[i]->GetSize(), i_rate[i]); } } double Similarity(Vector<VisualFeatureDesc<FeatBaseType> *> *i_desc) { if (m_numDesc != i_desc->size()) { throw new GeneralException("Exception in VisualTarget::Similarity: input descriptor vector size differs from current object's vector.",__FILE__,__LINE__); } double sim = 1.0; for (int i=0; i<m_numDesc; i++) { if ((*m_targetDesc)[i]->GetType() != (*i_desc)[i]->GetType()) { throw new GeneralException("Exception in VisualTarget::Similarity: features descriptor must have the same type.",__FILE__,__LINE__); } sim *= m_cueWeights[i]*(*m_targetDesc)[i]->Similarity((*i_desc)[i]->GetCstFeatures(), (*i_desc)[i]->GetSize()); } return sim; } double SimilarityWCueAdapt(Vector<VisualFeatureDesc<FeatBaseType> *> *i_desc, double i_rate) { if (m_numDesc != i_desc->size()) { throw new GeneralException("Exception in VisualTarget::SimilarityWCueAdapt: input descriptor vector size differs from current object's vector.",__FILE__,__LINE__); } int i; double sim = 1.0, cueSim, sumCueSim = 0.0; for (i=0; i<m_numDesc; i++) { if ((*m_targetDesc)[i]->GetType() != (*i_desc)[i]->GetType()) { throw new GeneralException("Exception in VisualTarget::SimilarityWCueAdapt: features descriptor must have the same type.",__FILE__,__LINE__); } m_tmpCueProb[i] = (*m_targetDesc)[i]->Similarity((*i_desc)[i]->GetCstFeatures(), (*i_desc)[i]->GetSize()); sumCueSim += m_tmpCueProb[i]; sim *= m_cueWeights[i]*m_tmpCueProb[i]; } // Adapt cue weight double rateInv = 1.0 - i_rate; double sumCueSimInv = 1.0/sumCueSim; sumCueSim = 0.0; if (sumCueSimInv == 0.0) { sumCueSimInv = 0.0; } for (i=0; i<m_numDesc; i++) { m_cueWeights[i] = i_rate*m_tmpCueProb[i]*sumCueSim + rateInv*m_cueWeights[i]; sumCueSim += m_cueWeights[i]; } // Normalize weights sumCueSimInv = 1.0/sumCueSim; if (sumCueSimInv == 0.0) { throw new GeneralException("Exception in VisualTarget::SimilarityWCueAdapt: cannot have a target with all cue weights are zero.",__FILE__,__LINE__); } for (i=0; i<m_numDesc; i++) { m_cueWeights[i] *= sumCueSimInv; } return sim; } void AgeTarget(bool i_matched) { if (i_matched) { if (m_activeAge < 15) { m_activeAge++; } if (m_passiveAge > 0) { m_passiveAge--; } } else { if (m_activeAge < 15) { m_passiveAge++; } if (m_activeAge > 0) { m_activeAge--; } } } bool IsValid() const { return m_valid; } int GetID() const { return m_id; } int GetActiveAge() const { return m_activeAge; } int GetPassiveAge() const { return m_passiveAge; } int GetCurrentAge() const { return m_activeAge - m_passiveAge; } VisualROI *GetROI() { return m_roi; } const VisualROI *GetCstROI() const { return (const VisualROI *)m_roi; } int GetNumDescriptors() const { return m_numDesc; } int GetCurDescIdx() const { return m_curDescIdx; } Vector<VisualFeatureDesc<FeatBaseType> *> *GetDescriptorsVec() { return m_targetDesc; } const Vector<VisualFeatureDesc<FeatBaseType> *> *GetCstDescriptorsVec() const { return (const Vector<VisualFeatureDesc<FeatBaseType> *> *)m_targetDesc; } VisualFeatureDesc<FeatBaseType> *GetDescriptor(int i_idx) { if (i_idx >= m_numDesc) { throw new GeneralException("Exception in VisualTarget::GetDescriptor: descriptor index is greater than the actual number of descriptors in current vector.",__FILE__,__LINE__); } return (*m_targetDesc)[i_idx]; } const VisualFeatureDesc<FeatBaseType> *GetCstDescriptor(int i_idx) const { if (i_idx >= m_numDesc) { throw new GeneralException("Exception in VisualTarget::GetCstDescriptor: descriptor index is greater than the actual number of descriptors in current vector.",__FILE__,__LINE__); } return (const VisualFeatureDesc<FeatBaseType> *)((*m_targetDesc)[i_idx]); } double *GetCueWeights() { return m_cueWeights; } const double *GetCueWeights() const { return (const double *)m_cueWeights; } double GetCueWeight(int i_idx) const { if (i_idx >= m_numDesc) { throw new GeneralException("Exception in VisualTarget::GetCueWeight: index is greater than the actual number of descriptors in current vector.",__FILE__,__LINE__); } return m_cueWeights[i_idx]; } void SetValidity(bool i_valid) { m_valid = i_valid; } void SetID(int i_id) { m_id = i_id; } void InitAges() { m_activeAge = 0; m_passiveAge = 0; } void SetActiveAge(int i_age) { m_activeAge = i_age; } void SetPassiveAge(int i_age) { m_passiveAge = i_age; } void SetROI(VisualROI *i_roi) { if (m_roi) { *m_roi = *i_roi; } else { m_roi = new VisualROI(*i_roi); } } // Requires to set descriptors afterwards void SetNumDescriptors(int i_numDesc) { if (m_targetDesc) { // First deallocate memory for (int i=0; i<m_numDesc; i++) { delete (*m_targetDesc)[i]; } } if (m_numDesc != i_numDesc) { delete m_targetDesc; delete [] m_cueWeights; delete [] m_tmpCueProb; // Reset vector size m_numDesc = i_numDesc; m_targetDesc = new Vector<VisualFeatureDesc<FeatBaseType> *>(m_numDesc); m_cueWeights = new double[m_numDesc]; m_tmpCueProb = new double[m_numDesc]; } } void SetCurDescIdx(int i_idx) { if (i_idx >= m_numDesc) { throw new GeneralException("Exception in VisualTarget::SetCurDescIdx: descriptor index is greater than the actual number of descriptors in current vector.",__FILE__,__LINE__); } m_curDescIdx = i_idx; } void SetDescriptorsVec(Vector<VisualFeatureDesc<FeatBaseType> *> *i_descVec) { // Reset the number of descriptors SetNumDescriptors(i_descVec->size()); for (int i=0; i<m_numDesc; i++) { (*m_targetDesc)[i] = (*i_descVec)[i]->clone(); } } void SetDescriptor(VisualFeatureDesc<FeatBaseType> *i_desc, int i_idx) { if (i_idx >= m_numDesc) { throw new GeneralException("Exception in VisualTarget::SetDescriptor: descriptor index is greater than the actual number of descriptors in current vector.",__FILE__,__LINE__); } *((*m_targetDesc)[i_idx]) = *i_desc; } void InitCueWeights() { double initWeight = 1.0/(double)m_numDesc; for (int i=0; i<m_numDesc; i++) { m_cueWeights[i] = initWeight; } } void SetCueWeights(double *i_weight) { for (int i=0; i<m_numDesc; i++) { m_cueWeights[i] = i_weight[i]; } } void SetCueWeight(double i_weight, int i_idx) { if (i_idx >= m_numDesc) { throw new GeneralException("Exception in VisualTarget::SetCueWeight: index is greater than the actual number of descriptors in current vector.",__FILE__,__LINE__); } m_cueWeights[i_idx] = i_weight; } private: bool m_valid; int m_id; int m_activeAge; int m_passiveAge; VisualROI *m_roi; int m_numDesc; int m_curDescIdx; Vector<VisualFeatureDesc<FeatBaseType> *> *m_targetDesc; double *m_cueWeights; double *m_tmpCueProb; }; #endif --- NEW FILE: VisualHistogramDesc.h --- /* Copyright (C) 2005 Pierre Moisan (Pie...@US...) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _VISUALHISTOGRAMFEATURE_H_ #define _VISUALHISTOGRAMFEATURE_H_ #include "Object.h" #include <iostream> #include <math.h> #include "VisualFeatureDesc.h" #include "VisualROI.h" #include "Vector.h" typedef enum { e_VISUALHIST_BhattacharyyaCoeff = 0, e_VISUALHIST_BhattacharyyaDist, // TODO: add other similarity methods e_VISUALHIST_unknownSimilarity } e_VISUALHIST_similarityType; // // BinType will be used to save each bin value // and should only be basic c/c++ types // e.g. double, int, char, ... // // FeatType is the type used for the feature map // that will used to compute the histogram // e.g. unsigned char if the feature map is // an image // template <class BinType, class FeatType> class VisualHistogramDesc : public VisualFeatureDesc<BinType> { public: static const double k_VISUALHIST_2DIVPI = 2.0/3.14159265358979323846; VisualHistogramDesc() : VisualFeatureDesc<BinType>(e_VISUALDESCRIPTOR_histogram), m_simType(e_VISUALHIST_unknownSimilarity), m_normFlag(false), m_numDimensions(0), m_numBins(NULL), m_binsWidth(NULL), m_bins(NULL) { } VisualHistogramDesc(e_VISUALHIST_similarityType i_simType, bool i_normFlag, unsigned int i_numDimensions, const Vector<int> *i_numBins) : VisualFeatureDesc<BinType>(e_VISUALDESCRIPTOR_histogram), m_simType(i_simType), m_normFlag(i_normFlag), m_numDimensions(i_numDimensions), m_numBins(NULL), m_binsWidth(NULL), m_bins(NULL) { unsigned int totalBins = 1; m_numBins = new int[m_numDimensions]; for(int d=0; d<m_numDimensions; d++) { m_numBins[d] = (*i_numBins)[d]; totalBins *= m_numBins[d]; } m_totalBins = totalBins; m_bins = new BinType[m_totalBins]; m_binsWidth = new FeatType[m_numDimensions]; m_lastMaxVal = (FeatType)0; SetSimilarityFct(); SetAdaptFct(); } VisualHistogramDesc(const VisualHistogramDesc<BinType,FeatType> &i_ref) { this->SetType(i_ref.GetType()); m_simType = i_ref.m_simType; m_normFlag = i_ref.m_normFlag; SetSimilarityFct(); SetAdaptFct(); m_numDimensions = i_ref.m_numDimensions; m_numBins = new int[m_numDimensions]; m_totalBins = i_ref.m_totalBins; m_bins = new BinType[m_totalBins]; m_binsWidth = new FeatType[m_numDimensions]; m_lastMaxVal = i_ref.m_lastMaxVal; for (int i=0; i<m_numDimensions; ++i) { m_binsWidth[i] = i_ref.m_binsWidth[i]; } for(int d=0; d<m_numDimensions; d++) { m_numBins[d] = i_ref.m_numBins[d]; } for (int b=0; b<m_totalBins; b++) { m_bins[b] = i_ref.m_bins[b]; } } ~VisualHistogramDesc() { delete [] m_numBins; delete [] m_binsWidth; delete [] m_bins; } VisualHistogramDesc<BinType,FeatType> & operator =(const VisualHistogramDesc<BinType,FeatType> &i_ref) { // Avoid self assignment if (&i_ref == this) { return *this; } this->SetType(i_ref.GetType()); m_simType = i_ref.m_simType; m_normFlag = i_ref.m_normFlag; SetSimilarityFct(); SetAdaptFct(); // Reallocate memory only if required if (m_numDimensions != i_ref.m_numDimensions) { delete [] m_numBins; delete [] m_binsWidth; m_numDimensions = i_ref.m_numDimensions; m_numBins = new int[m_numDimensions]; m_binsWidth = new FeatType[m_numDimensions]; } if (m_totalBins != i_ref.m_totalBins) { delete [] m_bins; m_totalBins = i_ref.m_totalBins; m_bins = new BinType[m_totalBins]; } m_lastMaxVal = i_ref.m_lastMaxVal; for (int i=0; i<m_numDimensions; ++i) { m_binsWidth[i] = i_ref.m_binsWidth[i]; } for(int d=0; d<m_numDimensions; d++) { m_numBins[d] = i_ref.m_numBins[d]; } for (int b=0; b<m_totalBins; b++) { m_bins[b] = i_ref.m_bins[b]; } return *this; } VisualHistogramDesc<BinType,FeatType>* clone() const { return new VisualHistogramDesc<BinType,FeatType>(*this); } // Default routine to print a VisualHistogramDesc object to an output stream void printOn(std::ostream &out) const { int b, d; out << "<VisualHistogramDesc " << endl; out << "<NumDimensions " << m_numDimensions << " >" << endl; out << "<TotalBins " << m_totalBins << " >" << endl; out << "<SimilarityType " << (int)m_simType << " >" << endl; out << "<NormFlag " << (int)m_normFlag << " >" << endl; out << "<LastMaxVal " << m_lastMaxVal << " >" << endl; out << "<NumBins " << endl; for (d=0; d<m_numDimensions; d++) { out << m_numBins[d] << " "; } out << " >" << endl; out << "<BinsWidth " << endl; for (d=0; d<m_numDimensions; d++) { out << m_binsWidth[d] << " "; } out << " >" << endl; out << "<Bins " << endl; for (b=0; b<m_totalBins; ++b) { out << m_bins[b] << " "; } out << " >" << endl; out << " >" << endl; } // Default routine to read a VisualHistogramDesc object from an input stream void readFrom(std::istream &in) { std::string tag; while (1) { char ch; in >> ch; if (ch == '>') { break; } else if (ch != '<') { throw new GeneralException ("VisualHistogramDesc::readFrom : Parse error: '<' expected",__FILE__,__LINE__); } in >> tag; if (tag == "VisualHistogramDesc") { continue; } else if (tag == "NumDimensions") { in >> m_numDimensions; if (m_numDimensions < 1) { throw new GeneralException ("VisualHistogramDesc::readFrom : invalid number of dimensions",__FILE__,__LINE__); } } else if (tag == "TotalBins") { in >> m_totalBins; if (m_totalBins < 1) { throw new GeneralException ("VisualHistogramDesc::readFrom : invalid total number of bins",__FILE__,__LINE__); } } else if (tag == "SimilarityType") { int val; in >> val; m_simType = e_VISUALHIST_similarityType(val); SetSimilarityFct(); } else if (tag == "NormFlag") { int val; in >> val; m_normFlag = bool(val); SetAdaptFct(); } else if (tag == "LastMaxVal") { in >> m_lastMaxVal; } else if (tag == "NumBins") { // Allocate memory delete [] m_numBins; m_numBins = new int[m_numDimensions]; for (int d=0; d<m_numDimensions; d++) { in >> m_numBins[d]; } } else if (tag == "BinsWidth") { // Allocate memory delete [] m_binsWidth; m_binsWidth = new FeatType[m_numDimensions]; for (int d=0; d<m_numDimensions; d++) { in >> m_binsWidth[d]; } } else if (tag == "Bins") { // Allocate memory delete [] m_bins; m_bins = new BinType[m_totalBins]; for (int b=0; b<m_totalBins; b++) { in >> m_bins[b]; } } else { throw new GeneralException ("VisualHistogramDesc::readFrom : Unknown argument: " + tag,__FILE__,__LINE__); } if (!in) { throw new GeneralException ("VisualHistogramDesc::readFrom : Parse error trying to build " + tag,__FILE__,__LINE__); } in >> tag; if (tag != ">") { throw new GeneralException ("VisualHistogramDesc::readFrom : Parse error: '>' expected ",__FILE__,__LINE__); } } } double Similarity(const BinType *i_candidate, unsigned int i_size) const { try { if (m_totalBins != i_size ) { throw new GeneralException ("VisualHistogramDesc::Similarity : number of histogram bins differs from current descriptor",__FILE__,__LINE__); } // Use appropriate function return (this->*m_similarityFct)(i_candidate); } catch (BaseException *e) { throw e->add(new GeneralException("Exception caught in VisualHistogramDesc::Similarity:",__FILE__,__LINE__)); } } void Adapt(const BinType *i_candidate, unsigned int i_size, double i_rate) { try { if (m_totalBins != i_size ) { throw new GeneralException ("VisualHistogramDesc::Adapt : number of histogram bins differs from current descriptor",__FILE__,__LINE__); } if (i_rate < 0.0 || i_rate > 1.0) { throw new GeneralException ("VisualHistogramDesc::Adapt : adaptation rate must be in the interval [0.0,1.0]",__FILE__,__LINE__); } if (i_rate == 0.0) { // Nothing to do return; } if (i_rate == 1.0) { SetFeatures(i_candidate, i_size); return; } // Use appropriate function (this->*m_adaptFct)(i_candidate, i_rate); } catch (BaseException *e) { throw e->add(new GeneralException("Exception caught in VisualHistogramDesc::Adapt:",__FILE__,__LINE__)); } } void ComputeHistStd(const FeatType* i_featMap, FeatType i_maxValPlusOne, BinType i_binIncScale, int i_width, int i_height, const VisualROI *i_roi) { if (!m_bins) { throw new GeneralException ("VisualHistogramDesc::ComputeHistStd : histogram bins must be initialized.",__FILE__,__LINE__); } int hx=i_roi->GetHSX(), hy=i_roi->GetHSY(); int xCen = i_roi->GetXCen(); int yCen = i_roi->GetYCen(); const FeatType *p_features; const unsigned char *p_mask = i_roi->GetCstMask(); int val, fact, indx, maskLeftOffset, maskRightOffset; int x, y, i, j, numFeatures, xLeftOffset, xRightOffset, yTopOffset, yBottomOffset; BinType weightSum = (BinType)0; if (m_lastMaxVal != i_maxValPlusOne) { m_lastMaxVal = i_maxValPlusOne; for (i=0; i<m_numDimensions; ++i) { m_binsWidth[i] = (FeatType)((double)i_maxValPlusOne/(double)m_numBins[i] + 0.5); } } for (i=0; i<m_totalBins; ++i) { m_bins[i] = (BinType)(0); } // Adjust region offsets to fit in feature map only if (xCen-hx < 0) { xLeftOffset = 0; maskLeftOffset = hx-xCen; } else { xLeftOffset = xCen-hx; maskLeftOffset = 0; } if (xCen+hx > i_width-1) { xRightOffset = i_width-1; maskRightOffset = xCen+hx - i_width-1; } else { xRightOffset = xCen+hx; maskRightOffset = 0; } if (yCen-hy < 0) { yTopOffset = 0; p_mask += (hy-yCen)*(2*hx+1); } else { yTopOffset = yCen-hy; } if (yCen+hy > i_height-1) { yBottomOffset = i_height-1; } else { yBottomOffset = yCen+hy; } p_features = (const FeatType *)(i_featMap + m_numDimensions*(yTopOffset*i_width + xLeftOffset)); // For each pixels in the region of interest, compute the histogram bin index // then add "1" to this bin. for (y=yTopOffset; y<=yBottomOffset; y++) { numFeatures = 0; p_mask += maskLeftOffset; for (x=xLeftOffset; x<=xRightOffset; x++) { if (*p_mask++) { indx = 0; for (i=0; i<m_numDimensions; ++i) { fact = 1; val = (*p_features)/m_binsWidth[i]; p_features++; for (j=i; j>0; --j) { fact *= m_numBins[j-1]; } indx += val*fact; } if (indx < 0 || indx >= m_totalBins) { throw new GeneralException ("VisualHistogramDesc::ComputeHistStd : invalid histogram bin index",__FILE__,__LINE__); } m_bins[indx]++; weightSum++; } else { p_features += m_numDimensions; } numFeatures++; } p_features += m_numDimensions*(i_width-numFeatures); p_mask += maskRightOffset; } if (m_normFlag) { NormalizeBins(weightSum); } } void ComputeKernelWeightedHist(const FeatType* i_featMap, FeatType i_maxValPlusOne, BinType i_binIncScale, int i_width, int i_height, const VisualROI *i_roi) { if (!m_bins) { throw new GeneralException ("VisualHistogramDesc::ComputeKernelWeightedHist : histogram bins must be initialized.",__FILE__,__LINE__); } int hx=i_roi->GetHSX(), hy=i_roi->GetHSY(); int xCen = i_roi->GetXCen(); int yCen = i_roi->GetYCen(); const FeatType *p_features; const unsigned char *p_mask = i_roi->GetCstMask(); int val, fact, indx, maskLeftOffset, maskRightOffset; int x, y, i, j, numFeatures, xLeftOffset, xRightOffset, yTopOffset, yBottomOffset; double xnorm, ynorm, weight; BinType weightSum = (BinType)0; if (m_lastMaxVal != i_maxValPlusOne) { m_lastMaxVal = i_maxValPlusOne; for (i=0; i<m_numDimensions; ++i) { m_binsWidth[i] = (FeatType)round((double)i_maxValPlusOne/(double)m_numBins[i] + 0.5); } } for (i=0; i<m_totalBins; ++i) { m_bins[i] = (BinType)(0); } // Adjust region offsets to fit in feature map only if (xCen-hx < 0) { xLeftOffset = 0; maskLeftOffset = hx-xCen; } else { xLeftOffset = xCen-hx; maskLeftOffset = 0; } if (xCen+hx > i_width-1) { xRightOffset = i_width-1; maskRightOffset = xCen+hx - i_width-1; } else { xRightOffset = xCen+hx; maskRightOffset = 0; } if (yCen-hy < 0) { yTopOffset = 0; p_mask += (hy-yCen)*(2*hx+1); } else { yTopOffset = yCen-hy; } if (yCen+hy > i_height-1) { yBottomOffset = i_height-1; } else { yBottomOffset = yCen+hy; } p_features = (const FeatType *)(i_featMap + m_numDimensions*(yTopOffset*i_width + xLeftOffset)); // For each pixels in the region of interest, compute the histogram bin index // then compute this pixel weight base on its distance from the center of the // region using a kernel profile. This weight will be added in the appropriate // histogram bin. for (y=yTopOffset; y<=yBottomOffset; y++) { numFeatures = 0; p_mask += maskLeftOffset; for (x=xLeftOffset; x<=xRightOffset; x++) { if (*p_mask++) { indx = 0; for (i=0; i<m_numDimensions; ++i) { fact = 1; val = (*p_features)/m_binsWidth[i]; p_features++; for (j=i; j>0; --j) { fact *= m_numBins[j-1]; } indx += val*fact; } if (indx < 0 || indx >= m_totalBins) { throw new GeneralException ("VisualHistogramDesc::ComputeKernelWeightedHist : invalid histogram bin index",__FILE__,__LINE__); } // Compute normalized distance of the pixel from the center // assuming a unit circle region. xnorm = (double)(x-xCen)/(double)(hx); ynorm = (double)(y-yCen)/(double)(hy); // Using by default the Epanechnikov kernel // TODO: make kernel profiles enum to choose from weight = EpanechKernel(xnorm*xnorm + ynorm*ynorm); BinType histWeight = (BinType)(weight*(double)(i_binIncScale)); weightSum += histWeight; m_bins[indx] += histWeight; } else { p_features += m_numDimensions; } numFeatures++; } p_features += m_numDimensions*(i_width-numFeatures); p_mask += maskRightOffset; } if (m_normFlag && weightSum) { NormalizeBins(weightSum); } } void ComputeMSLocation(const FeatType* i_featMap, FeatType i_maxValPlusOne, BinType i_binIncScale, int i_width, int i_height, const VisualROI *i_roi, const BinType *i_refBins, double i_cueWeight, Vector<double> *o_msLoc) { if (!m_bins) { throw new GeneralException ("VisualHistogramDesc::ComputeMSLocation : histogram bins must be initialized.",__FILE__,__LINE__); } int hx=i_roi->GetHSX(), hy=i_roi->GetHSY(); int xCen = i_roi->GetXCen(); int yCen = i_roi->GetYCen(); const FeatType *p_features; const unsigned char *p_mask = i_roi->GetCstMask(); int val, fact, indx, maskLeftOffset, maskRightOffset; int x, y, i, j, numFeatures, xLeftOffset, xRightOffset, yTopOffset, yBottomOffset; double xnorm, ynorm; double tmpRef, tmpCand, posWeight, kernWeight; double numeratorX=0.0, numeratorY=0.0, denominator=0.0; // Adjust region offsets to fit in feature map only if (xCen-hx < 0) { xLeftOffset = 0; maskLeftOffset = hx-xCen; } else { xLeftOffset = xCen-hx; maskLeftOffset = 0; } if (xCen+hx > i_width-1) { xRightOffset = i_width-1; maskRightOffset = xCen+hx - i_width-1; } else { xRightOffset = xCen+hx; maskRightOffset = 0; } if (yCen-hy < 0) { yTopOffset = 0; p_mask += (hy-yCen)*(2*hx+1); } else { yTopOffset = yCen-hy; } if (yCen+hy > i_height-1) { yBottomOffset = i_height-1; } else { yBottomOffset = yCen+hy; } p_features = (const FeatType *)(i_featMap + m_numDimensions*(yTopOffset*i_width + xLeftOffset)); for (y=yTopOffset; y<=yBottomOffset; y++) { numFeatures = 0; p_mask += maskLeftOffset; for (x=xLeftOffset; x<=xRightOffset; x++) { if (*p_mask++) { indx = 0; for (i=0; i<m_numDimensions; ++i) { fact = 1; val = (*p_features)/m_binsWidth[i]; p_features++; for (j=i; j>0; --j) { fact *= m_numBins[j-1]; } indx += val*fact; } if (indx < 0 || indx >= m_totalBins) { throw new GeneralException ("VisualHistogramDesc::ComputeMSLocation : invalid histogram bin index",__FILE__,__LINE__); } // Compute normalized distance of the pixel from the center // assuming a unit circle region. xnorm = (double)(x-xCen)/(double)(hx); ynorm = (double)(y-yCen)/(double)(hy); tmpRef = (double)(i_refBins[indx]); tmpCand = (double)(m_bins[indx]); if (tmpCand > 0.0) { posWeight = sqrt(tmpRef/tmpCand); kernWeight = DerivedEpanechKernel(xnorm*xnorm + ynorm*ynorm); numeratorX += posWeight*kernWeight*x; numeratorY += posWeight*kernWeight*y; denominator += posWeight*kernWeight; } } else { p_features += m_numDimensions; } numFeatures++; } p_features += m_numDimensions*(i_width-numFeatures); p_mask += maskRightOffset; } if (denominator > 0.0) { // Mean shift x location (*o_msLoc)[0] = i_cueWeight*(numeratorX/denominator); // Mean shift y location (*o_msLoc)[1] = i_cueWeight*(numeratorY/denominator); } else { // Invalid mean shift estimation. // Output current ROI center location. // Mean shift x location (*o_msLoc)[0] = xCen; // Mean shift y location (*o_msLoc)[1] = yCen; } } unsigned int GetNumDimensions() const { return m_numDimensions; } unsigned int GetSize() const { return m_totalBins; } unsigned int *GetNumBinsPtr() { return (const unsigned int *)m_numBins; } const unsigned int *GetCstNumBinsPtr() const { return (const unsigned int *)m_numBins; } FeatType *GetBinsWidthPtr() { return m_binsWidth; } const FeatType *GetCstBinsWidthPtr() const { return (const FeatType *)m_binsWidth; } BinType *GetFeatures() { return m_bins; } const BinType *GetCstFeatures() const { return (const BinType *)m_bins; } // WARNING: this function modifies only the histogram bins void SetSize(unsigned int i_size) { delete [] m_bins; m_totalBins = i_size; m_bins = new BinType[m_totalBins]; } // A more complete Set function for the histogram bins void SetBins(unsigned int i_numDimensions, unsigned int *i_numBins) { m_numDimensions = i_numDimensions; unsigned int totalBins = 1; delete [] m_numBins; m_numBins = new unsigned int[m_numDimensions]; delete [] m_binsWidth; m_binsWidth = new FeatType[m_numDimensions]; m_lastMaxVal = (FeatType)0; for(int d=0; d<m_numDimensions; d++) { m_numBins[d] = i_numBins[d]; totalBins *= m_numBins[d]; } if (totalBins != m_totalBins) { SetSize(totalBins); } } // WARNING: assumes that i_ref has the same size as the current object void SetFeatures(const BinType *i_ref, unsigned int i_size) { if (m_totalBins != i_size ) { throw new GeneralException ("VisualHistogramDesc::SetFeatures : number of histogram bins differs from current descriptor",__FILE__,__LINE__); } for (int b=0; b<m_totalBins; b++) { m_bins[b] = i_ref[b]; } } private: void SetSimilarityFct() { if (m_simType == e_VISUALHIST_BhattacharyyaCoeff) { m_similarityFct = &VisualHistogramDesc::BhattacharyyaCoeff; } else if (m_simType == e_VISUALHIST_BhattacharyyaDist) { m_similarityFct = &VisualHistogramDesc::BhattacharyyaDist; } else { m_similarityFct = NULL; } } void SetAdaptFct() { if (m_normFlag) { m_adaptFct = &VisualHistogramDesc::AdaptNorm; } else { m_adaptFct = &VisualHistogramDesc::AdaptOnly; } } double BhattacharyyaDist(const BinType *i_candidateBins) const { try { return sqrt(1.0-BhattacharyyaCoeff(i_candidateBins)); } catch (BaseException *e) { throw e->add(new GeneralException("Exception caught in VisualHistogramDesc::BhattacharyyaDist:",__FILE__,__LINE__)); } } double BhattacharyyaCoeff(const BinType *i_candidateBins) const { int b; double coeff = 0.0; const BinType *p_curBins = (const BinType *)m_bins; const BinType *p_candidateBins = i_candidateBins; for (int b=0; b<m_totalBins; b++) { coeff += sqrt((*p_curBins) * (*p_candidateBins)); p_curBins++; p_candidateBins++; } //return coeff/sqrt((double)m_totalBins); return coeff; } void AdaptNorm(const BinType *i_candidateBins, double i_rate) { int b; const BinType *p_adaptBins = i_candidateBins; double compRate = 1.0 - i_rate; BinType sum = (BinType)0; for (b=0; b<m_totalBins; b++) { m_bins[b] = (BinType)(compRate*(double)(m_bins[b]) + i_rate*(double)(p_adaptBins[b])); sum += m_bins[b]; } if (sum != (BinType)0) { NormalizeBins(sum); } } void AdaptOnly(const BinType *i_candidateBins, double i_rate) { int b; const BinType *p_adaptBins = i_candidateBins; double compRate = 1.0 - i_rate; for (b=0; b<m_totalBins; b++) { m_bins[b] = (BinType)(compRate*(double)(m_bins[b]) + i_rate*(double)(p_adaptBins[b])); } } void NormalizeBins(BinType i_fact) { int b; double invFact = 1.0/(double)i_fact; for (b=0; b<m_totalBins; ++b) { m_bins[b] = (BinType)((double)(m_bins[b])*invFact); } } inline double EpanechKernel(double i_dist) const { return max( 0.0, k_VISUALHIST_2DIVPI * (1.0 - i_dist) ); } inline double DerivedEpanechKernel(double i_dist) const { if (i_dist <= 1.0) { return 1.0; } return 0.0; } private: // Similarity/Distance type to use e_VISUALHIST_similarityType m_simType; // Flag to normalize histogram bins bool m_normFlag; // Numer of dimensions of the histogram unsigned int m_numDimensions; // Total number of bins in the histogram unsigned int m_totalBins; // Number of bins for each dimension int *m_numBins; // Width (in terms of the feature type) of each bin FeatType *m_binsWidth; // Last maximum value used to compute the width of each bin FeatType m_lastMaxVal; // Histogram bin data BinType *m_bins; // Function pointer to the appropriate private similarity routine double (VisualHistogramDesc::*m_similarityFct)(const BinType *) const; // Function pointer to the appropriate private adaptation routine void (VisualHistogramDesc::*m_adaptFct)(const BinType *, double); }; #endif --- NEW FILE: VisualROI.h --- /* Copyright (C) 2005 Pierre Moisan (Pie...@US...) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _VISUALROI_H_ #define _VISUALROI_H_ #include "Object.h" #include <iostream> #include <math.h> #include "cv.h" typedef enum { e_VISUALROI_rectangular = 0, e_VISUALROI_elliptical, e_VISUALROI_unknown } e_VISUALROI_type; class VisualROI : public Object { public: VisualROI(); VisualROI(e_VISUALROI_type i_type, int i_xCen, int i_yCen, int i_hsX, int i_hsY, int i_angle); VisualROI(const VisualROI& i_ref); virtual ~VisualROI(); VisualROI & operator =(const VisualROI &i_ref); // Default routine to print a VisualROI object to an output stream void printOn(ostream &out) const; // Default routine to read a VisualROI object from an input stream void readFrom(istream &in); void DrawROI(IplImage *io_frame, const unsigned char *i_color) const; void Reset(int i_hsX, int i_hsY, int i_angle); e_VISUALROI_type GetType() const; int GetPerimLength() const; short *GetPerim(); const short *GetCstPerim() const; float *GetNormVects(); const float *GetCstNormVects() const; int GetXCen() const; int GetYCen() const; int GetHSX() const; int GetHSY() const; int GetAngle() const; int GetArea() const; unsigned char *GetMask(); const unsigned char *GetCstMask() const; void SetXCen(int i_xCen); void SetYCen(int i_yCen); void SetHSX(int i_hsX); void SetHSY(int i_hsY); void SetAngle(int i_angle); private: void MakeRectangularRegion(); void MakeEllipticalRegion(); int MakeRegionMask(); private: // Region geometric type e_VISUALROI_type m_type; // Number of pixels along perimeter int m_perimLength; // Perimeter pixels short *m_perim; // Contour normal vectors float *m_normVects; // X value for the region center position int m_xCen; // Y value for the region center position int m_yCen; // Halfsize for x axis int m_hsX; // Halfsize for y axis int m_hsY; // Orientation of the region in degrees int m_angle; // Number of pixels in mask int m_area; // (2*m_hsX+1) X (2*m_hsY+1) region unsigned char *m_mask; }; #endif --- NEW FILE: VisualFeatureDesc.h --- /* Copyright (C) 2005 Pierre Moisan (Pie...@US...) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _VISUALFEATUREDESC_H_ #define _VISUALFEATUREDESC_H_ #include "Object.h" #include <iostream> typedef enum { e_VISUALDESCRIPTOR_histogram = 0, e_VISUALDESCRIPTOR_statistics, e_VISUALDESCRIPTOR_point, e_VISUALDESCRIPTOR_unknown } e_VISUALDESCRIPTOR_type; // // This should be an abstract base class. Since many containers (like Vector) // do not allow abstract functions, each abstract function throws an exception // to avoid the direct use of the base class "abstract" routines. // template <class FeatBaseType> class VisualFeatureDesc : public Object { friend std::ostream &operator<<(std::ostream &o_out, const VisualFeatureDesc<FeatBaseType> &i_ref) { try { i_ref.printOn(o_out); // Enable cascading return o_out; } catch (BaseException *e) { throw e->add(new GeneralException("Exception in VisualFeatureDesc::operator<<:",__FILE__,__LINE__)); } } friend std::istream &operator>>(std::istream &i_in, VisualFeatureDesc<FeatBaseType> &o_ref) { try { o_ref.readFrom(i_in); // Enable cascading return i_in; } catch (BaseException *e) { throw e->add(new GeneralException("Exception in VisualFeatureDesc::operator>>:",__FILE__,__LINE__)); } } public: VisualFeatureDesc() : m_descType(e_VISUALDESCRIPTOR_unknown) { } VisualFeatureDesc(e_VISUALDESCRIPTOR_type i_descType) : m_descType(i_descType) { } VisualFeatureDesc(const VisualFeatureDesc<FeatBaseType> &i_ref) { cout << "Using VisualFeatureDesc::copy constructor" << endl; m_descType = i_ref.m_descType; } virtual ~VisualFeatureDesc() { } virtual VisualFeatureDesc<FeatBaseType> & operator =(const VisualFeatureDesc<FeatBaseType> &i_ref) { cout << "Using VisualFeatureDesc::operator=" << endl; // Avoid self assignment if (&i_ref != this) { this->m_descType = i_ref.m_descType; } return *this; } virtual VisualFeatureDesc<FeatBaseType>* clone() const { cout << "Using VisualFeatureDesc::clone" << endl; return new VisualFeatureDesc<FeatBaseType>(*this); } // Default routine to print a VisualFeatureDesc object to an output stream virtual void printOn(std::ostream &out) const { throw new GeneralException("Exception in VisualFeatureDesc::printOn: cannot use base class routine.",__FILE__,__LINE__); } // Default routine to read a VisualFeatureDesc object from an input stream virtual void readFrom(std::istream &in) { throw new GeneralException("Exception in VisualFeatureDesc::readFrom: cannot use base class routine.",__FILE__,__LINE__); } virtual double Similarity(const FeatBaseType *i_candidate, unsigned int i_size) const { throw new GeneralException("Exception in VisualFeatureDesc::Similarity: cannot use base class routine.",__FILE__,__LINE__); } virtual void Adapt(const FeatBaseType *i_candidate, unsigned int i_size, double i_rate) { throw new GeneralException("Exception in VisualFeatureDesc::Adapt: cannot use base class routine.",__FILE__,__LINE__); } virtual unsigned int GetSize() const { throw new GeneralException("Exception in VisualFeatureDesc::GetSize: cannot use base class routine.",__FILE__,__LINE__); } virtual FeatBaseType *GetFeatures() { throw new GeneralException("Exception in VisualFeatureDesc::GetFeatures: cannot use base class routine.",__FILE__,__LINE__); } virtual const FeatBaseType *GetCstFeatures() const { throw new GeneralException("Exception in VisualFeatureDesc::GetCstFeatures: cannot use base class routine.",__FILE__,__LINE__); } e_VISUALDESCRIPTOR_type GetType() const { return m_descType; } void SetType(e_VISUALDESCRIPTOR_type i_type) { m_descType = i_type; } virtual void SetSize(unsigned int i_size) { throw new GeneralException("Exception in VisualFeatureDesc::SetSize: cannot use base class routine.",__FILE__,__LINE__); } virtual void SetFeatures(const FeatBaseType *i_ref, unsigned int i_size) { throw new GeneralException("Exception in VisualFeatureDesc::SetFeatures: cannot use base class routine.",__FILE__,__LINE__); } private: e_VISUALDESCRIPTOR_type m_descType; }; #endif --- NEW FILE: VisualTargetManager.h --- /* Copyright (C) 2005 Pierre Moisan (Pie...@US...) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _VISUALTRACKER_H_ #define _VISUALTRACKER_H_ #include "BufferedNode.h" #include <iostream> #include "Image.h" #include "cv.h" #include "VisualTarget.h" #include <stdlib.h> #include <sys/timeb.h> class VisualTargetManager : public BufferedNode { friend class BufferedNode; public: VisualTargetManager(); VisualTargetManager(string nodeName, ParameterSet params); virtual ~VisualTargetManager(); // Default routine to print a VisualTargetManager object to an output stream void printOn(ostream &out) const { throw new GeneralException("Exception in VisualTargetManager::printOn: method not yet implemented.",__FILE__,__LINE__); } // Default routine to read a VisualTargetManager object from an input stream void readFrom(istream &in) { throw new GeneralException("Exception in VisualTargetManager::printOn: method not yet implemented.",__FILE__,__LINE__); } virtual void request(int output_id, const ParameterSet &req); void calculate(int output_id, int count, Buffer &out); private: // Input IDs (for BufferedNode) int m_imageInID; int m_roiInID; int m_featVecInID; int m_targetInID; int m_ppCompletedInID; // Output IDs (for BufferedNode) int m_imageOutID; int m_roiOutID; int m_targetOutID; int m_targetProbOutID; int m_targetDXOutID; int m_targetDYOutID; int m_nameOutID; int m_width; int m_height; int m_numChannels; int m_numPixels; int m_numBytesInFrame; float m_imgXCen; float m_imgYCen; unsigned char m_roiColor[3]; struct timeb m_t1, m_t2; int m_maxNumTargets; double m_targetMatchThres; double m_targetAdaptThres; double m_targetAdaptRate; double m_cueAdaptRate; bool m_ppCompleted; VisualTarget<double> *m_target; RCPtr<VisualTarget<double> > m_refTarget; //RCPtr<VisualROI> m_refROI; // Temporary image copy IplImage *m_curImage; }; #endif --- NEW FILE: VisualTracker.h --- /* Copyright (C) 2005 Pierre Moisan (Pie...@US...) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _VISUALTRACKER_H_ #define _VISUALTRACKER_H_ #include "BufferedNode.h" #include <iostream> #include "Image.h" #include "cv.h" #include "VisualFeatureDesc.h" #include "VisualROI.h" #include "VisualTarget.h" class VisualTracker : public BufferedNode { public: VisualTracker() { } VisualTracker(string nodeName, ParameterSet params) : BufferedNode(nodeName, params) { } virtual ~VisualTracker() { } // Default routine to print a VisualTracker object to an output stream virtual void printOn(ostream &out) const = 0; // Default routine to read a VisualTracker object from an input stream virtual void readFrom(istream &in) = 0; virtual void calculate(int output_id, int count, Buffer &out) = 0; //virtual void TrackTarget() = 0; }; #endif --- NEW FILE: ColorHistExtraction.h --- /* Copyright (C) 2005 Pierre Moisan (Pie...@US...) This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _COLORHISTEXTRACTION_H_ #define _COLORHISTEXTRACTION_H_ #include "VisualFeaturesExtraction.h" #include "VisualHistogramDesc.h" #include "VisualTarget.h" #include <stdlib.h> #include <sys/timeb.h> // // Color histogram Features Extraction for RobotFlow // class ColorHistExtraction : public VisualFeaturesExtraction<double> { public: // // Default constructor for Object // ColorHistExtraction(); // // Constructor with complete intialisation // ColorHistExtraction(int i_width, int i_height, int i_numChannels, const Vector<int> *i_numBins); // // Copy constructor // ColorHistExtraction(const ColorHistExtraction& i_ref); // // BufferedNode constructor // ColorHistExtraction(string nodeName, ParameterSet params); // // Constructor using input stream // ColorHistExtraction(istream &in) { readFrom(in); } virtual ~ColorHistExtraction(); // Default routine to print a ColorHistExtraction object to an output stream void printOn(ostream &out) const { throw new GeneralException("Exception in ColorHistExtraction::printOn: method not yet implemented.",__FILE__,__LINE__); } // Default routine to read a ColorHistExtraction object from an input stream void readFrom(istream &in) { throw new GeneralException("Exception in ColorHistExtraction::readFrom: method not yet implemented.",__FILE__,__LINE__); } virtual void request(int output_id, const ParameterSet &req); void calculate(int output_id, int count, Buffer &out); void ExtractFeatures(VisualROI *i_roi); void ExtractFeatures(IplImage *i_input, VisualROI *i_roi); void EstimateMSLocation(const VisualTarget<double> *i_targetRef, int i_descIdx, Vector<double> *o_msLocVec); VisualFeatureDesc<double> *GetDescriptor() { return m_colorHistogram; } const VisualFeatureDesc<double> *GetCstDescriptor() const { return (const VisualHistogramDesc<double, unsigned char> *)m_colorHistogram; } private: void Initialize(const Vector<int> *i_numBins); private: // Input IDs (for BufferedNode) int m_imageInID; int m_numBinsInID; int m_roiInID; int m_targetInID; int m_targetDescIdxInID; int m_useNextImgInID; // Output IDs (for BufferedNode) int m_featuresOutID; int m_msLocOutID; int m_ppCompletedOutID; // Width of images int m_width; // Height of images int m_height; // Number of channels in an image int m_numChannels; // Number of pixels in an image int m_numPixels; // Number of bytes in an image int m_numBytesInFrame; // Initialization flag bool m_init; // Current region of interest VisualROI *m_roi; // Histogram descriptor for region of interest VisualHistogramDesc<double, unsigned char> *m_colorHistogram; // Function pointer to the appropriate private extraction routine void (ColorHistExtraction::*m_extractionFct)(unsigned char *); // Temporary image copy IplImage *m_curImage; }; #endif |