Code covered by the BSD License  

Highlights from
C/C++ and Matlab types convertor

4.25

4.2 | 8 ratings Rate this file 40 Downloads (last 30 days) File Size: 65.3 KB File ID: #20927
image thumbnail

C/C++ and Matlab types convertor

by

 

31 Jul 2008 (Updated )

Bidirectional conversion between C/C++ type (STL, openCV) and Matlab matrix (compile or run time)

| Watch this File

File Information
Description

Title: C/C++ <-> Matlab types convertor
Brief: Bidirectional conversion between C/C++ types (native, STL, openCV...) and Matlab matrix (compile or run time).
Key words: C, C++, mxArray, OpenCV, IplImage, iterator, mex, engine

Description:

Consider below scenarios (especially when programming with signal/speech/image processing and scientific computation):
1 Dumping C/C++ data into Matlab workspace in run-time to visualize data and facilitate debugging. But calling engine APIs directly seems not to be that convenient.
2 Implementing underlying algorithm as mex file to accelarate m file. Each time you must convert mxArray to C/C++ types, do some job, and finally convert C/C++ types back to returned mxArray.

This project provides easy access to above tasks given appropriate iterators (refer to any C++ STL textbook if iterator seems nothing to you).

As to scenario 1 see below examples:

/**************************
 * EXAMPLE A for scenario 1
 **************************/
unsigned char rgb_img[3*256*126]; // 3D signal, i.e. color image
// initialize rgb_img and do something to it...
const char* command = "figure; imshow(I)";

// Dump it as 3D matrix named I. column(width):256?row(height):126 and page(channel):3,
// then view it using matlab function "imshow".
matlab << name ("I")
  << width (256) << height (126) << channel (3)
  << start (rgb_img)
  << cmd (command);

Dump 1D and 2D signal goes similarly. Currently maximum 3D is supported.
Also openCV is supported. For example:

/**************************
 * EXAMPLE B for scenario 1
 **************************/
IplImage* pimg; // openCV image types
// initialize p and do something to it...

// Dump pimg as matrix I. The size and types are made the same as pimg automatically.
// Then view it.
matlab << name("I") << var(pimg) << cmd("figure;imshow(I)");

As to scenario 2 see below example:

/**************************
 * EXAMPLE A for scenario 2
 **************************/
void mexFunction(int nlhs, mxArray *plhs[],
                 int nrhs, const mxArray *prhs[])
{
  vector<double> vt;
  double val;
  int n = (int) mxGetNumberOfElements(prhs[0]);
  vt.resize(n);
  // 1. convert mxArray to C/C++ types
  mat_to_values (prhs[0], vt.begin());
  mat_to_scalar (prhs[1], &val);

  // 2. do some job to vt and val

  // 3. finally convert C/C++ types back to mxArray
  plhs[0] = values_to_new_mat (vt.begin(),vt.end());
}

mat_to_values and values_to_new_mat take pointer/iterator as input arguments, with appropriate iterator in hand you can also convert C native array, std::list or any other user data types (e.g. OpenCV IplImage).

For details, see more examples and the document in the zip file.
Have fun:)

Note:
To compile the codes with VC10, you need add _SECURE_SCL=1 to COMPFLAGS into mexopts.bat file, or you will get the errors:

c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(287) : error C2582: 'operator '='

References
1. http://stackoverflow.com/questions/9357699/error-c2582-operator-function-is-unavailable-in-bitstreambitset-extracto/16558308#16558308

2.
http://preshing.com/20110807/the-cost-of-_secure_scl

