matlab将矩阵c右下角3x2子矩阵,C / C+ +类型和Matlab矩阵之间的双向转换

C/C++ and Matlab types convertor

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[])

{

vectorvt;

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!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值