From: Pierre M. <sid...@us...> - 2005-06-02 16:49:48
|
Update of /cvsroot/robotflow/RobotFlow/Vision/include In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11331 Added Files: IntegralColorExtraction.h IntegralEdgesOriExtraction.h IntegralLBPExtraction.h VisualIntegralDesc.h Log Message: New integral image features extraction and descriptor. --- NEW FILE: IntegralLBPExtraction.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 _INTEGRALLBPEXTRACTION_H_ #define _INTEGRALLBPEXTRACTION_H_ #include "VisualFeaturesExtraction.h" #include "VisualIntegralDesc.h" #include "VisualTarget.h" #include <stdlib.h> #include <sys/timeb.h> namespace RobotFlow { // // Integral LBP (Lcoal Binary Pattern) Features Extraction for RobotFlow // class IntegralLBPExtraction : public VisualFeaturesExtraction<double> { public: // // Default constructor for Object // IntegralLBPExtraction(); // // Constructor with complete intialisation // IntegralLBPExtraction(int i_width, int i_height, int i_numChannels, int i_numHoriIntRect, int i_numVertIntRect, int i_numSamples, int i_predicate, bool i_doInterpolation, bool i_useUniform, int i_startAngle, double i_maxValue); // // BufferedNode constructor // IntegralLBPExtraction(std::string nodeName, FD::ParameterSet params); // // Constructor using input stream // IntegralLBPExtraction(std::istream &in) { readFrom(in); } virtual ~IntegralLBPExtraction(); // Default routine to print a IntegralLBPExtraction object to an output stream void printOn(std::ostream &out) const { throw new FD::GeneralException("Exception in IntegralLBPExtraction::printOn: method not yet implemented.",__FILE__,__LINE__); } // Default routine to read a IntegralLBPExtraction object from an input stream void readFrom(std::istream &in) { throw new FD::GeneralException("Exception in IntegralLBPExtraction::readFrom: method not yet implemented.",__FILE__,__LINE__); } virtual void request(int output_id, const FD::ParameterSet &req); void calculate(int output_id, int count, FD::Buffer &out); void Preprocess(IplImage *i_src); void ExtractFeatures(VisualROI *i_roi); void ExtractFeatures(IplImage *i_input, VisualROI *i_roi); VisualFeatureDesc<double> *GetDescriptor() { return (*m_featVect)[0]; } const VisualFeatureDesc<double> *GetCstDescriptor() const { return (const VisualIntegralDesc<double> *)(*m_featVect)[0]; } private: void Initialize(); void InitSamplePoints(); void ExtractLBPGeneralWithInterpolation(unsigned char *i_pixPtr); void ExtractLBPGeneralWithoutInterpolation(unsigned char *i_pixPtr); void ExtractLBPGeneralRIU2WithInterpolation(unsigned char *i_pixPtr); void ExtractLBPGeneralRIU2WithoutInterpolation(unsigned char *i_pixPtr); void ExtractLBP8WithInterpolation(unsigned char *i_pixPtr); void ExtractLBP8WithoutInterpolation(unsigned char *i_pixPtr); void ExtractLBP8RIU2WithInterpolation(unsigned char *i_pixPtr); void ExtractLBP8RIU2WithoutInterpolation(unsigned char *i_pixPtr); int ComputeBitTransitions(unsigned int i_val); int CountOneBits(unsigned int i_val); private: // Input IDs (for BufferedNode) int m_imageInID; int m_roiInID; int m_useNextImgInID; // Output IDs (for BufferedNode) int m_featuresOutID; int m_ppCompletedOutID; // Width of images int m_width; int m_imgSumWidth; // 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; // Number of independant rectangular region to compute // the integral color features int m_numHoriIntRect; int m_numVertIntRect; int m_numIntRect; // The number of samples in the local neighborhood. int m_numSamples; // The current predicate (radius), i.e. the distance of the // neighborhood from its center. int m_predicate; // Interpolation flag bool m_doInterpolation; // Flag to use only uniform patterns i.e. patterns with 2 or less // bit transitions bool m_useUniform; // The angle of the first neighbor. int m_startAngle; // Maximum pixel channel value double m_maxValue; // Number of valid local binary patterns int m_numValidPattern; // Precalculated table of interpolation points. CvPoint *m_samplePoints; // Precalculated table of interpolation offsets. CvPoint2D32f *m_pointsOffsets; // Precalculated values for interpolation multiplication. double *m_BiLiMultipliers; // Temporary pixel pointers corresponding to the neighborhood samples unsigned char **m_tmpSamples; // Integral color descriptor for region of interest FD::RCPtr<FD::Vector<VisualFeatureDesc<double> *> > m_featVect; // Temporary image copy IplImage *m_curImage; // Grayscale version of current image IplImage *m_grayImage; // Each local pattern is an image IplImage **m_patternImage; // Pointer to channel image pixels unsigned char **m_patternPixPtr; int **m_sumPixPtr; // Sum of pixels (integral) image IplImage **m_sumImage; // Function pointer to the appropriate private extraction routine void (IntegralLBPExtraction::*m_extractionFct)(unsigned char *); }; } #endif --- NEW FILE: VisualIntegralDesc.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 _VISUALINTEGRALCOLORDESC_H_ #define _VISUALINTEGRALCOLORDESC_H_ #include <math.h> #include "VisualFeatureDesc.h" namespace RobotFlow { typedef enum { e_VISUALINTDESC_EuclideanDist = 0, // TODO: maybe add other similarity methods e_VISUALINTDESC_UnknownSimilarity } e_VISUALINTDESC_similarityType; // // Descriptor for integral image features // A typical integral image feature consists of a mean value // for a given region of interest in each channel of the image // template <class FeatType> class VisualIntegralDesc : public VisualFeatureDesc<FeatType> { public: VisualIntegralDesc() : VisualFeatureDesc<FeatType>(e_VISUALDESCRIPTOR_integral), m_simType(e_VISUALINTDESC_UnknownSimilarity), m_numClrChannels(0), m_numIntValues(0), m_featSize(0), m_valid(false) { SetSimilarityFct(); } VisualIntegralDesc(e_VISUALINTDESC_similarityType i_simType, unsigned int i_numClrChannels, unsigned int i_numIntValues, FeatType i_maxValue) : VisualFeatureDesc<FeatType>(e_VISUALDESCRIPTOR_integral), m_simType(i_simType), m_numClrChannels(i_numClrChannels), m_numIntValues(i_numIntValues), m_maxValue(i_maxValue), m_valid(true) { m_featSize = m_numClrChannels*m_numIntValues; m_intFeatures = new FeatType[m_featSize]; SetSimilarityFct(); } VisualIntegralDesc(const VisualIntegralDesc<FeatType> &i_ref) { try { this->SetType(i_ref.GetType()); m_simType = i_ref.m_simType; m_numClrChannels = i_ref.m_numClrChannels; m_numIntValues = i_ref.m_numIntValues; m_featSize = i_ref.m_featSize; m_maxValue = i_ref.m_maxValue; m_valid = i_ref.m_valid; m_intFeatures = new FeatType[m_featSize]; for (int i=0; i<m_featSize; i++) { m_intFeatures[i] = i_ref.m_intFeatures[i]; } SetSimilarityFct(); } catch (FD::BaseException *e) { throw e->add(new FD::GeneralException("Exception caught in VisualIntegralDesc::VisualIntegralDesc:",__FILE__,__LINE__)); } } ~VisualIntegralDesc() { delete [] m_intFeatures; } VisualIntegralDesc<FeatType> & operator =(const VisualIntegralDesc<FeatType> &i_ref) { // Avoid self assignment if (&i_ref == this) { return *this; } this->SetType(i_ref.GetType()); m_simType = i_ref.m_simType; m_numClrChannels = i_ref.m_numClrChannels; m_numIntValues = i_ref.m_numIntValues; m_featSize = i_ref.m_featSize; m_maxValue = i_ref.m_maxValue; m_valid = i_ref.m_valid; m_intFeatures = new FeatType[m_featSize]; for (int i=0; i<m_featSize; i++) { m_intFeatures[i] = i_ref.m_intFeatures[i]; } SetSimilarityFct(); return *this; } VisualIntegralDesc<FeatType>* clone() const { return new VisualIntegralDesc<FeatType>(*this); } // Default routine to print a VisualIntegralDesc object to an output stream void printOn(std::ostream &out) const { throw new FD::GeneralException("Exception in VisualIntegralDesc::printOn: cannot use base class routine.",__FILE__,__LINE__); } // Default routine to read a VisualIntegralDesc object from an input stream void readFrom(std::istream &in) { throw new FD::GeneralException("Exception in VisualIntegralDesc::readFrom: cannot use base class routine.",__FILE__,__LINE__); } double Similarity(const FeatType *i_candidate, unsigned int i_size) const { try { if (!i_candidate) { throw new FD::GeneralException("VisualIntegralDesc::Similarity: invalid (NULL) candidate features.",__FILE__,__LINE__); } if (i_size != m_featSize) { throw new FD::GeneralException("VisualIntegralDesc::Similarity: candidate features size differs from current features descriptor size.",__FILE__,__LINE__); } if (!m_similarityFct) { throw new FD::GeneralException("VisualIntegralDesc::Similarity: invalid or unknown similarity type.",__FILE__,__LINE__); } if (!m_valid) { // Invalid descriptor, output maximal distance return 0.0; } // Use appropriate function return (this->*m_similarityFct)(i_candidate); } catch (FD::BaseException *e) { throw e->add(new FD::GeneralException("Exception caught in VisualIntegralDesc::Similarity:",__FILE__,__LINE__)); } } void Adapt(const FeatType *i_candidate, unsigned int i_size, double i_rate) { try { if (!i_candidate) { throw new FD::GeneralException("VisualIntegralDesc::Adapt: invalid (NULL) candidate features.",__FILE__,__LINE__); } if (i_size != m_featSize) { throw new FD::GeneralException("VisualIntegralDesc::Adapt: candidate features size differs from current features descriptor size.",__FILE__,__LINE__); } if (i_rate < 0.0 || i_rate > 1.0) { throw new FD::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; } int i; const FeatType *p_adaptFeat = i_candidate; double compRate = 1.0 - i_rate; for (i=0; i<m_featSize; i++) { m_intFeatures[i] = (FeatType)(compRate*(double)(m_intFeatures[i]) + i_rate*(double)(p_adaptFeat[i])); } } catch (FD::BaseException *e) { throw e->add(new FD::GeneralException("Exception caught in VisualIntegralDesc::Adapt:",__FILE__,__LINE__)); } } unsigned int GetSize() const { return m_featSize; } FeatType *GetFeatures() { return m_intFeatures; } const FeatType *GetCstFeatures() const { return (const FeatType *)m_intFeatures; } bool GetValidity() const { return m_valid; } void SetSize(unsigned int i_size) { throw new FD::GeneralException("Exception in VisualIntegralDesc::SetSize: cannot use base class routine.",__FILE__,__LINE__); } void SetFeatures(const FeatType *i_ref, unsigned int i_size) { try { if (!i_ref) { throw new FD::GeneralException("VisualIntegralDesc::SetFeatures: invalid (NULL) input features.",__FILE__,__LINE__); } if (i_size != m_featSize) { throw new FD::GeneralException("VisualIntegralDesc::SetFeatures: candidate features size differs from current features descriptor size.",__FILE__,__LINE__); } int i; const FeatType *p_inFeat = i_ref; for (i=0; i<m_featSize; i++) { m_intFeatures[i] = *p_inFeat++; } } catch (FD::BaseException *e) { throw e->add(new FD::GeneralException("Exception caught in VisualIntegralDesc::SetFeatures:",__FILE__,__LINE__)); } } void SetValidity(bool i_flag) { m_valid = i_flag; } private: void SetSimilarityFct() { if (m_simType == e_VISUALINTDESC_EuclideanDist) { m_similarityFct = &VisualIntegralDesc::EuclideanDist; } else { m_similarityFct = NULL; } } double EuclideanDist(const FeatType *i_candidate) const { FeatType diff; double dist = 0.0; const FeatType *p_curFeat = (const FeatType *)m_intFeatures; const FeatType *p_candFeat = i_candidate; for (int i=0; i<m_numIntValues; i++) { double clrDist = 0.0; for (int c=0; c<m_numClrChannels; c++) { diff = ((*p_curFeat) - (*p_candFeat))/m_maxValue; clrDist += (double)(diff*diff); p_curFeat++; p_candFeat++; } dist += sqrt(clrDist); } dist /= m_numIntValues; if (dist > 1.0) { std::cout << "VisualIntegralDesc::EuclideanDist: Invalid dist=" << dist << std::endl; throw new FD::GeneralException("VisualIntegralDesc::EuclideanDist: distance should not be greater than 1.0.",__FILE__,__LINE__); } return 1.0-dist; } private: // Similarity/Distance type to use e_VISUALINTDESC_similarityType m_simType; // Number of color channels of the color space used unsigned int m_numClrChannels; // Number of integral rectangle values/features unsigned int m_numIntValues; // The size is m_numIntValues*m_numClrChannels unsigned int m_featSize; // Maximum channel value FeatType m_maxValue; // Validity flag in cases where ROI was invalid for features extraction bool m_valid; // Integral features FeatType *m_intFeatures; // Function pointer to the appropriate private similarity routine double (VisualIntegralDesc::*m_similarityFct)(const FeatType *) const; }; } #endif --- NEW FILE: IntegralColorExtraction.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 _INTEGRALCOLOREXTRACTION_H_ #define _INTEGRALCOLOREXTRACTION_H_ #include "VisualFeaturesExtraction.h" #include "VisualIntegralDesc.h" #include "VisualTarget.h" #include <stdlib.h> #include <sys/timeb.h> namespace RobotFlow { // // Integral Color Features Extraction for RobotFlow // class IntegralColorExtraction : public VisualFeaturesExtraction<double> { public: // // Default constructor for Object // IntegralColorExtraction(); // // Constructor with complete intialisation // IntegralColorExtraction(int i_width, int i_height, int i_numChannels, int i_numHoriIntRect, int i_numVertIntRect, double i_maxValue); // // BufferedNode constructor // IntegralColorExtraction(std::string nodeName, FD::ParameterSet params); // // Constructor using input stream // IntegralColorExtraction(std::istream &in) { readFrom(in); } virtual ~IntegralColorExtraction(); // Default routine to print a IntegralColorExtraction object to an output stream void printOn(std::ostream &out) const { throw new FD::GeneralException("Exception in IntegralColorExtraction::printOn: method not yet implemented.",__FILE__,__LINE__); } // Default routine to read a IntegralColorExtraction object from an input stream void readFrom(std::istream &in) { throw new FD::GeneralException("Exception in IntegralColorExtraction::readFrom: method not yet implemented.",__FILE__,__LINE__); } virtual void request(int output_id, const FD::ParameterSet &req); void calculate(int output_id, int count, FD::Buffer &out); void Preprocess(const unsigned char *i_src); void ExtractFeatures(VisualROI *i_roi); void ExtractFeatures(IplImage *i_input, VisualROI *i_roi); VisualFeatureDesc<double> *GetDescriptor() { return (*m_featVect)[0]; } const VisualFeatureDesc<double> *GetCstDescriptor() const { return (const VisualIntegralDesc<double> *)(*m_featVect)[0]; } private: void Initialize(); private: // Input IDs (for BufferedNode) int m_imageInID; int m_roiInID; int m_useNextImgInID; // Output IDs (for BufferedNode) int m_featuresOutID; int m_ppCompletedOutID; // Width of images int m_width; int m_imgSumWidth; // 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; // Number of independant rectangular region to compute // the integral color features int m_numHoriIntRect; int m_numVertIntRect; int m_numIntRect; // Maximum pixel channel value double m_maxValue; // Integral color descriptor for region of interest FD::RCPtr<FD::Vector<VisualFeatureDesc<double> *> > m_featVect; // Temporary image copy IplImage *m_curImage; // Each channel image IplImage **m_chImage; // Pointer to channel image pixels unsigned char **m_chPixPtr; int **m_sumPixPtr; // Sum of pixels (integral) image IplImage **m_sumImage; }; } #endif --- NEW FILE: IntegralEdgesOriExtraction.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 _INTEGRALEDGESORIEXTRACTION_H_ #define _INTEGRALEDGESORIEXTRACTION_H_ #include "VisualFeaturesExtraction.h" #include "VisualIntegralDesc.h" #include "VisualTarget.h" #include <stdlib.h> #include <sys/timeb.h> namespace RobotFlow { // // Integral Edges Orientation Features Extraction for RobotFlow // class IntegralEdgesOriExtraction : public VisualFeaturesExtraction<double> { public: // // Default constructor for Object // IntegralEdgesOriExtraction(); // // Constructor with complete intialisation // IntegralEdgesOriExtraction(int i_width, int i_height, int i_numChannels, int i_numOriBins, double i_edgesStrTresh, double i_maxStrengthValue); // // BufferedNode constructor // IntegralEdgesOriExtraction(std::string nodeName, FD::ParameterSet params); // // Constructor using input stream // IntegralEdgesOriExtraction(std::istream &in) { readFrom(in); } virtual ~IntegralEdgesOriExtraction(); // Default routine to print a IntegralEdgesOriExtraction object to an output stream void printOn(std::ostream &out) const { throw new FD::GeneralException("Exception in IntegralEdgesOriExtraction::printOn: method not yet implemented.",__FILE__,__LINE__); } // Default routine to read a IntegralEdgesOriExtraction object from an input stream void readFrom(std::istream &in) { throw new FD::GeneralException("Exception in IntegralEdgesOriExtraction::readFrom: method not yet implemented.",__FILE__,__LINE__); } virtual void request(int output_id, const FD::ParameterSet &req); void calculate(int output_id, int count, FD::Buffer &out); void Preprocess(IplImage *i_srcImg); void ExtractFeatures(VisualROI *i_roi); void ExtractFeatures(IplImage *i_input, VisualROI *i_roi); VisualFeatureDesc<double> *GetDescriptor() { return (*m_featVect)[0]; } const VisualFeatureDesc<double> *GetCstDescriptor() const { return (const VisualIntegralDesc<double> *)(*m_featVect)[0]; } private: void Initialize(); private: // Input IDs (for BufferedNode) int m_imageInID; int m_roiInID; int m_useNextImgInID; // Output IDs (for BufferedNode) int m_featuresOutID; int m_ppCompletedOutID; // Width of images int m_width; int m_imgSumWidth; // 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; // Number of orientations to use int m_numOriBins; // Number of independant rectangular region to compute // the integral edges orientation features int m_numHoriIntRect; int m_numVertIntRect; int m_numIntRect; // Edge strength threshold to remove noisy edges double m_edgesStrTresh; // Maximum strength channel value double m_maxStrengthValue; // Integral color descriptor for region of interest FD::RCPtr<FD::Vector<VisualFeatureDesc<double> *> > m_featVect; // Temporary image copy IplImage *m_curImage; // Grayscale version of current image IplImage *m_grayImage; // Result from filtering With Sobel (X) IplImage *m_oriXImage; // Result from filtering With Sobel (Y) IplImage *m_oriYImage; // Multi-channel edges orientation map IplImage **m_edgesOri; // Multi-channel sum of edges orientation map IplImage **m_edgesOriSum; // Pixel/value pointers float **m_edgesOriPix; double **m_edgesOriSumPix; }; } #endif |