2

我在 linux 上使用 mcc 来构建一个使用 MATLAB 进行计算和图像处理的 C++ 应用程序。C++ 应用程序调用 MATLAB 函数并需要将 jpeg 图像缓冲区作为参数传递给 MATLAB。我尝试了使用 double、unsigned char 等的各种组合,但未能成功地将图像作为参数传递。请看下面的测试代码,让我知道需要纠正的地方:(我正在使用 ImageMagick 库来读取图像)

    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;

这是crop_img MATLAB代码:

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

    I = imgbuf;

    img = imcrop(I,a);

    imshow(img);
end
4

0 回答 0