testHelperDataConversionMex.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #ifdef NICE_USELIB_MEX
  2. /**
  3. * @file GPHIKClassifierMex.cpp
  4. * @author Alexander Freytag
  5. * @date 07-01-2014 (dd-mm-yyyy)
  6. * @brief Matlab-Interface of our GPHIKClassifier, allowing for training, classification, optimization, variance prediction, incremental learning, and storing/re-storing.
  7. */
  8. // STL includes
  9. #include <math.h>
  10. #include <matrix.h>
  11. #include <mex.h>
  12. // NICE-core includes
  13. #include <core/vector/MatrixT.h>
  14. #include <core/vector/VectorT.h>
  15. #include "vislearning/cbaselib/Example.h"
  16. // Interface for conversion between Matlab and C objects
  17. #include "gp-hik-core/matlab/classHandleMtoC.h"
  18. #include "gp-hik-core/matlab/ConverterMatlabToNICE.h"
  19. #include "gp-hik-core/matlab/ConverterNICEToMatlab.h"
  20. using namespace std; //C basics
  21. using namespace NICE; // nice-core
  22. // MAIN MATLAB FUNCTION
  23. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  24. {
  25. // get the command string specifying what to do
  26. if (nrhs < 1)
  27. mexErrMsgTxt("No commands and options passed... Aborting!");
  28. if( !mxIsChar( prhs[0] ) )
  29. mexErrMsgTxt("First argument needs to be the command, ie.e, the unit test method to call... Aborting!");
  30. std::string cmd = MatlabConversion::convertMatlabToString( prhs[0] );
  31. // in all other cases, there should be a second input,
  32. // which the be the class instance handle
  33. if (nrhs < 2)
  34. {
  35. mexErrMsgTxt("Second input should be some kind of matrix variable");
  36. return;
  37. }
  38. if (nlhs < 1)
  39. {
  40. mexErrMsgTxt("No return value defined, possible loss of data... Aborting!");
  41. }
  42. ////////////////////////////////////////
  43. // Check which method to call //
  44. ////////////////////////////////////////
  45. if ( !strcmp("convertDoubleMatrixToExamples", cmd.c_str() ) )
  46. {
  47. if (nrhs != 3)
  48. {
  49. mexErrMsgTxt("needs 2 matrix inputs, first the training features, second the sample labels");
  50. return;
  51. }
  52. const mxArray *t_pArrTrainData = prhs[1];
  53. const mxArray *t_pArrTrainLabels = prhs[2];
  54. NICE::Vector t_vecLabelsTrain = MatlabConversion::convertDoubleVectorToNice( t_pArrTrainLabels );
  55. NICE::Matrix t_matDataTrain = MatlabConversion::convertDoubleMatrixToNice( t_pArrTrainData );
  56. OBJREC::Examples t_ExamplesTrain;
  57. bool bConversionSuccess = OBJREC::Examples::wrapExamplesAroundFeatureMatrix( t_matDataTrain, t_vecLabelsTrain, t_ExamplesTrain );
  58. //wrapExamplesAroundFeatureMatrix
  59. std::cerr << "Examples size: " << t_ExamplesTrain.size() << std::endl;
  60. for(int i=0; i< t_ExamplesTrain.size(); i++)
  61. {
  62. int iClass = t_ExamplesTrain[i].first;
  63. OBJREC::Example &t_Example = t_ExamplesTrain[i].second ;
  64. std::cerr << "Example["<<i<<"]" << "L:" << iClass << " data: "<< *t_Example.vec << std::endl;
  65. }
  66. // clean up
  67. t_ExamplesTrain.clean();
  68. // output
  69. plhs[0] = mxCreateLogicalScalar( bConversionSuccess );
  70. return;
  71. }
  72. // Got here, so command not recognized
  73. std::string errorMsg (cmd.c_str() );
  74. errorMsg += " -- command not recognized.";
  75. mexErrMsgTxt( errorMsg.c_str() );
  76. }
  77. #endif