X-Boost  2.3.8
SoftCascade.h
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 _CASCADE_CLASSIFIER_H
22 #define _CASCADE_CLASSIFIER_H
23 
28 template<class WeakClassifier >
31 struct SoftCascadeStage : public BoostableClassifier< WeakClassifier > {
32 public:
33 
36 
37 public:
38 
40  float rejection;
41 
42 public:
43 
44  SoftCascadeStage() : rejection(0.0f) { }
45 
47  template<class R, class P1, class P2>
48  SoftCascadeStage(bool dummy, const SoftCascadeStage<R> & src, P1 p1, P2 p2) : BoostableClassifier< WeakClassifier >( src, p1, p2), rejection (src.rejection)
49  {
50  }
51 
52  template<class R, class P1, class P2, class P3>
53  SoftCascadeStage(const SoftCascadeStage<R> & src, P1 scale, P2 offset, P3 stride) : BoostableClassifier< WeakClassifier >( src, scale, offset, stride), rejection (src.rejection)
54  {
55  }
56 
57  SoftCascadeStage(const WeakClassifier & weak, float a, float r) : BoostableClassifier<WeakClassifier>(weak, a), rejection(r) { }
58 
59  static std::string signature() {
60  return "softcascade-" + BoostableClassifier<WeakClassifier>::signature();
61  }
62 };
63 
64 // IO operators
65 
66 template<class T>
67 std::istream & operator >> (std::istream & in, SoftCascadeStage<T> & s);
68 
69 template<class T>
70 std::ostream & operator << (std::ostream & out, const SoftCascadeStage<T> & s);
71 
75 template<class T>
77 
78 public:
79 
81  typedef T WeakClassifier;
85  typedef std::vector< Classifier > ClassifierList;
86 
87 public:
88 
91 
92 public:
93 
96 
97 public:
98 
100  template<class R>
101  SoftCascadeClassifier(const SoftCascadeClassifier<R> & src, int scale, int offset, long stride)
102  {
104  {
105  m_weak_classifiers.push_back( SoftCascadeStage< T > (*i, scale, offset, stride) );
106  }
107  }
108 
110  template<class R, class Param1Type, class Param2Type>
111  SoftCascadeClassifier(const SoftCascadeClassifier<R> & src, Param1Type p1, const Param2Type & p2)
112  {
114  {
115  m_weak_classifiers.push_back( SoftCascadeStage< T > (true, *i, p1, p2) );
116  }
117  }
118 
122  SoftCascadeClassifier(std::istream & in);
125 
127  inline unsigned int length() const {
128  return m_weak_classifiers.size();
129  }
130 
131  inline void insert(const WeakClassifier & weak, float alpha, float reject)
132  {
133  m_weak_classifiers.push_back( Classifier(weak, alpha, reject) );
134  }
135 
137  static std::string signature() {
138  return Classifier::signature();
139  }
140 
142 inline ClassifierList & list() { return m_weak_classifiers; }
143 inline const ClassifierList & list() const { return m_weak_classifiers; }
144 
146  template<class TParam1>
147  static float compute(const TParam1 pIntImage, const Classifier *classifier, int n);
148 
150  template<class TParam1, class TParam2>
151  static float compute(const TParam1 pIntImage, const TParam2 stride, const Classifier *classifier, int n);
152 
153  template<class FeatureType>
154  void export_features(std::vector<FeatureType> & out) const
155  {
156  for(typename ClassifierList::const_iterator i = m_weak_classifiers.begin(); i != m_weak_classifiers.end(); i++)
157  out.push_back(*i);
158  }
159 
163  template<class TParam1, class TParam2>
164  float raw(const TParam1 data, const TParam2 p1) const
165  {
166  float acc = 0.0;
167  for(typename ClassifierList::const_iterator i = m_weak_classifiers.begin(); i != m_weak_classifiers.end(); i++)
168  {
169  acc += (*i)(data, p1);
170  }
171  return acc;
172  }
173 
176  template<class TParam1>
177  inline float operator()(const TParam1 pIntImage) const {
178  return compute(pIntImage, &m_weak_classifiers[0], m_weak_classifiers.size());
179  }
180 
183  template<class TParam1, class TParam2>
184  inline float operator()(const TParam1 pIntImage, const TParam2 stride) const {
185  return compute(pIntImage, stride, &m_weak_classifiers[0], m_weak_classifiers.size());
186  }
187 };
188 
189 template<class T>
190 std::istream & operator >>(std::istream & in, SoftCascadeClassifier<T> & dst);
191 
192 template<class T>
193 std::ostream & operator << (std::ostream & out, const SoftCascadeClassifier<T> & src);
194 
196 
197 template<class T>
198 inline std::istream & operator >> (std::istream & in, SoftCascadeStage<T> & s)
199 {
200  in >> s.rejection >> static_cast< BoostableClassifier<T> &>(s);
201  return in;
202 }
203 
204 template<class T>
205 inline std::ostream & operator << (std::ostream & out, const SoftCascadeStage<T> & s)
206 {
207  out << s.rejection << ' ' << static_cast<const BoostableClassifier<T> &>(s);
208  return out;
209 }
210 
211 // IO
212 template<class T>
213 std::istream & operator >>(std::istream & in, SoftCascadeClassifier<T> & dst)
214 {
215  while(in)
216  {
218  in >> h;
219  if(in)
220  {
221  dst.m_weak_classifiers.push_back(h);
222  }
223  }
224  return in;
225 }
226 
227 // IO
228 template<class T>
229 std::ostream & operator << (std::ostream & out, const SoftCascadeClassifier<T> & src)
230 {
231  for(typename SoftCascadeClassifier<T>::ClassifierList::const_iterator i = src.m_weak_classifiers.begin(); i != src.m_weak_classifiers.end(); i++)
232  out << *i << '\n';
233  return out;
234 }
235 
236 
237 template<class T>
239 
240 template<class T>
242 
243 template<class T>
245 {
246  while(in)
247  {
249  in >> h;
250  if(in)
251  {
252  // std::cout << h << std::endl;
253  m_weak_classifiers.push_back(h);
254  }
255  }
256 }
257 
258 template<class T>
259 template<class TParam1, class TParam2>
260 float SoftCascadeClassifier<T>::compute(const TParam1 pIntImage, const TParam2 stride, const Classifier *bc, int n)
261 {
262  float acc = 0.0f;
263  for(int i = 0; i<n; i++)
264  {
265  acc += bc[i](pIntImage, stride);
266  if(acc < bc[i].rejection) // reject now
267  return -1.0f;
268  }
269  return acc;
270 }
271 
272 template<class T>
273 template<class TParam1>
274 float SoftCascadeClassifier<T>::compute(const TParam1 pIntImage, const Classifier *bc, int n)
275 {
276  float acc = 0.0f;
277  for(int i = 0; i<n; i++)
278  {
279  acc += bc[i](pIntImage);
280  if(acc < bc[i].rejection) // reject now
281  return -1.0f;
282  }
283  return acc;
284 }
285 
286 #endif
Definition: SoftCascade.h:31
SoftCascadeClassifier(const SoftCascadeClassifier< R > &src, int scale, int offset, long stride)
Create and convert a SoftCascade.
Definition: SoftCascade.h:101
SoftCascadeStage< T > Classifier
A softcascadeboostable classifier is a weak classifier with a weight associated and a rejection thres...
Definition: SoftCascade.h:83
std::vector< Classifier > ClassifierList
List of BoostSoftCascade classifier.
Definition: SoftCascade.h:85
~SoftCascadeClassifier()
dtor
Definition: SoftCascade.h:241
SoftCascadeStage< typename WeakClassifier::OptimizedType > OptimizedType
the optimized version of the SoftCascadeStage classifier
Definition: SoftCascade.h:35
float operator()(const TParam1 pIntImage) const
Definition: SoftCascade.h:177
ClassifierList m_weak_classifiers
A che collection of SoftCascadeStage weak classifier.
Definition: SoftCascade.h:95
static std::string signature()
propagate signature
Definition: SoftCascade.h:137
SoftCascadeClassifier()
ctor
Definition: SoftCascade.h:238
SoftCascadeClassifier< typename T::OptimizedType > OptimizedType
the optimized version of the SoftCascadeStage classifier
Definition: SoftCascade.h:90
ClassifierList & list()
Return the inner list of classifier.
Definition: SoftCascade.h:142
float rejection
Definition: SoftCascade.h:40
static float compute(const TParam1 pIntImage, const Classifier *classifier, int n)
the real computer
Definition: SoftCascade.h:274
Definition: SoftCascade.h:76
unsigned int length() const
Return the number of classifiers.
Definition: SoftCascade.h:127
SoftCascadeClassifier(const SoftCascadeClassifier< R > &src, Param1Type p1, const Param2Type &p2)
Create and convert a SoftCascade.
Definition: SoftCascade.h:111
T WeakClassifier
the inner weak classifier
Definition: SoftCascade.h:81
float raw(const TParam1 data, const TParam2 p1) const
Definition: SoftCascade.h:164
Definition: BoostableClassifier.h:40
float operator()(const TParam1 pIntImage, const TParam2 stride) const
Definition: SoftCascade.h:184
SoftCascadeStage(bool dummy, const SoftCascadeStage< R > &src, P1 p1, P2 p2)
Definition: SoftCascade.h:48