X-Boost  2.3.8
IntegralChannelImagePreprocessor.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 _INTEGRAL_CHANNEL_IMAGE_PREPROCS_H
22 #define _INTEGRAL_CHANNEL_IMAGE_PREPROCS_H
23 
29 #include "IntegralImage.h"
30 #include <string.h>
31 // #include <IO/pnmio.h>
32 // #include <stdio.h>
33 
34 
37  long stride1, stride2;
38  unsigned int width, height, nchannels;
39 
40  inline unsigned int Width() const {
41  return width;
42  }
43  inline unsigned int Height() const {
44  return height;
45  }
46  inline unsigned int NChannels() const {
47  return nchannels;
48  }
49 
50 
51 
53  bool operator ==(const IntegralChannelImageParams & b) const {
54  return (b.width == width) && (b.height == height) && (b.nchannels == nchannels);
55  }
56 
57 
58 };
59 
60 std::ostream& operator<< (std::ostream& out, const IntegralChannelImageParams& param);
61 std::istream& operator>> (std::istream& in, IntegralChannelImageParams& feat);
62 /*
63 
64 */
65 
66 #define ICF_GENERATE_ATAN2_TABLE
67 
71 private:
72 
74  bool m_useOrientationSign;
76  unsigned int m_nOrientationBin;
78  bool m_magnitudeChannel;
80  bool m_luminanceChannel;
82  int m_derivativeFilter; // 0: derivative, 1: sobel 3x3
83 
85  int nChannels;
86 
87  #ifdef ICF_GENERATE_ATAN2_TABLE
88  unsigned char *table_atan2_module;
89  #endif
90 
91 public:
92 
93 
96 
99 
101  typedef std::pair<DataType, ParamType> ReturnType;
102 
103 public:
104 
106 
108  IntegralChannelImagePreprocessor(std::istream & in);
109 
111  IntegralChannelImagePreprocessor(const char *conf);
112 
114 
115  void Configure(bool orientationSign, int nOrientationBin, bool magnitudeChannel, bool luminanceChannel,int derivativeFilter);
116 
118  void Configure(const char* str);
119  void Configure(std::istream & in);
120 
121  void GetConf(char* strconf) const
122  {
123  char strPconf[10];
124  strPconf[0] = m_nOrientationBin + 0x30;
125  int i=1;
126  if(m_useOrientationSign) strPconf[i++]='s';
127  if(m_magnitudeChannel) strPconf[i++]='m';
128  if(m_luminanceChannel) strPconf[i++]='l';
129  strPconf[i++]=0;
130  strcpy(strconf,strPconf);
131  }
132 
133  void GetParam(ParamType & params, unsigned int width, unsigned int height) const
134  {
135  params.width = width;
136  params.height = height;
137  params.nchannels = nChannels;
138  params.stride1 = width+1;
139  params.stride2 = (width+1)*(height);
140  }
141 
143  unsigned int DataSize(unsigned int width, unsigned int height) const
144  {
145  return sizeof(uint32_t) * (width + 1) * (height * nChannels + 1);
146  }
147 
148  void Serialize(unsigned char *buf, const IntegralImageData & src, int n) const
149  {
150  memcpy(buf, src.data, n);
151  }
152 
153  void DeSerialize(IntegralImageData & dst, const unsigned char *buf, int n) const
154  {
155  dst.data = new uint32_t[n/4];
156  memcpy(dst.data, buf, n);
157  }
158 
159  template<class T, bool magnitude, bool luminance>
160  bool core(unsigned char *buffer, const T *image, unsigned int width, unsigned int height, unsigned int j0, unsigned int j1, long stride) const
161  {
162  unsigned int channel_size = width * height;
163  const int magnitude_offset = m_nOrientationBin * channel_size;
164  const int luminance_offset = (m_nOrientationBin + (magnitude ? 1 : 0) ) * channel_size;
165 
166 #ifndef ICF_GENERATE_ATAN2_TABLE
167  static const float scale = 1.0f / std::sqrt(2.0f);
168  static const float norm_factor = (m_useOrientationSign) ? ((double)m_nOrientationBin/ (2.0f*M_PI)) : ((double)m_nOrientationBin/ (M_PI));
169 #endif
170  image += j0 * stride;
171  buffer += j0 * width;
172 
173  // 1..N Orientation
174  for(unsigned int j=j0; j<j1; ++j)
175  {
176  for(unsigned int i=1; i<width-1; ++i)
177  {
178  int dx,dy;
179 
180  // DERIVATIVE FILTER:
181  dx = (int)image[i + 1] - (int)image[i - 1];
182  dy = (int)image[i + stride] - (int)image[i - stride];
183 
184 #ifndef ICF_GENERATE_ATAN2_TABLE
185  unsigned char m = std::sqrt(dx*dx + dy*dy) *scale;
186  float p = std::atan2( (float) dy, (float) dx);
187  int bin = (int) ( (p + M_PI) * norm_factor);
188  bin = (bin) % m_nOrientationBin;
189 #else
190  unsigned char bin = table_atan2_module[((dx + 255) + (dy + 255) * 512)*2+0];
191  unsigned char m = table_atan2_module[((dx + 255) + (dy + 255) * 512)*2+1];
192 #endif
193 
194  buffer[bin * channel_size + i] = m;
195 
196  if(magnitude)
197  {
198  buffer[i + magnitude_offset] = m;
199  }
200  if(luminance)
201  {
202  buffer[i + luminance_offset] = image[i];
203  }
204 
205  }
206 
207  image += stride;
208  buffer += width;
209  }
210  return true;
211  }
212 
214  template<class T>
215  bool Process(std::pair<IntegralImageData, IntegralChannelImageParams> & out, const T *image, unsigned int width, unsigned int height, long stride) const
216  {
217  unsigned char *buffer = new unsigned char [width * height * nChannels];
218 
219  out.second.width = width;
220  out.second.height = height;
221  out.second.stride1 = width+1;
222  out.second.stride2 = (width+1)*(height);
223  out.second.nchannels = nChannels;
224 
225  // TODO:
226  memset(buffer,0,width*height*nChannels);
227 
228  // avoid using of if()
229  if(!m_magnitudeChannel && !m_luminanceChannel)
230  core<T, false, false>(buffer, image, width, height, 1, height-1, stride);
231  else
232  if(m_magnitudeChannel && !m_luminanceChannel)
233  core<T, true, false>(buffer, image, width, height, 1, height-1, stride);
234  else
235  if(!m_magnitudeChannel && m_luminanceChannel)
236  core<T, false, true>(buffer, image, width, height, 1, height-1, stride);
237  else
238  if(m_magnitudeChannel && m_luminanceChannel)
239  core<T, true, true>(buffer, image, width, height, 1, height-1, stride);
240 
241  IntegralImageHandle<uint32_t> integral_image;
242  integral_image.Build(buffer, width, height * nChannels, width, true);
243  delete [] buffer;
244 
245  out.first.data = integral_image.data;
246  return true;
247  }
248 };
249 
250 #endif
a descritor based on a integral image
std::pair< DataType, ParamType > ReturnType
Data provided by this preprocessor.
Definition: IntegralChannelImagePreprocessor.h:101
bool Process(std::pair< IntegralImageData, IntegralChannelImageParams > &out, const T *image, unsigned int width, unsigned int height, long stride) const
compute the ICH
Definition: IntegralChannelImagePreprocessor.h:215
IntegralImageData DataType
Data provided by this Preprocessor.
Definition: IntegralChannelImagePreprocessor.h:95
Definition: IntegralChannelImagePreprocessor.h:70
parameters required to address a channel params
Definition: IntegralChannelImagePreprocessor.h:36
unsigned int DataSize(unsigned int width, unsigned int height) const
compute memory required to serialize/deserialize the preprocessed image
Definition: IntegralChannelImagePreprocessor.h:143
il dato IntegralImage e' pressoche' equivalente a quello di RawData < uint32_t >
Definition: IntegralImageData.h:52
bool operator==(const IntegralChannelImageParams &b) const
to check if parameters are related
Definition: IntegralChannelImagePreprocessor.h:53
void Build(const _S *src, unsigned int width, unsigned int height, long stride, bool extend=false)
Build integral image from buffer.
Definition: IntegralImage.h:202
IntegralChannelImageParams ParamType
Common data provided by this preprocessor.
Definition: IntegralChannelImagePreprocessor.h:98