00001 /*!@file Learn/LocalBinaryPatterns.H Multiclass LocalBinaryPatterns Classifier module */ 00002 // //////////////////////////////////////////////////////////////////// // 00003 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00004 // University of Southern California (USC) and the iLab at USC. // 00005 // See http://iLab.usc.edu for information about this project. // 00006 // //////////////////////////////////////////////////////////////////// // 00007 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00008 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00009 // in Visual Environments, and Applications'' by Christof Koch and // 00010 // Laurent Itti, California Institute of Technology, 2001 (patent // 00011 // pending; application number 09/912,225 filed July 23, 2001; see // 00012 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00013 // //////////////////////////////////////////////////////////////////// // 00014 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00015 // // 00016 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00017 // redistribute it and/or modify it under the terms of the GNU General // 00018 // Public License as published by the Free Software Foundation; either // 00019 // version 2 of the License, or (at your option) any later version. // 00020 // // 00021 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00022 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00023 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00024 // PURPOSE. See the GNU General Public License for more details. // 00025 // // 00026 // You should have received a copy of the GNU General Public License // 00027 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00028 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00029 // Boston, MA 02111-1307 USA. // 00030 // //////////////////////////////////////////////////////////////////// // 00031 // 00032 // Primary maintainer for this file: Dan Parks <danielfp@usc.edu> 00033 // $HeadURL$ 00034 // $Id$ 00035 // 00036 00037 #ifndef LOCALBINARYPATTERNS_H_DEFINED 00038 #define LOCALBINARYPATTERNS_H_DEFINED 00039 00040 #include <deque> 00041 #include <map> 00042 00043 #include "Image/Image.H" 00044 #include "Image/Pixels.H" 00045 00046 // ###################################################################### 00047 //! Multiclass LocalBinaryPatterns Class 00048 class LocalBinaryPatterns 00049 { 00050 00051 public: 00052 // Map of model histograms (multiple per model id) 00053 typedef std::map<int, std::vector<std::vector<float> > > MapModelVector; 00054 00055 //! Constructor 00056 LocalBinaryPatterns(int LBPRadii=1, int LBPPixels=8, int varBins=128, bool useColor=false, bool normalize=true); 00057 00058 //! Destructor 00059 ~LocalBinaryPatterns(); 00060 00061 //! Add histograms using color texture 00062 void addModel(const Image<PixRGB<byte> >& texture, const int id); 00063 00064 //! Add a LBP and VAR histogram explicitly as a model to the system 00065 void addModel(const std::vector<float>& lbp, const std::vector<float>&col, const std::vector<float>& var, const int id); 00066 00067 //! Return the final histogram 00068 std::vector<float> createHistogram(const Image<PixRGB<byte> >& texture); 00069 00070 //! Determine the LBP histogram and variance values 00071 void createRawHistogram(const Image<PixRGB<byte> >& colTexture, std::vector<float>& lbps, std::vector<float>& col, std::vector<float>& vars); 00072 00073 //! Sum the colors within the rectangle 00074 void colorSum(Image<PixRGB<byte> >img, Rectangle rec, float& rg, float& by); 00075 00076 //! Convert list of variances to a variance histogram 00077 std::vector<float> convertVariance(const std::vector<float>& vars); 00078 00079 //! Set the map of built models 00080 void setModels(const MapModelVector& models); 00081 00082 //! Return a map of the built models 00083 MapModelVector getModels(); 00084 00085 //! Return a separate data vector and label vector from the MapModelVector 00086 void getLabeledData(const MapModelVector& models, std::vector<std::vector<float> >& data, std::vector<float>& labels); 00087 00088 //! Merge two ordered vectors 00089 std::vector<float> merge(const std::vector<float>& left, const std::vector<float>& right); 00090 00091 //! Load the incomplete models from a combined format 00092 void setIncompleteModels(const MapModelVector& incompleteModels); 00093 00094 std::vector<float> getVarThresholds(); 00095 00096 void setVarThresholds(std::vector<float> thresholds); 00097 00098 void combineModels(const std::vector< MapModelVector >& allModels, MapModelVector& combined); 00099 00100 void appendMap(MapModelVector& dst, const MapModelVector& src); 00101 00102 //! Return the incomplete models in a combined format 00103 MapModelVector getIncompleteModels(); 00104 00105 //! After all model exemplars have been given, build final model histograms and set up variance bins 00106 void buildModels(); 00107 00108 //! Convert incomplete models to complete models 00109 void convertIncompleteModels(); 00110 00111 //! Predict using classifier 00112 int predict(const Image<float>& texture); 00113 00114 //! Get total number of model exemplars 00115 uint getTotalModelExemplars(MapModelVector models); 00116 00117 //! Read file of models into system 00118 MapModelVector readModelsFile(std::string modelsFile); 00119 00120 //! Write file of models out 00121 void writeModelsFile(std::string modelsFile, MapModelVector models); 00122 00123 // Read bin thresholds in 00124 std::vector<float> readThresholdsFile(std::string thresholdsFile); 00125 00126 // Write bin thresholds out 00127 void writeThresholdsFile(std::string thresholdsFile, std::vector<float> thresholds); 00128 00129 00130 protected: 00131 template<class T> int getVarIndex(const T&thresh, float var, int binHint=-1); 00132 00133 float itsLBPRadius; 00134 int itsLBPPixels; 00135 int itsVarBins; 00136 bool itsUseColor; 00137 bool itsNormalize; 00138 int itsColorBins; 00139 std::vector<float> itsVarBinThresholds; 00140 //! Store each model 00141 MapModelVector itsModels; 00142 //! Temporary LBP models (multiple per class) 00143 MapModelVector itsTempLBP; 00144 //! List of actual variances, kept until training is complete so that a full histogram can be built 00145 MapModelVector itsTempVariance; 00146 //! List of color histogram models 00147 MapModelVector itsTempColor; 00148 }; 00149 00150 #endif 00151 00152 00153 // ###################################################################### 00154 /* So things look consistent in everyone's emacs... */ 00155 /* Local Variables: */ 00156 /* indent-tabs-mode: nil */ 00157 /* End: */ 00158