vl_ikmeanspush.c
Go to the documentation of this file.
00001 /* file:        ikmeanspush.c
00002 ** description: MEX weighted ikmeanspush function.
00003 ** author:      Andrea Vedaldi
00004 **/
00005 
00006 /*
00007 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
00008 All rights reserved.
00009 
00010 This file is part of the VLFeat library and is made available under
00011 the terms of the BSD license (see the COPYING file).
00012 */
00013 
00014 #include<mexutils.h>
00015 
00016 #include<stdio.h>
00017 #include<stdlib.h>
00018 #include<math.h>
00019 #include<string.h>
00020 #include<assert.h>
00021 
00022 #include <vl/generic.h>
00023 #include <vl/ikmeans.h>
00024 
00025 enum {
00026   opt_method,
00027   opt_verbose
00028 } ;
00029 
00030 vlmxOption  options [] = {
00031   {"Method",       1,   opt_method      },
00032   {"Verbose",      0,   opt_verbose     },
00033   {0,              0,   0               }
00034 } ;
00035 
00036 /* driver */
00037 void
00038 mexFunction(int nout, mxArray *out[],
00039             int nin, const mxArray *in[])
00040 {
00041   enum {IN_X=0,IN_C,IN_END} ;
00042   enum {OUT_ASGN=0} ;
00043   VlIKMFilt *ikmf ;
00044   vl_uint32* asgn ;
00045   vl_ikmacc_t* centers ;
00046   vl_uint8* data ;
00047   mwSize M,N,K ;
00048   vl_uindex j ;
00049   int method_type = VL_IKM_LLOYD ;
00050   int verb = 0 ;
00051 
00052   int opt ;
00053   int next = IN_END ;
00054   mxArray const *optarg ;
00055 
00056   VL_USE_MATLAB_ENV ;
00057 
00062   if (nin < 2) {
00063     mexErrMsgTxt("At least two arguments required.") ;
00064   } else if (nout > 2) {
00065     mexErrMsgTxt("Too many output arguments.") ;
00066   }
00067 
00068   if(mxGetClassID(in[IN_X]) != mxUINT8_CLASS) {
00069     mexErrMsgTxt("X is not of class UINT8.") ;
00070   }
00071 
00072   if(mxGetClassID(in[IN_C]) != mxINT32_CLASS) {
00073     mexErrMsgTxt("C is not of class INT32.") ;
00074   }
00075 
00076   M = mxGetM(in[IN_X]) ; /* n of components */
00077   N = mxGetN(in[IN_X]) ; /* n of elements */
00078   K = mxGetN(in[IN_C]) ; /* n of centers */
00079 
00080   if (mxGetM(in[IN_C]) != M ) {
00081     mexErrMsgTxt("DATA and CENTERS must have the same number of columns.") ;
00082   }
00083 
00084   while ((opt = vlmxNextOption (in, nin, options, &next, &optarg)) >= 0) {
00085     char buf [1024] ;
00086 
00087     switch (opt) {
00088 
00089     case opt_verbose :
00090       ++ verb ;
00091       break ;
00092 
00093     case opt_method :
00094       if (!vlmxIsString (optarg, -1)) {
00095         mexErrMsgTxt("'Method' must be a string.") ;
00096       }
00097       if (mxGetString (optarg, buf, sizeof(buf))) {
00098         mexErrMsgTxt("Option argument too long.") ;
00099       }
00100       if (strcmp("lloyd", buf) == 0) {
00101         method_type = VL_IKM_LLOYD ;
00102       } else if (strcmp("elkan", buf) == 0) {
00103         method_type = VL_IKM_ELKAN ;
00104       } else {
00105         mexErrMsgTxt("Unknown cost type.") ;
00106       }
00107       break ;
00108 
00109     default :
00110       abort() ;
00111     }
00112   }
00113 
00118   if (verb) {
00119     char const * method_name = 0 ;
00120     switch (method_type) {
00121     case VL_IKM_LLOYD: method_name = "Lloyd" ; break ;
00122     case VL_IKM_ELKAN: method_name = "Elkan" ; break ;
00123     default :
00124       abort() ;
00125     }
00126     mexPrintf("ikmeanspush: Method = %s\n", method_name) ;
00127     mexPrintf("ikmeanspush: ndata  = %d\n", N) ;
00128   }
00129 
00130   out[OUT_ASGN] = mxCreateNumericMatrix (1, N, mxUINT32_CLASS, mxREAL) ;
00131 
00132   data = (vl_uint8*) mxGetData (in[IN_X]) ;
00133   centers = (vl_ikmacc_t*) mxGetData (in[IN_C]) ;
00134   asgn = (vl_uint32*) mxGetData (out[OUT_ASGN]) ;
00135   ikmf = vl_ikm_new (method_type) ;
00136 
00137   vl_ikm_set_verbosity (ikmf, verb) ;
00138   vl_ikm_init (ikmf, centers, M, K) ;
00139   vl_ikm_push (ikmf, asgn, data, N) ;
00140 
00141   /* adjust for MATLAB indexing */
00142   for(j = 0 ; j < N ; ++j) ++ asgn[j] ;
00143 
00144   vl_ikm_delete (ikmf) ;
00145 }


libvlfeat
Author(s): Andrea Vedaldi
autogenerated on Thu Jun 6 2019 20:25:51