Courtesy to SenyaLab (http://www.mathworks.cn/matlabcentral/fileexchange/authors/195611) on this issue!

MATLAB release MATLAB 7.6 (R2008a)
Other requirements Cross platform
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (24)
26 Feb 2014 Gleb

Thank you fo this submission!
Is it already adapted to OpenCV2.x?
Cos' I got Cannot open include file: 'cxtypes.h' error

26 Feb 2014 Gleb  
16 May 2013 sun peng

@SenyaLab:

Thanks so much for pointing out this!

15 May 2013 SenyaLab

Adding _SECURE_SCL=1 to COMPFLAGS into mexopts.bat file solved theproblem for me with this error.

c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(287) : error C2582: 'operator '='

References
1. http://stackoverflow.com/questions/9357699/error-c2582-operator-function-is-unavailable-in-bitstreambitset-extracto/16558308#16558308

2.
http://preshing.com/20110807/the-cost-of-_secure_scl

I am using Matalb2012b-64bit, VS2010-Professional, and OpenCV 2.4.5.

09 Jul 2012 Paul

Hey Kota, I did have the same problem! It seems to be a Visual Studio 2010 problem! Just install the 2008 version from here http://www.microsoft.com/en-us/download/details.aspx?id=14597 ! There it works fine!

29 Jun 2012 Kota

I got this error on VC2010 and MATLAB R2011a. It looks like there is a problem with mc_convert.cpp(162)?

c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(287) : error C2582: 'operator '=' ŠÖ”‚ð 'mxArray_iter_3d<data_t>' ‚ÅŽg—p‚Å‚«‚Ü‚¹‚ñB
with
[
data_t=T
]
c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\INCLUDE\xutility(2188) : ƒRƒ“ƒpƒCƒ‹‚³‚ꂽƒNƒ‰ƒX‚Ì ƒeƒ“ƒvƒŒ[ƒg ‚̃Cƒ“ƒXƒ^ƒ“ƒX‰» '_Iter &std::_Rechecked<_OutIt,_OutIt>(_Iter &,_UIter)' ‚ÌŽQÆ‚ðŠm”F‚µ‚Ä‚­‚¾‚³‚¢
with
[
_Iter=mxArray_iter_3d<T>,
_OutIt=mxArray_iter_3d<T>,
_UIter=mxArray_iter_3d<T>
]
c:\users\kota\documents\matlab\code\mc_convert.cpp(162) : ƒRƒ“ƒpƒCƒ‹‚³‚ꂽƒNƒ‰ƒX‚Ì ƒeƒ“ƒvƒŒ[ƒg ‚̃Cƒ“ƒXƒ^ƒ“ƒX‰» '_OutIt std::copy<pix_iter_rgb<data_t>,mxArray_iter_3d<data_t>>(_InIt,_InIt,_OutIt)' ‚ÌŽQÆ‚ðŠm”F‚µ‚Ä‚­‚¾‚³‚¢
with
[
_OutIt=mxArray_iter_3d<T>,
data_t=T,
_InIt=pix_iter_rgb<T>
]

29 Jun 2012 Kota  
05 Jan 2012 Dipesh Patil

hello friends i have joined this community today itself. I was searching about c to matlab conversion . If you have done it then please try to help.

Best regards.

07 Feb 2011 sun peng

To Bonneau:
I suggest you'd better use the up-to-date Matlab, or I can't assure the stability of my codes:)

To zeynab:
There is no particular sequence, pick any example you like:)

13 Jun 2009 zeynab

I don't know how to run these example, what is the order of executing of these file, plz write help me....
I am realy confused! I need use this example
thanks in advance
Zeynab

13 Jun 2009 zeynab  
05 Jun 2009 Phong Vo

goofd

12 May 2009 Patrick Bonneau

mwSize is probably related to an older version of matlab. I think I could declare it like that.

#ifndef mwSize
#define mwSize int
#endif

But I dont know what I can do with 'dim_'. I can't change My version of matlab. Do you have an idea?

12 May 2009 Patrick Bonneau

I am not able to compile. I have the error below. Do you have an idea?

------ Début de la génération : Projet : dump_opencv, Configuration : Debug Win32 ------
Compilation en cours...
main.cpp
d:\etude\maitrise\uvicmasc\project\cpp\masc\mc_ext_\src\matlab_dump.h(124) : error C2146: syntax error : missing ';' before identifier 'dim_'
d:\etude\maitrise\uvicmasc\project\cpp\masc\mc_ext_\src\matlab_dump.h(124) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\etude\maitrise\uvicmasc\project\cpp\masc\mc_ext_\src\matlab_dump.h(124) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\etude\maitrise\uvicmasc\project\cpp\masc\mc_ext_\src\matlab_dump.h(94) : error C2065: 'dim_' : undeclared identifier
matlab_dump.cpp
d:\etude\maitrise\uvicmasc\project\cpp\masc\mc_ext_\src\matlab_dump.h(124) : error C2146: syntax error : missing ';' before identifier 'dim_'
d:\etude\maitrise\uvicmasc\project\cpp\masc\mc_ext_\src\matlab_dump.h(124) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\etude\maitrise\uvicmasc\project\cpp\masc\mc_ext_\src\matlab_dump.h(124) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\etude\maitrise\uvicmasc\project\cpp\masc\mc_ext_\src\matlab_dump.h(94) : error C2065: 'dim_' : undeclared identifier
mc_convert.cpp
d:\etude\maitrise\uvicmasc\project\cpp\masc\mc_ext_\src\mc_convert.cpp(248) : error C2065: 'mwSize' : undeclared identifier
d:\etude\maitrise\uvicmasc\project\cpp\masc\mc_ext_\src\mc_convert.cpp(248) : error C2143: syntax error : missing ';' before 'const'
d:\etude\maitrise\uvicmasc\project\cpp\masc\mc_ext_\src\mc_convert.cpp(248) : error C2143: syntax error : missing ';' before 'const'
d:\etude\maitrise\uvicmasc\project\cpp\masc\mc_ext_\src\mc_convert.cpp(249) : error C2065: 'ndim' : undeclared identifier
d:\etude\maitrise\uvicmasc\project\cpp\masc\mc_ext_\src\mc_convert.cpp(291) : error C2143: syntax error : missing ';' before 'const'
d:\etude\maitrise\uvicmasc\project\cpp\masc\mc_ext_\src\mc_convert.cpp(291) : error C2143: syntax error : missing ';' before 'const'
Generating Code...
Le journal de génération a été enregistré à l'emplacement "file://d:\Etude\Maitrise\UvicMASc\Project\CPP\MASc\mc_ext_\example\dump_opencv\Debug\BuildLog.htm"
dump_opencv - 14 erreur(s), 0 avertissement(s)
========== Génération : 0 a réussi, 1 a échoué, 0 mis à jour, 0 a été ignoré ==========

08 May 2009 Martin Dale

Sana, you need to add libmx.lib (and also any other libraries such as libmex.lib, libmat.lib, libeng.lib, cxcore110.lib and cv110.lib) to the "additional dependancies" under Project->Properties->Linker->Input->Additional Dependancies. You may also need to add the paths to their locations (and their header files) in Linker->General->Additional Library Directories and C/C++->General->Additional Include Directories. Hope this helps. Don't forget to add MATLAB_MEX_FILE and HAS_OPENCV to the C/C++->PreProcessor->Definitions list either.

07 May 2009 Sana Sarfraz

I am trying to compile the project opencv_mex in visual studio and getting the following error.

1>LINK : fatal error LNK1104: cannot open file 'libmx.lib'

Any idea why?

16 Apr 2009 julian muñoz

Hi to all, anybody i's using the library under linux?
I have several errors, it could be bye HAS_OPENCV variable??

Thanks

27 Feb 2009 Xunkai Wei

I give a rate five!

27 Feb 2009 Xunkai Wei

good idea !

19 Feb 2009 Aviv Hurvitz  
18 Dec 2008 sun peng

Hi Caponetti:
It is supposed to be so, althouth I didn't test it.
All the codes are standard C++.

18 Dec 2008 Fabio Caponetti

Does it work under linux?

28 Nov 2008 sun peng

Conversion between mxArray and CvMat is available now(2D only).
Thanks Martin Dale for providing his codes generously:)

