View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

4.9 | 10 ratings Rate this file 35 Downloads (last 30 days) File Size: 14.7 KB File ID: #32479 Version: 1.16
image thumbnail



Dylan Muir (view profile)


09 Aug 2011 (Updated )

Read ROIs and ROI sets saved from ImageJ into MATLAB, without java.

| Watch this File

File Information

If this code is useful to your (academic) work, please cite the publication in lieu of thanks:
Muir and Kampa, "FocusStack and StimServer: A new open source MATLAB toolchain for visual stimulation and analysis of two-photon calcium neuronal imaging data". Frontiers in Neuroinformatics (accepted).
ReadImageJROI - FUNCTION Read an ImageJ ROI into a MATLAB structure
[sROI] = ReadImageJROI(strFilename)
[cvsROIs] = ReadImageJROI(cstrFilenames)
[cvsROIs] = ReadImageJROI(strROIArchiveFilename)
This function reads the ImageJ binary ROI file format.
'strFilename' is the full path to a '.roi' file. A list of ROI files can be passed as a cell array of filenames, in 'cstrFilenames'. An ImageJ ROI archive can be access by providing a '.zip' filename in 'strROIArchiveFilename'. Single ROIs are returned as matlab structures, with variable fields depending on the ROI type. Multiple ROIs are returned as a cell array of ROI structures.
The field '.strName' is guaranteed to exist, and contains the ROI name (the filename minus '.roi').

The field '.strType' is guaranteed to exist, and defines the ROI type: {'Rectangle', 'Oval', Line', 'Polygon', 'Freehand', 'Traced', 'PolyLine', 'FreeLine', 'Angle', 'Point', 'NoROI'}.

The field '.vnRectBounds' is guaranteed to exist, and defines the rectangular bounds of the ROI: ['nTop', 'nLeft', 'nBottom', 'nRight'].

The field '.nVersion' is guaranteed to exist, and defines the version number of the ROI format.

Other structure fields are described in the function help text.

ROIs returned by this function can be converted into useful regions using the bwconncomp function, but this requires some extra work to draw each ROI into a matrix.

MATLAB release MATLAB 7.9 (R2009b)
MATLAB Search Path
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (18)
22 Jun 2016 Li He

Li He (view profile)

20 Apr 2016 Paul

Paul (view profile)

It works well.

% Convert ImageJ ROI to a mask.
% 256 is image size.

mask = poly2mask(sROI.mnCoordinates(:,1),sROI.mnCoordinates(:,2), 256, 256);

17 Apr 2016 Petr Znamenskiy  
05 May 2015 Dylan Muir

Dylan Muir (view profile)

Hi Xen,

For freehand ROIs with more than one selection, the ROI shape (strType) is set to "Rectangle" and the ROI subtype (strSubtype) is set to "Shape". Then all the shape coordinates are returned in a vector "vfShapes".

I hope that helps.

Best regards,

Comment only
04 May 2015 Xen

Xen (view profile)

Excellent work Dylan. Thanks.
I wrote a code to generate to masks based on freehand ROIs I have. The first problem I encountered was that the masks were larger than the corresponding ones created with the fill tool in ImageJ; extra pixels were on the top and left of the region. I partially solved this by finding and removing these pixels, but when the ROI has very thin internal bulges the extra pixels remain. I am new to MATLAB and my code is simple, but maybe there is proper way to do it.
The second problem is that when the ROI consists of more than 1 selections/areas then the function recognises it as a rectangle instead of freehand, and thus coordinates are missing. I can't solve this!

11 Nov 2014 Dylan Muir

Dylan Muir (view profile)

Hi Luca,

Information from the "Header 2" field is now read in by ReadImageJROI, in the latest update.


Comment only
08 Sep 2014 Luca

Luca (view profile)

Seems Working!
Now you have also channel, slice and frame field in your structure.

Adding it form line 227
hdr2Offset=fread(fidROI,1, 'uint32');
if hdr2Offset>0 && ~fseek(fidROI, hdr2Offset+image_size+4, 'bof')
fseek(fidROI, hdr2Offset+c_position, 'bof');, 1, 'uint32');
sROI.slice=fread(fidROI, 1, 'uint32');
sROI.frame=fread(fidROI, 1, 'uint32');

