开发者

Transferring an image from Matlab to an OpenCV IplImage

I have an image in Matlab:

img = imopen('image.jpg')

which returns an uint8 array height x width x channels (3 channels: RGB).

Now I want to use openCV to do some manipulations on it, so I write up a MEX file which takes the image as a parameter and constructs an IplImage from it:

#include "mex.h"
#include "cv.h"

void mexFunction(int nlhs, mxArray **plhs, int nrhs, const mxArray **prhs) {
    char *matlabImage = (char *)mxGetData(prhs[0]);
    const mwSize *dim = mxGetDimensions(prhs[0]);

    CvSize size;
    size.height = dim[0];
    size.width = dim[1];

    IplImage *iplImage = cvCreateImageHeader(size, IPL_DEPTH_8U, dim[2]);
    iplImage->imageData = matlabImage;
    iplImage->imageDataOrigin = iplImage->imageData;

    /* Show the openCV image */
    cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
    cvShowImage("mainWin", iplImage);
}

This result looks completely wrong, because openCV uses other conventio开发者_开发百科ns than matlab for storing an image (for instance, they interleave the color channels).

Can anyone explain what the differences in conventions are and give some pointers on how to display the image correctly?


After spending the day doing fun image format conversions </sarcasm> I can now answer my own question.

Matlab stores images as 3 dimensional arrays: height × width × color
OpenCV stores images as 2 dimensional arrays: (color × width) × height

Furthermore, for best performance, OpenCV pads the images with zeros so rows are always aligned on 32 bit blocks.

I've done the conversion in Matlab:

function [cv_img, dim, depth, width_step] = convert_to_cv(img)

% Exchange rows and columns (handles 3D cases as well)
img2 = permute( img(:,end:-1:1,:), [2 1 3] );

dim = [size(img2,1), size(img2,2)];

% Convert double precision to single precision if necessary
if( isa(img2, 'double') )
    img2 = single(img2);
end

% Determine image depth
if( ndims(img2) == 3 && size(img2,3) == 3 )
    depth = 3;
else
    depth = 1;
end

% Handle color images
if(depth == 3 )
    % Switch from RGB to BGR
    img2(:,:,[3 2 1]) = img2;

    % Interleave the colors
    img2 = reshape( permute(img2, [3 1 2]), [size(img2,1)*size(img2,3) size(img2,2)] );
end

% Pad the image
width_step = size(img2,1) + mod( size(img2,1), 4 );
img3 = uint8(zeros(width_step, size(img2,2)));
img3(1:size(img2,1), 1:size(img2,2)) = img2;

cv_img = img3;

% Output to openCV
cv_display(cv_img, dim, depth, width_step);

The code to transform this into an IplImage is in the MEX file:

#include "mex.h"
#include "cv.h"
#include "highgui.h"

#define IN_IMAGE prhs[0]
#define IN_DIMENSIONS prhs[1]
#define IN_DEPTH prhs[2]
#define IN_WIDTH_STEP prhs[3]

void mexFunction(int nlhs, mxArray **plhs, int nrhs, const mxArray **prhs) {
    bool intInput = true;

    if(nrhs != 4)
        mexErrMsgTxt("Usage: cv_disp(image, dimensions, depth, width_step)");

    if( mxIsUint8(IN_IMAGE) )
        intInput = true;
    else if( mxIsSingle(IN_IMAGE) )
        intInput = false;
    else 
        mexErrMsgTxt("Input should be a matrix of uint8 or single precision floats.");

    if( mxGetNumberOfElements(IN_DIMENSIONS) != 2 )
        mexErrMsgTxt("Dimension vector should contain two elements: [width, height].");

    char *matlabImage = (char *)mxGetData(IN_IMAGE);

    double *imgSize = mxGetPr(IN_DIMENSIONS);
    size_t width = (size_t) imgSize[0];
    size_t height = (size_t) imgSize[1];

    size_t depth = (size_t) *mxGetPr(IN_DEPTH);
    size_t widthStep = (size_t) *mxGetPr(IN_WIDTH_STEP) * (intInput ? sizeof(unsigned char):sizeof(float));

    CvSize size;
    size.height = height;
    size.width = width;

    IplImage *iplImage = cvCreateImageHeader(size, intInput ? IPL_DEPTH_8U:IPL_DEPTH_32F, depth);
    iplImage->imageData = matlabImage;
    iplImage->widthStep = widthStep;
    iplImage->imageDataOrigin = iplImage->imageData;

    /* Show the openCV image */
    cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
    cvShowImage("mainWin", iplImage);
}


You could optimize your program with mxGetDimensions and mxGetNumberOfDimensions to get the size of the image and use the mxGetClassID to determine the depth more accurately.

I wanted to do the same but I think it would be better to do this using matlab dll and calllib. I would not do the transformation of the image in opencv format not in matlab because it would be slow. This is one of the biggest problems with matlab openCV. I think opencv2.2 has some good solutions for that problem. It looks like there are some solutions like that done from opencv community for octave but I still don't understand them. They are somehow using the c++ functionality of opencv.


Try using the library developed by Kota Yamaguchi: http://github.com/kyamagu/mexopencv It defines a class called 'MxArray' that can perform all types of conversions from MATLAB mxArray variables to OpenCV objects (and from OpenCV to MATLAB). For example, this library can convert between mxArray and cv::Mat data types. Btw, IplImage is not relevant anymore if you use C++ API of OpenCV, it's better to use cv::Mat instead.

Note: if using the library, make sure to compile your mex function with MxArray.cpp file from the library; you can do so in MATLAB command line with:

    mex yourmexfile.cpp MxArray.cpp


Based on the answer and How the image matrix is stored in the memory on OpenCV, we can make it with Opencv Mat operation only!

C++: Mat::Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=0)
C++: void merge(const Mat* mv, size_t count, OutputArray dst)

Then the mex C/C++ code is:

#include "mex.h"
#include <opencv2/opencv.hpp>
#define uint8 unsigned char

 void mexFunction(int nlhs, mxArray *out[], int nrhs, const mxArray *input[])
{

    // assume the type of image is  uint8
    if(!mxIsClass(input[0], "uint8"))
    {
        mexErrMsgTxt("Only image arrays of the UINT8 class are allowed.");
        return;
    }

    uint8* rgb = (uint8*) mxGetPr(input[0]);
    int* dims = (int*) mxGetDimensions(input[0]);

    int height = dims[0];
    int width = dims[1];
    int imsize = height * width;


    cv::Mat imR(1, imsize, cv::DataType<uint8>::type, rgb);
    cv::Mat imG(1, imsize, cv::DataType<uint8>::type, rgb+imsize);
    cv::Mat imB(1, imsize, cv::DataType<uint8>::type, rgb+imsize + imsize);

    // opencv is BGR and matlab is column-major order
    cv::Mat imA[3];
    imA[2] = imR.reshape(1,width).t();
    imA[1] = imG.reshape(1,width).t();
    imA[0] = imB.reshape(1,width).t();

    // done! imf is what we want!
    cv::Mat imf; 
    merge(imA,3,imf);

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