SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LBPPyrDotFeatures.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2010 Vojtech Franc, Soeren Sonnenburg
8  * Copyright (C) 2010 Vojtech Franc, xfrancv@cmp.felk.cvut.cz
9  * Copyright (C) 2010 Berlin Institute of Technology
10  */
12 
13 using namespace shogun;
14 
15 #define LIBLBP_INDEX(ROW,COL,NUM_ROWS) ((COL)*(NUM_ROWS)+(ROW))
16 
17 //void CLBPPyrDotFeatures::liblbp_pyr_features(char *vec)
18 //{
19 // uint32_t offset, ww, hh, x, y,center,j ;
20 // uint8_t pattern;
21 //
22 // offset=0;
25 // ww=img_nCols;
26 // hh=img_nRows;
27 // while(1)
28 // {
29 // for(x=1; x < ww-1; x++)
30 // {
31 // for(y=1; y< hh-1; y++)
32 // {
33 // pattern = 0;
34 // center = img[LIBLBP_INDEX(y,x,img_nRows)];
35 // if(img[LIBLBP_INDEX(y-1,x-1,img_nRows)] < center) pattern = pattern | 0x01;
36 // if(img[LIBLBP_INDEX(y-1,x,img_nRows)] < center) pattern = pattern | 0x02;
37 // if(img[LIBLBP_INDEX(y-1,x+1,img_nRows)] < center) pattern = pattern | 0x04;
38 // if(img[LIBLBP_INDEX(y,x-1,img_nRows)] < center) pattern = pattern | 0x08;
39 // if(img[LIBLBP_INDEX(y,x+1,img_nRows)] < center) pattern = pattern | 0x10;
40 // if(img[LIBLBP_INDEX(y+1,x-1,img_nRows)] < center) pattern = pattern | 0x20;
41 // if(img[LIBLBP_INDEX(y+1,x,img_nRows)] < center) pattern = pattern | 0x40;
42 // if(img[LIBLBP_INDEX(y+1,x+1,img_nRows)] < center) pattern = pattern | 0x80;
43 //
44 // vec[offset+pattern]++;
45 // offset += 256;
46 //
47 // }
48 // }
49 // if(vec_nDim <= offset)
50 // return;
51 //
52 // if(ww % 2 == 1) ww--;
53 // if(hh % 2 == 1) hh--;
54 //
55 // ww = ww/2;
56 // for(x=0; x < ww; x++)
57 // for(j=0; j < hh; j++)
58 // img[LIBLBP_INDEX(j,x,img_nRows)] = img[LIBLBP_INDEX(j,2*x,img_nRows)] +
59 // img[LIBLBP_INDEX(j,2*x+1,img_nRows)];
60 //
61 // hh = hh/2;
62 // for(y=0; y < hh; y++)
63 // for(j=0; j < ww; j++)
64 // img[LIBLBP_INDEX(y,j,img_nRows)] = img[LIBLBP_INDEX(2*y,j,img_nRows)] +
65 // img[LIBLBP_INDEX(2*y+1,j,img_nRows)];
66 //
67 // }
68 //
69 // return;
70 //}
71 
72 //void CLBPPyrDotFeatures::liblbp_pyr_subvec(int64_t *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols)
73 //{
74 // uint32_t offset, ww, hh, x, y,center,j ;
75 // uint8_t pattern;
76 //
77 // offset=0;
80 // ww=img_nCols;
81 // hh=img_nRows;
82 // while(1)
83 // {
84 // for(x=1; x < ww-1; x++)
85 // {
86 // for(y=1; y< hh-1; y++)
87 // {
88 // pattern = 0;
89 // center = img[LIBLBP_INDEX(y,x,img_nRows)];
90 // if(img[LIBLBP_INDEX(y-1,x-1,img_nRows)] < center) pattern = pattern | 0x01;
91 // if(img[LIBLBP_INDEX(y-1,x,img_nRows)] < center) pattern = pattern | 0x02;
92 // if(img[LIBLBP_INDEX(y-1,x+1,img_nRows)] < center) pattern = pattern | 0x04;
93 // if(img[LIBLBP_INDEX(y,x-1,img_nRows)] < center) pattern = pattern | 0x08;
94 // if(img[LIBLBP_INDEX(y,x+1,img_nRows)] < center) pattern = pattern | 0x10;
95 // if(img[LIBLBP_INDEX(y+1,x-1,img_nRows)] < center) pattern = pattern | 0x20;
96 // if(img[LIBLBP_INDEX(y+1,x,img_nRows)] < center) pattern = pattern | 0x40;
97 // if(img[LIBLBP_INDEX(y+1,x+1,img_nRows)] < center) pattern = pattern | 0x80;
98 //
99 // vec[offset+pattern]--;
100 // offset += 256;
101 //
102 // }
103 // }
104 // if(vec_nDim <= offset)
105 // return;
106 //
107 // if(ww % 2 == 1) ww--;
108 // if(hh % 2 == 1) hh--;
109 //
110 // ww = ww/2;
111 // for(x=0; x < ww; x++)
112 // for(j=0; j < hh; j++)
113 // img[LIBLBP_INDEX(j,x,img_nRows)] = img[LIBLBP_INDEX(j,2*x,img_nRows)] +
114 // img[LIBLBP_INDEX(j,2*x+1,img_nRows)];
115 //
116 // hh = hh/2;
117 // for(y=0; y < hh; y++)
118 // for(j=0; j < ww; j++)
119 // img[LIBLBP_INDEX(y,j,img_nRows)] = img[LIBLBP_INDEX(2*y,j,img_nRows)] +
120 // img[LIBLBP_INDEX(2*y+1,j,img_nRows)];
121 //
122 // }
123 //
124 // return;
125 //}
126 
128 {
129  SG_UNSTABLE("CLBPPyrDotFeatures::CLBPPyrDotFeatures()", "\n");
130 
131  m_feat = NULL;
132 
133  img = NULL;
134  img_nRows = 0;
135  img_nCols = 0;
136  vec_nDim = 0;
137 }
138 
140  : CDotFeatures()
141 {
142  ASSERT(images);
143 
144  m_feat = images;
145  SG_REF(m_feat);
147  vec_nDim=liblbp_pyr_get_dim(num_pyramids);
148 }
149 
151 {
152  SG_UNREF(m_feat);
153 }
154 
156 {
157  SG_PRINT("CLBPPyrDotFeatures:\n");
159 };
160 
162 {
163  return vec_nDim;
164 }
165 
167 {
168  return vec_nDim;
169 }
170 
172 {
173  return F_UNKNOWN;
174 }
175 
177 {
178  return C_POLY;
179 }
180 
182 {
183  if (m_feat)
184  return m_feat->get_num_vectors();
185  else
186  return 0;
187 
188 }
189 
191 {
192  return sizeof(float64_t);
193 }
194 
195 void* CLBPPyrDotFeatures::get_feature_iterator(int32_t vector_index)
196 {
198  return NULL;
199 }
200 
201 bool CLBPPyrDotFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator)
202 {
204  return NULL;
205 }
206 
208 {
210 }
211 
212 float64_t CLBPPyrDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2)
213 {
215  return 0;
216 }
217 
218 float64_t CLBPPyrDotFeatures::dense_dot(int32_t vec_idx1, const float64_t* vec2, int32_t vec2_len)
219 {
220  if (vec2_len != vec_nDim)
221  SG_ERROR("Dimensions don't match, vec2_dim=%d, vec_nDim=%d\n", vec2_len, vec_nDim);
222 
223  //int32_t vlen;
224  //bool do_free;
225  //uint32_t* vec=m_feat->get_feature_vector(i, vlen, vfree);
226 
227  //double CLBPPyrDotFeatures::liblbp_pyr_dotprod(double *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols)
228  //{
229  double dot_prod = 0;
230  int32_t offset=0;
231  int32_t ww, hh, x, y, j;
232  uint32_t center;
233  uint8_t pattern;
234 
235  /* ww=win_W;*/
236  /* hh=win_H;*/
237  ww=img_nCols;
238  hh=img_nRows;
239  while(1)
240  {
241  for(x=1; x < ww-1; x++)
242  {
243  for(y=1; y< hh-1; y++)
244  {
245  pattern = 0;
246  center = img[LIBLBP_INDEX(y,x,img_nRows)];
247  if (img[LIBLBP_INDEX(y-1,x-1,img_nRows)] < center) pattern |= 0x01;
248  if (img[LIBLBP_INDEX(y-1,x,img_nRows)] < center) pattern |= 0x02;
249  if (img[LIBLBP_INDEX(y-1,x+1,img_nRows)] < center) pattern |= 0x04;
250  if (img[LIBLBP_INDEX(y,x-1,img_nRows)] < center) pattern |= 0x08;
251  if (img[LIBLBP_INDEX(y,x+1,img_nRows)] < center) pattern |= 0x10;
252  if (img[LIBLBP_INDEX(y+1,x-1,img_nRows)] < center) pattern |= 0x20;
253  if (img[LIBLBP_INDEX(y+1,x,img_nRows)] < center) pattern |= 0x40;
254  if (img[LIBLBP_INDEX(y+1,x+1,img_nRows)] < center) pattern |= 0x80;
255 
256  dot_prod += vec2[offset+pattern];
257  offset += 256;
258 
259 
260  }
261  }
262  if(vec_nDim <= offset)
263  return(dot_prod);
264 
265 
266  if(ww % 2 == 1) ww--;
267  if(hh % 2 == 1) hh--;
268 
269  ww = ww/2;
270  for(x=0; x < ww; x++)
271  for(j=0; j < hh; j++)
273  img[LIBLBP_INDEX(j,2*x+1,img_nRows)];
274 
275  hh = hh/2;
276  for(y=0; y < hh; y++)
277  for(j=0; j < ww; j++)
278  img[LIBLBP_INDEX(y,j,img_nRows)] = img[LIBLBP_INDEX(2*y,j,img_nRows)] +
279  img[LIBLBP_INDEX(2*y+1,j,img_nRows)];
280  }
281 
282  //m_feat->free_feature_vector(vec, vlen, do_free);
283  return dot_prod;
284 }
285 
286 void CLBPPyrDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val)
287 {
288  if (vec2_len != vec_nDim)
289  SG_ERROR("Dimensions don't match, vec2_dim=%d, vec_nDim=%d\n", vec2_len, vec_nDim);
290 
291  //int32_t vlen;
292  //bool do_free;
293  //uint32_t* vec=m_feat->get_feature_vector(i, vlen, vfree);
294 
295 
296 //void CLBPPyrDotFeatures::liblbp_pyr_addvec(int64_t *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols)
297 //{
298  int32_t offset, ww, hh, x, y, j;
299  uint32_t center;
300  uint8_t pattern;
301 
302  offset=0;
303 /* ww=win_W;*/
304 /* hh=win_H;*/
305  ww=img_nCols;
306  hh=img_nRows;
307  while(1)
308  {
309  for(x=1; x < ww-1; x++)
310  {
311  for(y=1; y< hh-1; y++)
312  {
313  pattern = 0;
314  center = img[LIBLBP_INDEX(y,x,img_nRows)];
315  if(img[LIBLBP_INDEX(y-1,x-1,img_nRows)] < center) pattern |= 0x01;
316  if(img[LIBLBP_INDEX(y-1,x,img_nRows)] < center) pattern |= 0x02;
317  if(img[LIBLBP_INDEX(y-1,x+1,img_nRows)] < center) pattern |= 0x04;
318  if(img[LIBLBP_INDEX(y,x-1,img_nRows)] < center) pattern |= 0x08;
319  if(img[LIBLBP_INDEX(y,x+1,img_nRows)] < center) pattern |= 0x10;
320  if(img[LIBLBP_INDEX(y+1,x-1,img_nRows)] < center) pattern |= 0x20;
321  if(img[LIBLBP_INDEX(y+1,x,img_nRows)] < center) pattern |= 0x40;
322  if(img[LIBLBP_INDEX(y+1,x+1,img_nRows)] < center) pattern |= 0x80;
323 
324  vec2[offset+pattern]+=alpha;
325  offset += 256;
326 
327  }
328  }
329  if(vec_nDim <= offset)
330  return;
331 
332  if(ww % 2 == 1) ww--;
333  if(hh % 2 == 1) hh--;
334 
335  ww = ww/2;
336  for(x=0; x < ww; x++)
337  for(j=0; j < hh; j++)
339  img[LIBLBP_INDEX(j,2*x+1,img_nRows)];
340 
341  hh = hh/2;
342  for(y=0; y < hh; y++)
343  for(j=0; j < ww; j++)
344  img[LIBLBP_INDEX(y,j,img_nRows)] = img[LIBLBP_INDEX(2*y,j,img_nRows)] +
345  img[LIBLBP_INDEX(2*y+1,j,img_nRows)];
346 
347  }
348 
349  // return;
350 //}
351 
352  //m_feat->free_feature_vector(vec, vlen, do_free);
353 }
354 
356 {
357  return new CLBPPyrDotFeatures(*this);
358 }
359 
360 uint32_t CLBPPyrDotFeatures::liblbp_pyr_get_dim(uint16_t nPyramids)
361 {
362  uint32_t w, h, N, i;
363 
364  for(w=img_nCols, h=img_nRows, N=0, i=0; i < nPyramids && CMath::min(w,h) >= 3; i++)
365  {
366  N += (w-2)*(h-2);
367 
368  if(w % 2) w--;
369  if(h % 2) h--;
370  w = w/2;
371  h = h/2;
372  }
373  return(256*N);
374 }

SHOGUN Machine Learning Toolbox - Documentation