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(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
00168
00169
00170
00171
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
00180
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
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
00236
00237
00238
00239
00240
00241
00242 int32_t offset, ww, hh, x, y, j;
00243 uint32_t center;
00244 uint8_t pattern;
00245
00246 offset=0;
00247
00248
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
00294
00295
00296
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 }