Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3228720
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:43:35+00:00 2026-05-17T16:43:35+00:00

I have an image in Matlab: img = imopen(‘image.jpg’) which returns an uint8 array

  • 0

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 conventions 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T16:43:35+00:00Added an answer on May 17, 2026 at 4:43 pm

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an image in MATLAB: y = rgb2gray(imread('some_image_file.jpg')); and I want to do
I have an image in MATLAB: im = rgb2gray(imread('some_image.jpg'); % normalize the image to
I have a binary image in Matlab, and I need the binary array(0 and
I have an RGB image in MATLAB, and I want to loop through each
I've written an image processing program in MATLAB which makes heavy use of the
i have a view on which i have image view and scroll view and
I have image data and i want to get a sub image of that
I have: image 1:Many imageToTag Many:1 tag I want to issue a query that
I have an image and on it are logos (it's a map), I want
I have an image of a basic game map. Think of it as just

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.