opencv / opencv_contrib

Repository for OpenCV's extra modules
Apache License 2.0
9.33k stars 5.74k forks source link

some errors in cv::bridge::Bridge::FromMat #322

Open antran89 opened 9 years ago

antran89 commented 9 years ago

Hi OpenCV developers, I am using cv::bridge::Bridge to convert a cv::Mat object to an matlab::mxArray in a simple mex file. The program can be compiled fine, but it will cause Matlab crashed when running. I cannot debug this error by my own. It might be my usage of the function is not correctly. I hope someone who has experiences in this matlab interface will point out some critical facts. Thank you.

#include <mex.h>
#include <opencv2/core.hpp>
#include <opencv2/matlab/mxarray.hpp>
#include <opencv2/matlab/bridge.hpp>
#include <stdint.h>

using namespace cv;
using namespace std;

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    int dims = 5;
    mxArray *cell_array_ptr = mxCreateCellMatrix(5, 1);

    mxArray *num_array = mxCreateNumericMatrix(1, dims, mxUINT8_CLASS, mxREAL);
    mxSetData( num_array, mxMalloc(dims*sizeof(uint8_t)) );
    uint8_t *num_data = (uint8_t*)mxGetData(num_array);
    for (int i = 0; i < dims; i++)
        num_data[i] = i;
    mxSetCell(cell_array_ptr, 0, num_array);

    Mat a = Mat::zeros(3, 3, CV_32F);
    matlab::MxArray arr = cv::bridge::Bridge::FromMat<float>(a);    // this line has errors
    //mxSetCell(cell_array_ptr, 1, arr.releaseOwnership());

    plhs[0] = cell_array_ptr;
}

I attached cpp, log, and compile files in this repo https://github.com/howtobeahacker/opencv_matlab_interface. The program is tested under Ubuntu and Matlab R2012a.

spezifisch commented 8 years ago

I tried your code with Matlab R2014b + opencv-3.0 on Debian Jessie with gcc 4.9 (everything 64 bit) on an Intel Core i5 system and it also crashed Matlab when running it. I noticed the following output on the console before the crash: cl_get_gt_device(): error, unknown device

So I guessed the problem might have something to do with OpenCL, therefore I uninstalled the beignet packages from my system which provide OpenCL support for Intel CPUs (I don't need them): % apt-get remove beignet beignet-dev

This solved (or worked around) the problem and Matlab no longer crashes when running your code.