A step by step guide to easily send OpenCV C++ variables to Matlab -


i want able send opencv variables matlab in order plot graphs , calculate statistics in confortable way.

i know have use matlab engine, there little on web how make accessible part of code, or functions convert cv::mat matlab arrays, or how deal column-major , row-major in specific case.

i think step step procedure opencv-to-matlab interesting since opencv becoming popular , matlab helps lot debugging.

step step sending data opencv matlab

1.- including , linking libraries

the neccesary headers working matlab engine "engine.h" , "mex.h". include path this:

c:\program files (x86\matlab\r2010a\extern\include)

in additional dependencies should add: libeng.lib, libmex.lib , libmx.lib.

the easiest way set project using cmake, need write lines

find_package( matlab required )

include_directories( ${matlab_include_dir})

cmake set paths , link needed libraries. using environment variables make project user-independent. that's reason why use cmake.

2.- singleton template unique instance of matlab engine.

a comfortable way call matlab engine part of code creating singleton template. create "matlabsingleton.h", example, , write this.

#include "engine.h"; #include "mex.h";  class matlabwrapper { private:     static matlabwrapper *_theinstance;     ///< private instance of class     matlabwrapper(){}               ///< private constructor     static engine *eng;  public:         static matlabwrapper *getinstance()         ///< instance public method     {         if(!_theinstance) _theinstance = new matlabwrapper();   // if null, create         return _theinstance;                         } public:         static void openengine();         void initializevariable(const string vname) const;         // ... other functions ... }; 

then in "matlabsingleton.cpp" should write

#include "matlabsingleton.h"  matlabwrapper *matlabwrapper::_theinstance = null;  ///< initialize instance null     engine *matlabwrapper::eng=null;  void matlabwrapper::openengine() {     if (!(eng = engopen(null)))      {         cerr << "can't start matlab engine" << endl;         exit(-1);     }        } void matlabwrapper::initializevariable(const string vname) const {     string command = vname + "=0";     engevalstring(eng, command.c_str()); } 

3.- using matlab engine main code.

there many ways define function initializematlab() initialize variables (matlab variables) later use. don't need create class matlabwrapper because first time call getinstance() created. next times call getinstance returned. that's singletone for.

void initializematlab(){    matlabwrapper::getinstance()->initializevariable("whatever1"); //class created    matlabwrapper::getinstance()->initializevariable("whatever2"); //instance of class returned } 

4.- sending opencv matrix matlab

at foun more difficulties. simple function send matrix matlab debugging step step. overwritten each time.

void matlabwrapper::cvloadmatrixtomatlab(const mat& m, const string name) {     int rows=m.rows;     int cols=m.cols;              string text;     mxarray *t=mxcreatedoublematrix(cols, rows, mxreal);                double *buffer=(double*)mxgetpr(t);     for(int i=0; i<rows; i++){             for(int j=0; j<cols; j++){             buffer[i*(cols)+j]= (double)m.at<float>(i,j);               }     }        engputvariable(eng, name.c_str(), t);     text = name + "=" + name + "'";                    // column major row major     engevalstring(eng, text.c_str());     mxdestroyarray(t); } 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -