I have tried to convert Matlab code into opencv code. I have used few inbuilt Matlab c++ code for my opencv implementation. I don't know emxArray_real_T to Mat conversion. Does anyone know the answer?
there is no need for "conversion", if you read the documentation.
emxArray_real_T is defined like this:
typedef struct emxArray_real_T
{
double *data;
int *size;
int allocatedSize;
int numDimensions;
boolean_T canFreeData;
} emxArray_real_T;
So you have your data, stored as an array of double, and the matrix size, stored as an array of int. That's exactly what OpenCV Mat needs for the constructor.
Mat::Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP)
In your specific case (I break the code over multiple lines just to make it more readable):
// if your emxArray_real_T is called 'ml_array'
int rows=ml_array.size[0];
int cols=ml_array.size[1];
double* data=ml_array.data;
// CV_64FC1 is how OpenCV calls double-precision
// floating point values according to the documentation.
Mat ocv_img(rows, cols, CV_64FC1, data);
Caveats:
I don't know if OpenCV assumes some format for type CV_64FC1. E.g. it may require IEEE 754. It's up to you to know how your compiler represents floating point values.
AFAIK, Matlab works with column-major arrays, you may need to transpose your image. Do some experimentation.
I do not use Matlab (because it limits user freedom and harms research) so I didn't test any of this. Some tweaking may be required.
there is no need for "conversion", if you read the documentation.
emxArray_real_T is defined like this:
typedef struct emxArray_real_T
{
double *data;
int *size;
int allocatedSize;
int numDimensions;
boolean_T canFreeData;
} emxArray_real_T;
So you have your data, stored as an array of double, and the matrix size, stored as an array of int. That's exactly what OpenCV Mat needs for the constructor.
Mat::Mat(int _rows, int _cols, int _type, void* _data, size_t _step=AUTO_STEP)
In your specific case (I break the code over multiple lines just to make it more readable):
// if your emxArray_real_T is called 'ml_array'
int rows=ml_array.size[0];
int cols=ml_array.size[1];
double* data=ml_array.data;
// CV_64FC1 is how OpenCV calls double-precision
// floating point values according to the documentation.
Mat ocv_img(rows, cols, CV_64FC1, data);
Caveats:
I don't know if OpenCV assumes some format for type CV_64FC1. E.g. it may require IEEE 754. It's up to you to know how your compiler represents floating point values.
AFAIK, Matlab works with column-major arrays, you may need to transpose your image. Do some experimentation.
I do not use Matlab (because it limits user freedom and harms research) so I didn't test any of this. Some tweaking may be required.