vl_vlad.c
Go to the documentation of this file.
00001 
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 <vl/vlad.h>
00015 #include <mexutils.h>
00016 #include <string.h>
00017 #include <stdio.h>
00018 
00019 enum {
00020   opt_verbose,
00021   opt_normalize_components,
00022   opt_unnormalized,
00023   opt_square_root,
00024   opt_normalize_mass
00025 } ;
00026 
00027 
00028 vlmxOption  options [] = {
00029   {"Verbose",             0,   opt_verbose                  },
00030   {"Unnormalized",        0,   opt_unnormalized             },
00031   {"NormalizeComponents", 0,   opt_normalize_components     },
00032   {"NormalizeMass",       0,   opt_normalize_mass           },
00033   {"SquareRoot",          0,   opt_square_root              }
00034 } ;
00035 
00036 /* driver */
00037 void
00038 mexFunction (int nout VL_UNUSED, mxArray * out[], int nin, const mxArray * in[])
00039 {
00040   enum {IN_DATA = 0, IN_MEANS, IN_ASSIGNMENTS, IN_END} ;
00041   enum {OUT_ENC} ;
00042 
00043   int opt ;
00044   int next = IN_END ;
00045   mxArray const  *optarg ;
00046 
00047   mxArray const * means_array = in[IN_MEANS] ;
00048   mxArray const * data_array = in[IN_DATA] ;
00049   mxArray const * assign_array = in[IN_ASSIGNMENTS] ;
00050 
00051   vl_size numClusters ;
00052   vl_size dimension ;
00053   vl_size numData ;
00054   int flags = 0 ;
00055 
00056   void const * means = NULL;
00057   void const * assignments = NULL;
00058   void const * data = NULL ;
00059   int verbosity = 0 ;
00060 
00061   vl_type dataType ;
00062   mxClassID classID ;
00063 
00064   VL_USE_MATLAB_ENV ;
00065 
00066   /* -----------------------------------------------------------------
00067    *                                               Check the arguments
00068    * -------------------------------------------------------------- */
00069 
00070   if (nin < 3) {
00071     vlmxError (vlmxErrInvalidArgument,
00072                "At least three arguments required.");
00073   }
00074   if (nout > 1) {
00075     vlmxError (vlmxErrInvalidArgument,
00076                "At most one output argument.");
00077   }
00078 
00079   if (!vlmxIsMatrix(IN(DATA),-1,-1)) {
00080     vlmxError (vlmxErrInvalidArgument,
00081                "DATA is not a dense matrix.") ;
00082   }
00083 
00084   classID = mxGetClassID (IN(DATA)) ;
00085   switch (classID) {
00086     case mxSINGLE_CLASS: dataType = VL_TYPE_FLOAT ; break ;
00087     case mxDOUBLE_CLASS: dataType = VL_TYPE_DOUBLE ; break ;
00088     default:
00089       vlmxError (vlmxErrInvalidArgument,
00090                  "DATA is neither of class SINGLE or DOUBLE.") ;
00091   }
00092 
00093   if (mxGetClassID (IN(MEANS)) != classID) {
00094     vlmxError(vlmxErrInvalidArgument, "MEANS is not of the same class as DATA.") ;
00095   }
00096   if (mxGetClassID (IN(ASSIGNMENTS)) != classID) {
00097     vlmxError(vlmxErrInvalidArgument, "ASSIGNMENTS is not of the same class as DATA.") ;
00098 
00099   }
00100 
00101   dimension = mxGetM (IN(DATA)) ;
00102   numData = mxGetN (IN(DATA)) ;
00103   numClusters = mxGetN (IN(MEANS)) ;
00104 
00105   if (dimension == 0) {
00106     vlmxError (vlmxErrInvalidArgument, "SIZE(DATA,1) is zero.") ;
00107   }
00108 
00109   if (!vlmxIsMatrix(IN(MEANS), dimension, -1)) {
00110     vlmxError (vlmxErrInvalidArgument, "MEANS is not a matrix or does not have the right size.") ;
00111   }
00112 
00113   if (!vlmxIsMatrix(IN(ASSIGNMENTS), numClusters, -1)) {
00114     vlmxError (vlmxErrInvalidArgument, "ASSIGNMENTS is not a matrix or does not have the right size.") ;
00115   }
00116 
00117   while ((opt = vlmxNextOption (in, nin, options, &next, &optarg)) >= 0) {
00118     switch (opt) {
00119       case opt_verbose : ++ verbosity ; break ;
00120       case opt_unnormalized: flags |= VL_VLAD_FLAG_UNNORMALIZED ; break ;
00121       case opt_normalize_components: flags |= VL_VLAD_FLAG_NORMALIZE_COMPONENTS ; break ;
00122       case opt_normalize_mass: flags |= VL_VLAD_FLAG_NORMALIZE_MASS ; break ;
00123       case opt_square_root: flags |= VL_VLAD_FLAG_SQUARE_ROOT ; break ;
00124       default :
00125         abort() ;
00126       break ;
00127     }
00128   }
00129 
00130   /* -----------------------------------------------------------------
00131    *                                                        Do the job
00132    * -------------------------------------------------------------- */
00133 
00134   data = mxGetPr(data_array);
00135   means = mxGetPr(means_array);
00136   assignments = mxGetData(assign_array);
00137 
00138   if (verbosity) {
00139     mexPrintf("vl_vlad: num data: %d\n", numData) ;
00140     mexPrintf("vl_vlad: num clusters: %d\n", numClusters) ;
00141     mexPrintf("vl_vlad: data dimension: %d\n", dimension) ;
00142     mexPrintf("vl_vlad: code dimension: %d\n", numClusters * dimension) ;
00143     mexPrintf("vl_vlad: unnormalized: %s\n", VL_YESNO(flags & VL_VLAD_FLAG_UNNORMALIZED)) ;
00144     mexPrintf("vl_vlad: normalize mass: %s\n", VL_YESNO(flags & VL_VLAD_FLAG_NORMALIZE_MASS)) ;
00145     mexPrintf("vl_vlad: normalize components: %s\n", VL_YESNO(flags & VL_VLAD_FLAG_NORMALIZE_COMPONENTS)) ;
00146     mexPrintf("vl_vlad: square root: %s\n", VL_YESNO(flags & VL_VLAD_FLAG_SQUARE_ROOT)) ;
00147   }
00148 
00149   OUT(ENC) = mxCreateNumericMatrix (dimension * numClusters, 1, classID, mxREAL) ;
00150 
00151   vl_vlad_encode (mxGetPr(OUT(ENC)), dataType,
00152                   means, dimension, numClusters,
00153                   data, numData,
00154                   assignments,
00155                   flags) ;
00156 }


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