X-Boost  2.3.8
HaarFeature.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 _HAAR_FEATURE_H
22 #define _HAAR_FEATURE_H
23 
28 #include <vector>
29 #include <iostream>
30 
34 struct HaarNode {
35  int sign; // multiplier
36  int x,y; // posizion of feature
37 
38 public:
39 
40  HaarNode() { }
41  HaarNode(int _sign, int _x, int _y) : sign(_sign), x(_x), y(_y)
42  {
43  }
44 
45  inline int operator()(const unsigned int *pIntImage, long stride) const
46  {
47  return (int)(pIntImage[x + y * stride]) * sign;
48  }
49 };
50 
51 std::istream & operator >> (std::istream & in, HaarNode & d);
52 std::ostream & operator << (std::ostream & out, const HaarNode & s);
53 
54 typedef int (* Evaluate_t)(const unsigned int *pIntImage, int stride, const HaarNode *node);
55 
57 extern const Evaluate_t ievaluate[];
58 
61 
63 struct HaarFeature : public std::vector<HaarNode> {
64 
65  const char *name; // for debug purpose (remember to remove in final version)
66 
67 public:
68 
70  typedef unsigned int InputDataType;
71 
73  typedef int DescriptorType;
74 
77 
78  enum { InvokeParam = 2 };
79 
80 public:
81 
82  HaarFeature() : name(NULL) { }
83 
85  HaarFeature(const HaarFeature &src, int scale, long offset, long stride)
86  {
87  //rescale(scale,scale);
88  //std::cout << "allocate features @ scale "<<scale <<std::endl;
89 
90  this->resize(src.size());
91  std::vector<HaarNode>::iterator nodes=this->begin();
92  int j=0;
93  for(std::vector<HaarNode>::const_iterator i=src.begin();i!=src.end();++i)
94  {
95  int x = (int)(i->x * scale + scale - 1);
96  int y = (int)(i->y * scale + scale - 1);
97  nodes[j].sign=i->sign;
98  nodes[j].x = x;
99  nodes[j].y = y;
100  ++j;
101  }
102  }
103 
104 
105  inline const char *debug_name() const { return name; }
106  inline void debug_name(const char *str) { name = str; }
107 
109  static std::string signature() { return "haar"; }
110 
113 {
114  for(std::vector<HaarNode>::iterator i=this->begin();i!=this->end();i++)
115  i->sign = - i->sign;
116 }
117 
118 #if 0
119  int response(const unsigned int *pIntImage, int stride) const
120  {
121  for(std::vector<HaarNode> i=m_nodes.begin();i!=m_nodes.end();i++)
122  sum += node[i](pIntImage, stride);
123  }
124 #else
125  inline int response(const unsigned int *pIntImage, int stride) const
128  {
129  return ievaluate[size()](pIntImage, stride, &(*this)[0]);
130  }
131 #endif
132 
133  inline int operator() (const unsigned int *pIntImage, int stride) const
134  {
135  return response(pIntImage, stride);
136  }
137 
141  void rescale(int sx, int sy)
142  {
143  for(std::vector<HaarNode>::iterator i = this->begin(); i!= this->end(); ++i)
144  i->x = (i->x * sx + sx - 1), i->y = (i->y * sy + sy - 1);
145  }
146 };
147 
148 
149  std::istream & operator >> (std::istream & in, HaarFeature & s);
150  std::ostream & operator << (std::ostream & out, const HaarFeature & s);
151 
152 #endif
HaarFeature(const HaarFeature &src, int scale, long offset, long stride)
Definition: HaarFeature.h:85
void invert_polarity()
invert the sign of haar nodes (in order to simplify the decision stump)
Definition: HaarFeature.h:112
image/size TODO namespace
Definition: Types.h:39
CollapsedHaarFeature OptimizedType
the feature used during the application in fast app
Definition: HaarFeature.h:76
int DescriptorType
type of data returned by this feature extractor
Definition: HaarFeature.h:73
an optimized HaarFeature
Definition: CollapsedHaarFeature.h:50
void rescale(int sx, int sy)
Definition: HaarFeature.h:141
static std::string signature()
Definition: HaarFeature.h:109
int response(const unsigned int *pIntImage, int stride) const
Definition: HaarFeature.h:127
An haar Feature: a collection of weighted HaarNode.
Definition: HaarFeature.h:63
Definition: HaarFeature.h:34
unsigned int InputDataType
type of data requested by this Feature Extractor
Definition: HaarFeature.h:70