SHOGUN v0.9.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 "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(void) 00128 : CDotFeatures() 00129 { 00130 SG_UNSTABLE("CLBPPyrDotFeatures::CLBPPyrDotFeatures(void)", "\n"); 00131 00132 m_feat = NULL; 00133 00134 img = NULL; 00135 img_nRows = 0; 00136 img_nCols = 0; 00137 vec_nDim = 0; 00138 } 00139 00140 CLBPPyrDotFeatures::CLBPPyrDotFeatures(CSimpleFeatures<uint32_t>* images, uint16_t num_pyramids) 00141 : CDotFeatures() 00142 { 00143 ASSERT(images); 00144 00145 m_feat = images; 00146 SG_REF(m_feat); 00147 img=m_feat->get_feature_matrix(img_nRows, img_nCols); 00148 vec_nDim=liblbp_pyr_get_dim(num_pyramids); 00149 } 00150 00151 CLBPPyrDotFeatures::~CLBPPyrDotFeatures() 00152 { 00153 SG_UNREF(m_feat); 00154 } 00155 00156 float64_t CLBPPyrDotFeatures::dot(int32_t vec_idx1, CDotFeatures* df, int32_t vec_idx2) 00157 { 00158 SG_NOTIMPLEMENTED; 00159 return 0; 00160 } 00161 00162 float64_t CLBPPyrDotFeatures::dense_dot(int32_t vec_idx1, const float64_t* vec2, int32_t vec2_len) 00163 { 00164 if (vec2_len != vec_nDim) 00165 SG_ERROR("Dimensions don't match, vec2_dim=%d, vec_nDim=%d\n", vec2_len, vec_nDim); 00166 00167 //int32_t vlen; 00168 //bool do_free; 00169 //uint32_t* vec=m_feat->get_feature_vector(i, vlen, vfree); 00170 00171 //double CLBPPyrDotFeatures::liblbp_pyr_dotprod(double *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols) 00172 //{ 00173 double dot_prod = 0; 00174 int32_t offset=0; 00175 int32_t ww, hh, x, y, j; 00176 uint32_t center; 00177 uint8_t pattern; 00178 00179 /* ww=win_W;*/ 00180 /* hh=win_H;*/ 00181 ww=img_nCols; 00182 hh=img_nRows; 00183 while(1) 00184 { 00185 for(x=1; x < ww-1; x++) 00186 { 00187 for(y=1; y< hh-1; y++) 00188 { 00189 pattern = 0; 00190 center = img[LIBLBP_INDEX(y,x,img_nRows)]; 00191 if (img[LIBLBP_INDEX(y-1,x-1,img_nRows)] < center) pattern |= 0x01; 00192 if (img[LIBLBP_INDEX(y-1,x,img_nRows)] < center) pattern |= 0x02; 00193 if (img[LIBLBP_INDEX(y-1,x+1,img_nRows)] < center) pattern |= 0x04; 00194 if (img[LIBLBP_INDEX(y,x-1,img_nRows)] < center) pattern |= 0x08; 00195 if (img[LIBLBP_INDEX(y,x+1,img_nRows)] < center) pattern |= 0x10; 00196 if (img[LIBLBP_INDEX(y+1,x-1,img_nRows)] < center) pattern |= 0x20; 00197 if (img[LIBLBP_INDEX(y+1,x,img_nRows)] < center) pattern |= 0x40; 00198 if (img[LIBLBP_INDEX(y+1,x+1,img_nRows)] < center) pattern |= 0x80; 00199 00200 dot_prod += vec2[offset+pattern]; 00201 offset += 256; 00202 00203 00204 } 00205 } 00206 if(vec_nDim <= offset) 00207 return(dot_prod); 00208 00209 00210 if(ww % 2 == 1) ww--; 00211 if(hh % 2 == 1) hh--; 00212 00213 ww = ww/2; 00214 for(x=0; x < ww; x++) 00215 for(j=0; j < hh; j++) 00216 img[LIBLBP_INDEX(j,x,img_nRows)] = img[LIBLBP_INDEX(j,2*x,img_nRows)] + 00217 img[LIBLBP_INDEX(j,2*x+1,img_nRows)]; 00218 00219 hh = hh/2; 00220 for(y=0; y < hh; y++) 00221 for(j=0; j < ww; j++) 00222 img[LIBLBP_INDEX(y,j,img_nRows)] = img[LIBLBP_INDEX(2*y,j,img_nRows)] + 00223 img[LIBLBP_INDEX(2*y+1,j,img_nRows)]; 00224 } 00225 00226 //m_feat->free_feature_vector(vec, vlen, do_free); 00227 return dot_prod; 00228 } 00229 00230 void CLBPPyrDotFeatures::add_to_dense_vec(float64_t alpha, int32_t vec_idx1, float64_t* vec2, int32_t vec2_len, bool abs_val) 00231 { 00232 if (vec2_len != vec_nDim) 00233 SG_ERROR("Dimensions don't match, vec2_dim=%d, vec_nDim=%d\n", vec2_len, vec_nDim); 00234 00235 //int32_t vlen; 00236 //bool do_free; 00237 //uint32_t* vec=m_feat->get_feature_vector(i, vlen, vfree); 00238 00239 00240 //void CLBPPyrDotFeatures::liblbp_pyr_addvec(int64_t *vec, uint32_t vec_nDim, uint32_t *img, uint16_t img_nRows, uint16_t img_nCols) 00241 //{ 00242 int32_t offset, ww, hh, x, y, j; 00243 uint32_t center; 00244 uint8_t pattern; 00245 00246 offset=0; 00247 /* ww=win_W;*/ 00248 /* hh=win_H;*/ 00249 ww=img_nCols; 00250 hh=img_nRows; 00251 while(1) 00252 { 00253 for(x=1; x < ww-1; x++) 00254 { 00255 for(y=1; y< hh-1; y++) 00256 { 00257 pattern = 0; 00258 center = img[LIBLBP_INDEX(y,x,img_nRows)]; 00259 if(img[LIBLBP_INDEX(y-1,x-1,img_nRows)] < center) pattern |= 0x01; 00260 if(img[LIBLBP_INDEX(y-1,x,img_nRows)] < center) pattern |= 0x02; 00261 if(img[LIBLBP_INDEX(y-1,x+1,img_nRows)] < center) pattern |= 0x04; 00262 if(img[LIBLBP_INDEX(y,x-1,img_nRows)] < center) pattern |= 0x08; 00263 if(img[LIBLBP_INDEX(y,x+1,img_nRows)] < center) pattern |= 0x10; 00264 if(img[LIBLBP_INDEX(y+1,x-1,img_nRows)] < center) pattern |= 0x20; 00265 if(img[LIBLBP_INDEX(y+1,x,img_nRows)] < center) pattern |= 0x40; 00266 if(img[LIBLBP_INDEX(y+1,x+1,img_nRows)] < center) pattern |= 0x80; 00267 00268 vec2[offset+pattern]+=alpha; 00269 offset += 256; 00270 00271 } 00272 } 00273 if(vec_nDim <= offset) 00274 return; 00275 00276 if(ww % 2 == 1) ww--; 00277 if(hh % 2 == 1) hh--; 00278 00279 ww = ww/2; 00280 for(x=0; x < ww; x++) 00281 for(j=0; j < hh; j++) 00282 img[LIBLBP_INDEX(j,x,img_nRows)] = img[LIBLBP_INDEX(j,2*x,img_nRows)] + 00283 img[LIBLBP_INDEX(j,2*x+1,img_nRows)]; 00284 00285 hh = hh/2; 00286 for(y=0; y < hh; y++) 00287 for(j=0; j < ww; j++) 00288 img[LIBLBP_INDEX(y,j,img_nRows)] = img[LIBLBP_INDEX(2*y,j,img_nRows)] + 00289 img[LIBLBP_INDEX(2*y+1,j,img_nRows)]; 00290 00291 } 00292 00293 // return; 00294 //} 00295 00296 //m_feat->free_feature_vector(vec, vlen, do_free); 00297 } 00298 00299 CFeatures* CLBPPyrDotFeatures::duplicate() const 00300 { 00301 return new CLBPPyrDotFeatures(*this); 00302 } 00303 00304 uint32_t CLBPPyrDotFeatures::liblbp_pyr_get_dim(uint16_t nPyramids) 00305 { 00306 uint32_t w, h, N, i; 00307 00308 for(w=img_nCols, h=img_nRows, N=0, i=0; i < nPyramids && CMath::min(w,h) >= 3; i++) 00309 { 00310 N += (w-2)*(h-2); 00311 00312 if(w % 2) w--; 00313 if(h % 2) h--; 00314 w = w/2; 00315 h = h/2; 00316 } 00317 return(256*N); 00318 }