Convert ImageLib image to Matlab and vice-versa

Hi guys!

I’m doing some basic processing using ImageJ and MATLAB. In a MATLAB script I call the ImageJ to load the image and later perform some processing, for example:

im = ij.ImagePlus(fn);
im_processor = im.getProcessor();

But now I would like to bring the im which is of type ImageLib to MATLAB data type. How can I do that?
I saw that in Fiji’s Scripts folder there is a file called copytoMatlab.m, but it only works with ImgLib2 data type.

Does any body know how to convert a ImageLib to MATLAB data file? I also would like to convert a MATLAB image (let’s say a uint8) to ImageLib

Any thoughts?


Take this advice with a grain of salt since I don’t have matlab in front of me.

In your code, im_processor should be of type ij.process.ImageProcessor, so one of these may get your image data into an array format (which matlab should be happy with).

farray = im_processor.getFloatArray()
iarray = im_processor.getIntArray()

If you’re using ImageJ2 then you will indeed want to go from a matlab array to an ImgLib2 data structure though, and that’s a different story. You could try:

import net.imglib2.img.array.ArrayImgs;
my_imglib2_img = ArrayImgs.doubles( my_matlab_array(:), size( my_matlab_array ));

but again, grain of salt, since I can’t test this at the moment. :worried:

Let me know if this helps, or if I misunderstood something,


Also, these are some good resources, but I’m not so familiar with them:

@bogovicj thank you… I’ll try this and let you know if it works or not.


@bogovicj, sorry for the delay…

I tested your suggestions and the first part (to converting ImagePlus to MATLAB) sort of worked…
Sort of, because the recieved image must to be flipped and rotated:

im = ij.ImagePlus(fn);;

im_processor = im.getProcessor();

im_array = uint8(im_processor.getFloatArray());
im_conv = fliplr(rot90(im_array,-1));
figure, imshow(im_conv);

In the second part I could get the ArrayImg but I couldnt convert it to ImageLib.
Anyway, thank you very much for your help @bogovicj.

Does anyone have some idea to convert an ArrayImg to ImageLib?

No problem, glad you figured out the flip/rotation necessary.

What do you mean by ImageLib?
ArrayImg is an Imglib2 datastructure - I assumed that’s what you meant.
Do you mean this this imagelib?