X-Boost  2.3.8
BinaryClassifier.h
Go to the documentation of this file.
1 /* XBoost: Ada-Boost and Friends on Haar/ICF/HOG Features, Library and ToolBox
2  *
3  * Copyright (c) 2008-2014 Paolo Medici <medici@ce.unipr.it>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #ifndef _BINARY_CLASSIFIER_H
22 #define _BINARY_CLASSIFIER_H
23 
29 #include <iostream>
30 #include <vector>
31 #include <string>
32 #include "Types.h"
33 
37 template<class FeatureExtractor, class Policy>
38 struct BinaryClassifier : public FeatureExtractor, public Policy {
39 
40 public:
41 
42  typedef FeatureExtractor FeatureType;
43 
44  typedef Policy PolicyType;
45 
46  typedef typename Policy::ReturnType ReturnType;
47 
50 
52  static const ClassifierType Type = Policy::Type;
53 
54 public:
55 
57 
59  template<class R, class P1>
60  explicit BinaryClassifier(const R & x, P1 p1) : FeatureExtractor(x, p1), Policy(x) { }
62  template<class R, class P1, class P2>
63  explicit BinaryClassifier(const R & x, P1 p1, P2 p2) : FeatureExtractor(x, p1, p2), Policy(x) { }
65  template<class R, class P0, class P1, class P2>
66  explicit BinaryClassifier(const R & x, P0 p0, P1 p1, P2 p2) : FeatureExtractor(x, p0, p1, p2), Policy(x)
67  {
68  Policy::rescale(p0,p0,1);//scale th
69  }
70 
71  BinaryClassifier() { }
72  ~BinaryClassifier() { }
73 
75  const char *str() const {
76  return FeatureExtractor::name;
77  }
78 
80  static std::string signature() {
81  return Policy::signature() + "-" + FeatureExtractor::signature();
82  }
83 
89  template<class D1, class D2>
90  inline ReturnType classify(const D1 d1, const D2 d2) const
91  {
92  return Policy::evaluate_feature( FeatureExtractor::response(d1, d2) );
93  }
94 
97  template<class D1>
98  inline ReturnType classify(const D1 d1) const
99  {
100  return Policy::evaluate_feature( FeatureExtractor::response(d1) );
101  }
102 
104  template<class D1, class D2>
105  inline ReturnType operator()(const D1 d1, const D2 d2) const
106  {
107  return classify(d1, d2);
108  }
109 
111  template<class D1>
112  inline ReturnType operator()(const D1 d1) const
113  {
114  return classify(d1);
115  }
117  template<class D1, class D2>
118  void test_and_evalute(int & value, ReturnType & test, const D1 d1, const D2 d2) const
119  {
120  value = FeatureExtractor::response(d1, d2);
121  test = Policy::evaluate_feature(value);
122  }
123 
125  void rescale(float sx, float sy)
126  {
127  // TODO: rescale th
128  FeatureExtractor::rescale(sx,sy);
129  }
130 
131  template<class FeatureType>
132  void export_features(std::vector<FeatureType> & out) const
133  {
134  out.push_back( static_cast<const FeatureType&>(*this) );
135  }
136 
137 };
138 
139 // IO operators
140 
141 template<class FeatureType, class Policy>
142 std::istream & operator >> (std::istream & in, BinaryClassifier<FeatureType, Policy> & s);
143 
144 template<class FeatureType, class Policy>
145 std::ostream & operator << (std::ostream & out, const BinaryClassifier<FeatureType, Policy> & s);
146 
148 
149 
150 template<class FeatureType, class Policy>
151 std::istream & operator >> (std::istream & in, BinaryClassifier<FeatureType, Policy> & s)
152 {
153  in >> static_cast<Policy&>(s) >> static_cast<FeatureType&>(s);
154  return in;
155 }
156 
157 template<class FeatureType, class Policy>
158 std::ostream & operator << (std::ostream & out, const BinaryClassifier<FeatureType, Policy> & s)
159 {
160  out << static_cast<const Policy&>(s) << ' ' << static_cast<const FeatureType&>(s);
161  return out;
162 }
163 
164 
165 #endif
ReturnType operator()(const D1 d1, const D2 d2) const
classify wrapper.
Definition: BinaryClassifier.h:105
ReturnType classify(const D1 d1, const D2 d2) const
Definition: BinaryClassifier.h:90
BinaryClassifier< typename FeatureExtractor::OptimizedType, Policy > OptimizedType
the optimized type
Definition: BinaryClassifier.h:49
A classifier composed by a Feature Extractor and an Evaluation Policy A "Second Level" classifier...
Definition: BinaryClassifier.h:38
Types involved in boosting.
void rescale(float sx, float sy)
this function permits to change the geometry of haar feature: integral value should be used ...
Definition: BinaryClassifier.h:125
ClassifierType
Definition: Types.h:31
const char * str() const
return the "feature" name (for debug purpose)
Definition: BinaryClassifier.h:75
BinaryClassifier(const R &x, P1 p1)
Definition: BinaryClassifier.h:60
ReturnType classify(const D1 d1) const
Definition: BinaryClassifier.h:98
a FeatureExtractor return a scalar number without relationship with classification ...
Definition: Types.h:35
static const ClassifierType Type
BinaryClassifier inherits type from Policy.
Definition: BinaryClassifier.h:52
void test_and_evalute(int &value, ReturnType &test, const D1 d1, const D2 d2) const
return both feature responde and classify hypothesis
Definition: BinaryClassifier.h:118
static std::string signature()
signature name is the name of provider (for simplicity)
Definition: BinaryClassifier.h:80
BinaryClassifier(const R &x, P0 p0, P1 p1, P2 p2)
Definition: BinaryClassifier.h:66
ReturnType operator()(const D1 d1) const
classify wrapper.
Definition: BinaryClassifier.h:112
BinaryClassifier(const R &x, P1 p1, P2 p2)
Definition: BinaryClassifier.h:63