Passing byte array to Matlab as if read by imread

6 views (last 30 days)
Possibly a very naive question, so please excuse me if so...
We are wrapping some Matlab functions in a C-callable DLL. The calling application already has images (as bitmap) loaded into memory (as byte arrays). I want to pass an image (as a byte array) to my exported Matlab function so that it can continue to use it internally as if it was read from imread(). How is this done?
So basically:
im = imread('c:\myimage.jpg')
I want to pass 'im' into my Matlab function from C/C++ (I don't want to have to save the bytes to disk and load them using imread since I already have them all in memory). Is simply passing in the raw bytes in mxArray good enough (including the image header bytes)?

Accepted Answer

dpb
dpb on 2 Oct 2013
That's all there is -- there's no header info returned by imread
Presuming the image format is one w/ 8-bit or less resolution then the return is uint8 otherwise of course it's uint16.
Whether that's "good enough" on the C side all depends on what it needs/expects. If you need the data that's in the header; it'll have to be handled on its own; this wouldn't/won't get it there.

More Answers (1)

Ashish Uthama
Ashish Uthama on 2 Oct 2013
Edited: Ashish Uthama on 2 Oct 2013
I assume you are using the MATLAB compiler to wrap MATLAB functions into a C-callable DLL. Have a look at this example which shows how to create an mxArray from C data and pass to the DLL.
In your example, the variable im will have decompressed image as an array. So, yes, creating an mxArray should be good enough provided what you say is a 'byte array' contains the decompressed image. (I did not get what you mean by 'image header bytes'.)
  2 Comments
Doug Durham
Doug Durham on 2 Oct 2013
Yes, I am using the MATLAB compiler.
Thanks for your response. So I guess the root of my question becomes: is simply serializing the image as a byte array (image headers and all) and passing that in what MATLAB expects?
What I mean by expects is binary-compatible to what is returned from imread() for the same image but on disk.
dpb
dpb on 2 Oct 2013
Edited: dpb on 2 Oct 2013
No. As said above, imread returns only the actual image data; any header information stored in the image file on disk is not in the returned array.
doc imread % for details
Also as said above, what you need/expect on the C side will be totally determined by what your C function is trying to do with the image data.
ADDENDUM:
Oh, on rereading I see I was speaking backwards -- was thinking you were loading in Matlab and passing to C instead of vice versa.
In the latter case, then it depends on what your C function is reading -- if it's only the image data w/o the header, that's what is equivalent to the return from imread.
NB however: the internal storage order in C is row-major whereas it is column-major in Matlab. Hence unless you do the rearrangement the interpretation will be the transpose.

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!