2

I am using mcc on linux to build a C++ application that uses MATLAB for computing and image processing. The C++ application calls MATLAB functions and needs to pass a jpeg image buffer as a parameter to MATLAB. I have tried various combinations of using double, unsigned char, etc. but have not been able to successfully pass a image as a parameter. Please see below test code and let me know what needs to be corrected: ( I am using ImageMagick library to read the image )

    Blob blob1;
Image img;
img.read("test.jpg");
img.write(&blob1);
BlobBuffer buf;
buf.buffer = (void*)blob1.data();
buf.len = blob1.length();

    mwArray ret;
    double data[] = {200};
    double data1[] = {300};
    double data2[] = {10};
    double data3[] = {10};
double d1[] = {1};

mwArray imgbuf(1, buf.len, mxUINT8_CLASS, mxREAL);
 unsigned char d[buf.len];
for (int i=0; i<buf.len; i++) {
    unsigned char *dptr = (unsigned char *)buf.buffer + (i);
     d[i++] = *dptr;            
}

imgbuf.SetData(d, buf.len);
    mwArray in(1, 1, mxDOUBLE_CLASS, mxREAL);
    mwArray in1(1, 1, mxDOUBLE_CLASS, mxREAL);
    mwArray in2(1, 1, mxDOUBLE_CLASS, mxREAL);
    mwArray in3(1, 1, mxDOUBLE_CLASS, mxREAL);
    in.SetData(data, 1);
    in1.SetData(data1, 1);
    in2.SetData(data2, 1);
    in3.SetData(data3, 1);

    // create output array, and call library functions
    crop_img(1, ret, imgbuf, in, in1, in2, in3);
    cout << "cropped img:\n" << ret << endl;

Here is the crop_img MATLAB code:

 function img = crop_image(imgbuf, x, y, w, h)
    a = [ x y w h ];

    I = imgbuf;

    img = imcrop(I,a);

    imshow(img);
end

0 Answers0