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_init.tc

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 
00014 #include "random.h"
00015 
00016 /* pairs are used to generate random permutations of data */
00017 typedef struct
00018 {
00019   vl_int32 w;
00020   vl_int32 j;
00021 } pair_t;
00022 
00023 static int cmp_pair (void const *a, void const *b)
00024 {
00025   pair_t *pa = (pair_t *) a;
00026   pair_t *pb = (pair_t *) b;
00027   vl_int32 d = pa->w - pb->w ;
00028   if (d) return d ;
00029   /* break ties (qsort not stable) */
00030   return pa->j - pb->j;
00031 }
00032 
00040 static void alloc (VlIKMFilt *f, int M, int K)
00041 {
00042   if (f-> centers) vl_free (f-> centers) ;
00043   f-> K = K ;
00044   f-> M = M ;  
00045   f-> centers = vl_malloc (sizeof(vl_ikm_acc) * M * K) ;
00046 }
00047 
00048 
00053 static
00054 void vl_ikm_init_helper (VlIKMFilt *f)
00055 {
00056   switch (f-> method) {
00057   case VL_IKM_LLOYD:  vl_ikm_init_lloyd (f) ; break ;
00058   case VL_IKM_ELKAN:  vl_ikm_init_elkan (f) ; break ;
00059   }
00060 }
00061 
00069 VL_EXPORT
00070 void
00071 vl_ikm_init (VlIKMFilt* f, vl_ikm_acc const * centers, int M, int K)
00072 {
00073   alloc (f, M, K) ;
00074 
00075   memcpy (f-> centers, centers, sizeof(vl_ikm_acc) * M * K) ;
00076 
00077   vl_ikm_init_helper (f) ;
00078 }
00079 
00086 VL_EXPORT
00087 void
00088 vl_ikm_init_rand 
00089 (VlIKMFilt* f, int M, int K)
00090 {
00091   int k, i ;
00092 
00093   alloc (f, M, K) ;
00094   
00095   for (k = 0 ; k < K ; ++ k) {
00096     for (i = 0 ; i < M ; ++ i) {
00097       f-> centers [k * M + i] = (vl_ikm_acc) (vl_rand_uint32 ()) ;
00098     }
00099   }
00100   
00101   vl_ikm_init_helper (f) ;
00102 }
00103 
00112 VL_EXPORT
00113 void
00114 vl_ikm_init_rand_data 
00115 (VlIKMFilt* f, vl_uint8 const* data, int M, int N, int K)
00116 {
00117   int i, j, k ;
00118   pair_t  *pairs = (pair_t *) vl_malloc (sizeof(pair_t) * N);
00119   
00120   alloc (f, M, K) ;
00121   
00122   /* permute the data randomly */
00123   for (j = 0 ; j < N ; ++j) {
00124     pairs[j].j = j ;
00125     pairs[j].w = ((vl_int32) vl_rand_uint32 ()) >> 2 ;
00126   }
00127   
00128   qsort (pairs, N, sizeof(pair_t), cmp_pair);
00129   
00130   /* initialize centers from random data points */
00131   for (k = 0 ; k < K ; ++ k) {
00132     for (i = 0 ; i < M ; ++ i) {
00133       f-> centers [k * M + i] = data [pairs[k].j * M + i] ;
00134     }
00135   }  
00136   
00137   vl_free (pairs) ;
00138 
00139   vl_ikm_init_helper (f) ;
00140 }
00141 
00142 /* 
00143  * Local Variables: *
00144  * mode: C *
00145  * End: *
00146  */
Copyright © 2008 Andrea Vedaldi and Brian Fulkerson