convert Matlab matrix to OpenCV Mat. Support CV_32FC3 only currently.
The Codeint matlab2opencv(cv::Mat &img, const mxArray *&mat){
mwSize num_dims = mxGetNumberOfDimensions(mat);
const mwSize *dims = mxGetDimensions(mat);
//for (int i = 0; i
// mexPrintf("%d-th:%dn", i, dims[i]);
//}
if (num_dims != 3 || dims[2] != 3){
mexPrintf("error image dimensions!n");
return EXIT_FAILURE;
}
if (!mxIsSingle(mat)){
mexPrintf("the input matrix must be double with value in [0,1].");
return EXIT_FAILURE;
}
int rows = dims[0];
int cols = dims[1];
int nchs = 3;
img.create(rows, cols, CV_32FC3);
float *mat_data = (float*)mxGetData(mat);
float *mat_temp_ptr = NULL;
int layer_size = rows*cols;
for (int ch = 0; ch
mat_temp_ptr = mat_data + ch*layer_size;
int rd = 2 - ch;
for (int c = 0; c
for (int r = 0; r
img.at(r, c)[rd] = mat_temp_ptr[c*rows+r];
}
}
}
return EXIT_SUCCESS;
}