testHelperDataConversionMex.cpp 3.1 KB

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