Code covered by the BSD License  

Highlights from
Load / Import ImageJ ROI files

4.83333

4.8 | 6 ratings Rate this file 42 Downloads (last 30 days) File Size: 15.2 KB File ID: #32479
image thumbnail

Load / Import ImageJ ROI files

by

 

09 Aug 2011 (Updated )

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

| Watch this File

File Information
Description

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
Usage:
[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.

Hint:
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 (13)
11 Nov 2014 Dylan Muir

Hi Luca,

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

Dylan.

08 Sep 2014 Luca

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

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

For infos:
http://imagej.nih.gov/ij/source/ij/io/RoiDecoder.java

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

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 (http://imagej.nih.gov/ij/source/ij/io/RoiDecoder.java), but my function was incorrectly reading it as a 16-bit int instead of a 32-bit int.

Thanks for your feedback,
Dylan.

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

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,
Dylan.

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!

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?
thanks!

10 Aug 2011 Dylan Muir

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

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...

10 Aug 2011 Jean-Yves Tinevez  
Updates
16 Aug 2011

Fixed the warning about capitalised FILESEP.

16 Aug 2011

Updated screenshot

29 Aug 2011

Fixed a bug reported by Benjamin Ricca

07 Nov 2011

Updated description

29 Jun 2012

ROI order is now preserved

02 Jul 2014

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

03 Jul 2014

Updated file to read position field correctly.

20 Oct 2014

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

16 Nov 2014

Changed title

08 Dec 2014

Added paper reference.

08 Dec 2014

Text tweak.

08 Dec 2014

Text tweak.

Contact us