SHOGUN
v2.0.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2010 Vojtech Franc, Soeren Sonnenburg 00008 * Copyright (C) 2010 Vojtech Franc, xfrancv@cmp.felk.cvut.cz 00009 * Copyright (C) 2010 Berlin Institute of Technology 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 //void CLBPPyrDotFeatures::liblbp_pyr_features(char *vec) 00018 //{ 00019 // uint32_t offset, ww, hh, x, y,center,j ; 00020 // uint8_t pattern; 00021 // 00022 // offset=0; 00025 // ww=img_nCols; 00026 // hh=img_nRows; 00027 // while(1) 00028 // { 00029 // for(x=1; x < ww-1; x++) 00030 // { 00031 // for(y=1; y< hh-1; y++) 00032 // { 00033 // pattern = 0; 00034 // center = img[LIBLBP_INDEX(y,x,img_nRows)]; 00035 // if(img[LIBLBP_INDEX(y-1,x-1,img_nRows)] < center) pattern = pattern | 0x01; 00036 // if(img[LIBLBP_INDEX(y-1,x,img_nRows)] < center) pattern = pattern | 0x02; 00037 // if(img[LIBLBP_INDEX(y-1,x+1,img_nRows)] < center) pattern = pattern | 0x04; 00038 // if(img[LIBLBP_INDEX(y,x-1,img_nRows)] < center) pattern = pattern | 0x08; 00039 // if(img[LIBLBP_INDEX(y,x+1,img_nRows)] < center) pattern = pattern | 0x10; 00040 // if(img[LIBLBP_INDEX(y+1,x-1,img_nRows)] < center) pattern = pattern | 0x20; 00041 // if(img[LIBLBP_INDEX(y+1,x,img_nRows)] < center) pattern = pattern | 0x40; 00042 // if(img[LIBLBP_INDEX(y+1,x+1,img_nRows)] < center) pattern = pattern | 0x80; 00043 // 00044 // vec[offset+pattern]++; 00045 // offset += 256; 00046 // 00047 // } 00048 // } 00049 // if(vec_nDim <= offset) 00050 // return; 00051 // 00052 // if(ww % 2 == 1) ww--; 00053 // if(hh % 2 == 1) hh--; 00054 // 00055 // ww = ww/2; 00056 // for(x=0; x < ww; x++) 00057 // for(j=0; j < hh; j++) 00058 // img[LIBLBP_INDEX(j,x,img_nRows)] = img[LIBLBP_INDEX(j,2*x,img_nRows)] + 00059 // img[LIBLBP_INDEX(j,2*x+1,img_nRows)]; 00060 // 00061 // hh = hh/2; 00062 // for(y=0; y < hh; y++) 00063 // for(j=0; j < ww; j++) 00064 // img[LIBLBP_INDEX(y,j,img_nRows)] = img[LIBLBP_INDEX(2*y,j,img_nRows)] + 00065 // img[LIBLBP_INDEX(2*y+1,j,img_nRows)]; 00066 // 00067 // } 00068 // 00069 // return; 00070 //} 00071 00072 //void CLBPPyrDotFeatures::liblbp_pyr_subvec(int64_t *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols) 00073 //{ 00074 // uint32_t offset, ww, hh, x, y,center,j ; 00075 // uint8_t pattern; 00076 // 00077 // offset=0; 00080 // ww=img_nCols; 00081 // hh=img_nRows; 00082 // while(1) 00083 // { 00084 // for(x=1; x < ww-1; x++) 00085 // { 00086 // for(y=1; y< hh-1; y++) 00087 // { 00088 // pattern = 0; 00089 // center = img[LIBLBP_INDEX(y,x,img_nRows)]; 00090 // if(img[LIBLBP_INDEX(y-1,x-1,img_nRows)] < center) pattern = pattern | 0x01; 00091 // if(img[LIBLBP_INDEX(y-1,x,img_nRows)] < center) pattern = pattern | 0x02; 00092 // if(img[LIBLBP_INDEX(y-1,x+1,img_nRows)] < center) pattern = pattern | 0x04; 00093 // if(img[LIBLBP_INDEX(y,x-1,img_nRows)] < center) pattern = pattern | 0x08; 00094 // if(img[LIBLBP_INDEX(y,x+1,img_nRows)] < center) pattern = pattern | 0x10; 00095 // if(img[LIBLBP_INDEX(y+1,x-1,img_nRows)] < center) pattern = pattern | 0x20; 00096 // if(img[LIBLBP_INDEX(y+1,x,img_nRows)] < center) pattern = pattern | 0x40; 00097 // if(img[LIBLBP_INDEX(y+1,x+1,img_nRows)] < center) pattern = pattern | 0x80; 00098 // 00099 // vec[offset+pattern]--; 00100 // offset += 256; 00101 // 00102 // } 00103 // } 00104 // if(vec_nDim <= offset) 00105 // return; 00106 // 00107 // if(ww % 2 == 1) ww--; 00108 // if(hh % 2 == 1) hh--; 00109 // 00110 // ww = ww/2; 00111 // for(x=0; x < ww; x++) 00112 // for(j=0; j < hh; j++) 00113 // img[LIBLBP_INDEX(j,x,img_nRows)] = img[LIBLBP_INDEX(j,2*x,img_nRows)] + 00114 // img[LIBLBP_INDEX(j,2*x+1,img_nRows)]; 00115 // 00116 // hh = hh/2; 00117 // for(y=0; y < hh; y++) 00118 // for(j=0; j < ww; j++) 00119 // img[LIBLBP_INDEX(y,j,img_nRows)] = img[LIBLBP_INDEX(2*y,j,img_nRows)] + 00120 // img[LIBLBP_INDEX(2*y+1,j,img_nRows)]; 00121 // 00122 // } 00123 // 00124 // return; 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, 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 //int32_t vlen; 00224 //bool do_free; 00225 //uint32_t* vec=m_feat->get_feature_vector(i, vlen, vfree); 00226 00227 //double CLBPPyrDotFeatures::liblbp_pyr_dotprod(double *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols) 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 /* ww=win_W;*/ 00236 /* hh=win_H;*/ 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 //m_feat->free_feature_vector(vec, vlen, do_free); 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 //int32_t vlen; 00292 //bool do_free; 00293 //uint32_t* vec=m_feat->get_feature_vector(i, vlen, vfree); 00294 00295 00296 //void CLBPPyrDotFeatures::liblbp_pyr_addvec(int64_t *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols) 00297 //{ 00298 int32_t offset, ww, hh, x, y, j; 00299 uint32_t center; 00300 uint8_t pattern; 00301 00302 offset=0; 00303 /* ww=win_W;*/ 00304 /* hh=win_H;*/ 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 // return; 00350 //} 00351 00352 //m_feat->free_feature_vector(vec, vlen, do_free); 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 }