18 Nov 2008 Martin Dale

Very useful tool although I'm struggling to make it work with the openCV CvMat data type - it works well for images though. Can't quire figure out CcMat is supported or not.

Updates
28 Nov 2008

* More types for conversion
* conversion between CvMat and mxArray (2D currently)
(All above are due to Dale Martin)
* Explicit function name. The old names are still valid but deprecated

29 Nov 2008

minor modification on document

21 Feb 2009

* A bug in pix_iter_rgb::end() is fixed
(Thank Aviv Hurvitz(http://www.mathworks.com/matlabcentral/fileexchange/authors/40629) for pointing it out:) )

21 May 2009

Modification on typename/typedef syntex to make it work with GCC
This job attributes to Martin Dale(http://www.mathworks.com/matlabcentral/fileexchange/authors/36049)
Thanks for his constant focus and constructive suggestions on this project:)

05 Jun 2009

* Codes involving typename are modified to make it work with GCC
(Thank Jack. Z.G.Tan (zgtan(at)eee(dot)hku(dot)hk) for pointing it out:) )

28 Jun 2009

* support for mxLOGICAL_CLASS conversion is added

16 May 2013

Guidance to compiling with VC10

29 Oct 2013

Adapt to OpenCV 2.x ("cxtypes.h" no longer exists)

Contact us