00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include <shogun/features/LBPPyrDotFeatures.h>
00012
00013 using namespace shogun;
00014
00015 #define LIBLBP_INDEX(ROW,COL,NUM_ROWS) ((COL)*(NUM_ROWS)+(ROW))
00016
00017
00018
00019
00020
00021
00022
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127 CLBPPyrDotFeatures::CLBPPyrDotFeatures() : CDotFeatures()
00128 {
00129 SG_UNSTABLE("CLBPPyrDotFeatures::CLBPPyrDotFeatures()", "\n");
00130
00131 m_feat = NULL;
00132
00133 img = NULL;
00134 img_nRows = 0;
00135 img_nCols = 0;
00136 vec_nDim = 0;
00137 }
00138
00139 CLBPPyrDotFeatures::CLBPPyrDotFeatures(CDenseFeatures<uint32_t>* images, uint16_t num_pyramids)
00140 : CDotFeatures()
00141 {
00142 ASSERT(images);
00143
00144 m_feat = images;
00145 SG_REF(m_feat);
00146 img=m_feat->get_feature_matrix(img_nRows, img_nCols);
00147 vec_nDim=liblbp_pyr_get_dim(num_pyramids);
00148 }
00149
00150 CLBPPyrDotFeatures::~CLBPPyrDotFeatures()
00151 {
00152 SG_UNREF(m_feat);
00153 }
00154
00155 CLBPPyrDotFeatures::CLBPPyrDotFeatures(const CLBPPyrDotFeatures & orig)
00156 {
00157 SG_PRINT("CLBPPyrDotFeatures:\n");
00158 SG_NOTIMPLEMENTED;
00159 };
00160
00161 int32_t CLBPPyrDotFeatures::get_dim_feature_space() const
00162 {
00163 return vec_nDim;
00164 }
00165
00166 int32_t CLBPPyrDotFeatures::get_nnz_features_for_vector(int32_t num)
00167 {
00168 return vec_nDim;
00169 }
00170
00171 EFeatureType CLBPPyrDotFeatures::get_feature_type() const
00172 {
00173 return F_UNKNOWN;
00174 }
00175
00176 EFeatureClass CLBPPyrDotFeatures::get_feature_class() const
00177 {
00178 return C_POLY;
00179 }
00180
00181 int32_t CLBPPyrDotFeatures::get_num_vectors() const
00182 {
00183 if (m_feat)
00184 return m_feat->get_num_vectors();
00185 else
00186 return 0;
00187
00188 }
00189
00190 int32_t CLBPPyrDotFeatures::get_size() const
00191 {
00192 return sizeof(float64_t);
00193 }
00194
00195 void* CLBPPyrDotFeatures::get_feature_iterator(int32_t vector_index)
00196 {
00197 SG_NOTIMPLEMENTED;
00198 return NULL;
00199 }
00200
00201 bool CLBPPyrDotFeatures::get_next_feature(int32_t& index, float64_t& value, void* iterator)
00202 {
00203 SG_NOTIMPLEMENTED;
00204 return NULL;
00205 }
00206
00207 void CLBPPyrDotFeatures::free_feature_iterator(void* iterator)
00208 {
00209 SG_NOTIMPLEMENTED;
00210 }
00211
00212 float64_t CLBPPyrDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2)
00213 {
00214 SG_NOTIMPLEMENTED;
00215 return 0;
00216 }
00217
00218 float64_t CLBPPyrDotFeatures::dense_dot(int32_t vec_idx1, const float64_t* vec2, int32_t vec2_len)
00219 {
00220 if (vec2_len != vec_nDim)
00221 SG_ERROR("Dimensions don't match, vec2_dim=%d, vec_nDim=%d\n", vec2_len, vec_nDim);
00222
00223
00224
00225
00226
00227
00228
00229 double dot_prod = 0;
00230 int32_t offset=0;
00231 int32_t ww, hh, x, y, j;
00232 uint32_t center;
00233 uint8_t pattern;
00234
00235
00236
00237 ww=img_nCols;
00238 hh=img_nRows;
00239 while(1)
00240 {
00241 for(x=1; x < ww-1; x++)
00242 {
00243 for(y=1; y< hh-1; y++)
00244 {
00245 pattern = 0;
00246 center = img[LIBLBP_INDEX(y,x,img_nRows)];
00247 if (img[LIBLBP_INDEX(y-1,x-1,img_nRows)] < center) pattern |= 0x01;
00248 if (img[LIBLBP_INDEX(y-1,x,img_nRows)] < center) pattern |= 0x02;
00249 if (img[LIBLBP_INDEX(y-1,x+1,img_nRows)] < center) pattern |= 0x04;
00250 if (img[LIBLBP_INDEX(y,x-1,img_nRows)] < center) pattern |= 0x08;
00251 if (img[LIBLBP_INDEX(y,x+1,img_nRows)] < center) pattern |= 0x10;
00252 if (img[LIBLBP_INDEX(y+1,x-1,img_nRows)] < center) pattern |= 0x20;
00253 if (img[LIBLBP_INDEX(y+1,x,img_nRows)] < center) pattern |= 0x40;
00254 if (img[LIBLBP_INDEX(y+1,x+1,img_nRows)] < center) pattern |= 0x80;
00255
00256 dot_prod += vec2[offset+pattern];
00257 offset += 256;
00258
00259
00260 }
00261 }
00262 if(vec_nDim <= offset)
00263 return(dot_prod);
00264
00265
00266 if(ww % 2 == 1) ww--;
00267 if(hh % 2 == 1) hh--;
00268
00269 ww = ww/2;
00270 for(x=0; x < ww; x++)
00271 for(j=0; j < hh; j++)
00272 img[LIBLBP_INDEX(j,x,img_nRows)] = img[LIBLBP_INDEX(j,2*x,img_nRows)] +
00273 img[LIBLBP_INDEX(j,2*x+1,img_nRows)];
00274
00275 hh = hh/2;
00276 for(y=0; y < hh; y++)
00277 for(j=0; j < ww; j++)
00278 img[LIBLBP_INDEX(y,j,img_nRows)] = img[LIBLBP_INDEX(2*y,j,img_nRows)] +
00279 img[LIBLBP_INDEX(2*y+1,j,img_nRows)];
00280 }
00281
00282
00283 return dot_prod;
00284 }
00285
00286 void CLBPPyrDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val)
00287 {
00288 if (vec2_len != vec_nDim)
00289 SG_ERROR("Dimensions don't match, vec2_dim=%d, vec_nDim=%d\n", vec2_len, vec_nDim);
00290
00291
00292
00293
00294
00295
00296
00297
00298 int32_t offset, ww, hh, x, y, j;
00299 uint32_t center;
00300 uint8_t pattern;
00301
00302 offset=0;
00303
00304
00305 ww=img_nCols;
00306 hh=img_nRows;
00307 while(1)
00308 {
00309 for(x=1; x < ww-1; x++)
00310 {
00311 for(y=1; y< hh-1; y++)
00312 {
00313 pattern = 0;
00314 center = img[LIBLBP_INDEX(y,x,img_nRows)];
00315 if(img[LIBLBP_INDEX(y-1,x-1,img_nRows)] < center) pattern |= 0x01;
00316 if(img[LIBLBP_INDEX(y-1,x,img_nRows)] < center) pattern |= 0x02;
00317 if(img[LIBLBP_INDEX(y-1,x+1,img_nRows)] < center) pattern |= 0x04;
00318 if(img[LIBLBP_INDEX(y,x-1,img_nRows)] < center) pattern |= 0x08;
00319 if(img[LIBLBP_INDEX(y,x+1,img_nRows)] < center) pattern |= 0x10;
00320 if(img[LIBLBP_INDEX(y+1,x-1,img_nRows)] < center) pattern |= 0x20;
00321 if(img[LIBLBP_INDEX(y+1,x,img_nRows)] < center) pattern |= 0x40;
00322 if(img[LIBLBP_INDEX(y+1,x+1,img_nRows)] < center) pattern |= 0x80;
00323
00324 vec2[offset+pattern]+=alpha;
00325 offset += 256;
00326
00327 }
00328 }
00329 if(vec_nDim <= offset)
00330 return;
00331
00332 if(ww % 2 == 1) ww--;
00333 if(hh % 2 == 1) hh--;
00334
00335 ww = ww/2;
00336 for(x=0; x < ww; x++)
00337 for(j=0; j < hh; j++)
00338 img[LIBLBP_INDEX(j,x,img_nRows)] = img[LIBLBP_INDEX(j,2*x,img_nRows)] +
00339 img[LIBLBP_INDEX(j,2*x+1,img_nRows)];
00340
00341 hh = hh/2;
00342 for(y=0; y < hh; y++)
00343 for(j=0; j < ww; j++)
00344 img[LIBLBP_INDEX(y,j,img_nRows)] = img[LIBLBP_INDEX(2*y,j,img_nRows)] +
00345 img[LIBLBP_INDEX(2*y+1,j,img_nRows)];
00346
00347 }
00348
00349
00350
00351
00352
00353 }
00354
00355 CFeatures* CLBPPyrDotFeatures::duplicate() const
00356 {
00357 return new CLBPPyrDotFeatures(*this);
00358 }
00359
00360 uint32_t CLBPPyrDotFeatures::liblbp_pyr_get_dim(uint16_t nPyramids)
00361 {
00362 uint32_t w, h, N, i;
00363
00364 for(w=img_nCols, h=img_nRows, N=0, i=0; i < nPyramids && CMath::min(w,h) >= 3; i++)
00365 {
00366 N += (w-2)*(h-2);
00367
00368 if(w % 2) w--;
00369 if(h % 2) h--;
00370 w = w/2;
00371 h = h/2;
00372 }
00373 return(256*N);
00374 }