For infos:

26 Aug 2014 Kenneth

This is great, thanks for making it! I just started using it so haven't put it through its paces. So far so good!

02 Jul 2014 Dylan Muir

Dylan Muir (view profile)

Hi Yousef and Samuel,

Sorry that I missed your comments. I've just uploaded a new version that correctly reads the position field, and returns it for each ROI. Please check and see if it works for you.

Position should be at byte 56 (, but my function was incorrectly reading it as a 16-bit int instead of a 32-bit int.

Thanks for your feedback,

Comment only
12 Jun 2014 Yousef Mazaheri

I would like to follow up on the comment by Samuel - the value of nPosition is important, but fseek(fidROI, 58, 'bof'); doesn't seem correct - any suggestions. thanks Yousef

22 Apr 2014 Samuel Barnes

There appears to be a small bug in reading part of the header. nPosition is read from bytes 57-58 when it should be read from 58-59. Since I don't know what is missing upstream an easy fix is to add the code:
% - Seek to get nPosition (slice)
fseek(fidROI, 58, 'bof');
nPosition = fread(fidROI, 1, 'int16');

This value is not returned but I would suggest returning it as it gives the slice number which is quite important. As far as I can tell it appears to be present in either version >223 or >224 (223 doesn't have it and 225 does).

04 Nov 2013 Dylan Muir

Dylan Muir (view profile)

Hi Andreas,

I think you may have an older version of the function. The current version on this page imports the ROIs with the correct location.

Thanks for your feedback,

Comment only
01 Nov 2013 Andreas Clemmensen

Hi! I've used this script to import some free-hand drawn ROIs into MATLAB, but couldn't get them to match the image they where originally drawn on in imageJ.

It seems that there is a small bug in line 363 and 364, which should read:
vnX = vnX + sROI.vnRectBounds(2);
vnY = vnY + sROI.vnRectBounds(1);
The convention of top and left bounds are opposite of MATLABs.

Thanks for a great script - excellent work!

26 Apr 2013 Zachary

Nevermind! Figured out the problem. I altered slightly how the transformation was done in respect to vnRectBounds and it seems to work flawlessly now!

Thanks for the script!

Comment only
14 Mar 2013 lobster soup

Can you let me know how I can access the xy coordinates of the roi so I can draw it?
when I say
[roilist] = ReadImageJROI(roiDirectoryCell)

I get a struct within a cell. The only information i can find to reconstruct the roi is vnRectBounds, and I don't understand how to make use of it.
The roi I'm trying to draw is of type 'oval'. could you please explain this in some more detail?

Comment only
10 Aug 2011 Dylan Muir

Dylan Muir (view profile)

Thanks for the comment; I must have that warning switched off! I'll fix it next version.

Comment only
10 Aug 2011 Jean-Yves Tinevez

Does what it says, and does it right. The function is well documented and cover all ROI types well.
There is just a minor warning coming from line 157, because of the capitalized FILESEP...

Comment only
10 Aug 2011 Jean-Yves Tinevez  
16 Aug 2011 1.1

Fixed the warning about capitalised FILESEP.

16 Aug 2011 1.2

Updated screenshot

29 Aug 2011 1.5

Fixed a bug reported by Benjamin Ricca

07 Nov 2011 1.6

Updated description

29 Jun 2012 1.8

ROI order is now preserved

02 Jul 2014 1.9

Different way of reading zip file contents guarantees that the order of ROIs is always preserved

20 Oct 2014 1.12

Added support for reading information from "header 2" metadata.

16 Nov 2014 1.13

Changed title

08 Dec 2014 1.14

Added paper reference.

08 Dec 2014 1.15

Text tweak.

08 Dec 2014 1.16

Text tweak.

19 Apr 2016 1.11

Updated file to read position field correctly.

19 Apr 2016 1.16

Removed GNU license statement

Contact us