This thread shows how to convert a CvMat
to mxArray
. Even though it's not exactly the conversion code you are looking for, it's pretty close.
This is a simple conversion and you should be able to adjust the code to work with cv::Mat
instead of CvMat
. If you can't, a quick hack is to convert your cv::Mat
data to CvMat
and then use the code below as is (taken from the link I suggested):
mxArray* CvMat_to_new_mxArr (const CvMat* mat)
{
const int TYPE = cvGetElemType (mat);
// 2-d image
if (CV_64FC1 == TYPE) {
return helper_2dcvmat_to_mat<CV_64FC1> (mat);
}
else if (CV_32FC1 == TYPE) {
return helper_2dcvmat_to_mat<CV_32FC1> (mat);
}
else if (CV_32SC1 == TYPE) {
return helper_2dcvmat_to_mat<CV_32SC1> (mat);
}
else if (CV_16SC1 == TYPE) {
return helper_2dcvmat_to_mat<CV_16SC1> (mat);
}
else if (CV_16UC1 == TYPE) {
return helper_2dcvmat_to_mat<CV_16UC1> (mat);
}
else if (CV_8UC1 == TYPE) {
return helper_2dcvmat_to_mat<CV_8UC1> (mat);
}
else if (CV_8SC1 == TYPE) {
return helper_2dcvmat_to_mat<CV_8SC1> (mat);
}
//Multi-dimensional arrays not supported, yet.
/*
// 3-d image
else if (CV_64FC3 == TYPE) {
return helper_rgbimage_to_mat<IPL_DEPTH_64F> (img);
}
else if (CV_32FC3 == TYPE) {
return helper_rgbimage_to_mat<IPL_DEPTH_32F> (img);
}
else if (CV_8UC3 == TYPE) {
return helper_rgbimage_to_mat<IPL_DEPTH_8U> (img);
}
*/
// unsupported conversion, return null mxArray
return mxCreateDoubleMatrix(0,0,mxREAL);
}
template<int TYPE>
mxArray* helper_2dcvmat_to_mat (const CvMat* mat)
{
void* pBeg;
int pitch;
cvGetRawData(mat, (uchar**)&pBeg,&pitch);
CvSize size = cvGetSize (mat);
const mxClassID cid = cvm_traits<TYPE>::CID;
mxArray* pArrOut =
mxCreateNumericMatrix(size.height,size.width,cid,mxREAL);
void* pBegOut = mxGetData(pArrOut);
typedef mc_traits<cid>::CT T;
pix_iterator_2d<T,eRowWise> it_src1(static_cast<T*>(pBeg),
size.width,size.height,pitch);
pix_iterator_2d<T,eRowWise> it_src2(static_cast<T*>(pBeg),
size.width,size.height,pitch);
it_src2.end ();
pix_iterator_2d<T,eColWise> it_dest(static_cast<T*>(pBegOut),
size.width,size.height);
std::copy (it_src1,it_src2,it_dest);
return pArrOut;
}