VLFeat.org

API docs

  • Home
    • Download and Install
    • API docs
    • Matlab docs
    • About VLFeat
  • Tutorials
    • SIFT
    • MSER
    • IKM
    • HIKM
    • AIB
    • Utils
  • Main Page
  • Related Pages
  • Data Structures
  • Files
  • Examples

ikmeans.c

Go to the documentation of this file.
00001 
00007 /* AUTORIGHTS
00008 Copyright 2007 (c) Andrea Vedaldi and Brian Fulkerson
00009 
00010 This file is part of VLFeat, available in the terms of the GNU
00011 General Public License version 2.
00012 */
00013 
00078 #include "ikmeans.h"
00079 
00080 #include <stdlib.h>
00081 #include <stdio.h>
00082 #include <string.h> /* memset */
00083 #include "assert.h"
00084 
00085 static void    vl_ikm_init_lloyd      (VlIKMFilt*) ;
00086 static void    vl_ikm_init_elkan      (VlIKMFilt*) ;
00087 static int     vl_ikm_train_lloyd     (VlIKMFilt*, vl_uint8 const*, int) ;
00088 static int     vl_ikm_train_elkan     (VlIKMFilt*, vl_uint8 const*, int) ;
00089 static void    vl_ikm_push_lloyd      (VlIKMFilt*, vl_uint*, vl_uint8 const*, int) ;
00090 static void    vl_ikm_push_elkan      (VlIKMFilt*, vl_uint*, vl_uint8 const*, int) ;
00091 
00104 VlIKMFilt *
00105 vl_ikm_new (int method)
00106 {
00107   VlIKMFilt *f =  vl_malloc (sizeof(VlIKMFilt)) ;
00108   f -> centers = 0 ;
00109   f -> inter_dist = 0 ;
00110 
00111   f -> M = 0 ;
00112   f -> K = 0 ;
00113   f -> method     = method ;
00114   f -> max_niters = 200 ;
00115   f -> verb       = 0 ;
00116 
00117   return f ;
00118 }
00119 
00125 void vl_ikm_delete (VlIKMFilt* f)
00126 {
00127   if (f) {
00128     if (f-> centers)    vl_free (f-> centers) ;
00129     if (f-> inter_dist) vl_free (f-> inter_dist) ;
00130     vl_free (f) ;
00131   }
00132 }
00133 
00143 VL_EXPORT
00144 int vl_ikm_train (VlIKMFilt *f, vl_uint8 const *data, int N)
00145 { 
00146   int err ;
00147   
00148   if (f-> verb) {
00149     VL_PRINTF ("ikm: training with %d data\n",  N) ;
00150     VL_PRINTF ("ikm: %d clusters\n",  f -> K) ;
00151   }
00152   
00153   switch (f -> method) {
00154   case VL_IKM_LLOYD : err = vl_ikm_train_lloyd (f, data, N) ; break ;
00155   case VL_IKM_ELKAN : err = vl_ikm_train_elkan (f, data, N) ; break ;
00156   default :
00157     assert (0) ;
00158   }
00159   return err ;
00160 }
00161 
00174 VL_EXPORT
00175 void
00176 vl_ikm_push (VlIKMFilt *f, vl_uint *asgn, vl_uint8 const *data, int N) {
00177   switch (f -> method) {
00178   case VL_IKM_LLOYD : vl_ikm_push_lloyd (f, asgn, data, N) ; break ;
00179   case VL_IKM_ELKAN : vl_ikm_push_elkan (f, asgn, data, N) ; break ;
00180   default :
00181     assert (0) ;
00182   }
00183 }
00184 
00198 VL_EXPORT
00199 vl_uint
00200 vl_ikm_push_one (vl_ikm_acc const *centers, 
00201      vl_uint8 const *data, 
00202      int M, int K)
00203 {
00204   int i,k ;
00205   
00206   /* assign data to centers */
00207   vl_int32 best_dist = 0 ;
00208   vl_uint  best      = (vl_uint)-1 ;
00209   
00210   for(k = 0 ; k < K ; ++k) {
00211     vl_int32 dist = 0 ;
00212     
00213     /* compute distance with this center */
00214     for(i = 0 ; i < M ; ++i) {
00215       vl_int32 delta = data[i] - centers[k*M + i] ;
00216       dist += delta * delta ;
00217     }
00218     
00219     /* compare with current best */
00220     if (best == (vl_uint)-1 || dist < best_dist) {
00221       best = k  ;
00222       best_dist = dist ;
00223     }
00224   }
00225   return best;
00226 }
00227 
00228 #include "ikmeans_init.tc"
00229 #include "ikmeans_lloyd.tc"
00230 #include "ikmeans_elkan.tc"
Copyright © 2008 Andrea Vedaldi and Brian Fulkerson