Ask Your Question
0

How to pass OpenCV Mat(CV_8UC3) to Matlab generated c++ code and vice-versa? Efficiently

asked 2019-03-14 07:22:36 -0500

JeyP4 gravatar image

updated 2019-03-14 09:12:03 -0500

berak gravatar image

'Matlab coder' generated a c++ function. (function name is 'matlab')

matlab(uv0, outImg);

//Before calling, arguments need to declared
emxArray_uint8_T *outImg;
static unsigned char uv0[758016];
emxInitArray_uint8_T(&outImg, 3);

I don't know how 'cv::Mat' can be converted into 'unsigned char uv0'

and

'emxArray_uint8_T *outImg' back into cv::Mat.

//some related function are

static void argInit_376x672x3_uint8_T(unsigned char result[758016])
{
  int idx0;
  int idx1;
  unsigned char result_tmp;
  for (idx0 = 0; idx0 < 376; idx0++) {
    for (idx1 = 0; idx1 < 672; idx1++) {
      result_tmp = argInit_uint8_T();
      result[idx0 + 376 * idx1] = result_tmp;
      result[(idx0 + 376 * idx1) + 252672] = result_tmp;
      result[(idx0 + 376 * idx1) + 505344] = argInit_uint8_T();
    }
  }
}

static unsigned char argInit_uint8_T()
{
  return 0U;
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2019-03-15 03:41:20 -0500

berak gravatar image

(disclaimer: no matlab here)

opencv uses "interleaved" color channels, like bgr bgr bgr, matlab has seperate color planes, like rrr ggg bbb

you'll have to split the opencv Mat into channels, stack them to a single buffer, and reverse the operation on the way back:

// ocv 2 matlab:
int h = 376;
int w = 672;
int slice_size = 252672; // w*h

Mat m = ...
Mat chan[3];
split(m, chan);
// careful: this assumes, uv0 points to an allocated buffer !
memcpy(uv0, chan[2].ptr<uchar>(), slice_size); // in R G B order for matlab !
memcpy(&[uv0[slice_size], chan[1].ptr<uchar>(), slice_size);
memcpy(&[uv0[slice_size*2], chan[0].ptr<uchar>(), slice_size);
// ... use uv0


// matlab 2 ocv:
emxArray_uint8_T *outImg;

Mat R(h,w,CV_8U, outImg);
Mat G(h,w,CV_8U, outImg+slice_size);
Mat B(h,w,CV_8U, outImg+(2*slice_size));

Mat chan[] {B,G,R}; // BGR order for opencv !
Mat final;
merge(chan, 3, final);
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2019-03-14 07:22:36 -0500

Seen: 136 times

Last updated: Mar 15