File Exchange

image thumbnail

Multi ROI/Mask Editor Class

version 1.0 (5.95 KB) by

This Class allows you to easily define multiple regions of interest (binary masks) with a nice UI.

16 Ratings



View License

This is a class with a convenient user interface to define multiple region of interest (ROI) masks on a given image.

All the imroi tools (freehand, circle, rectangle, polygon) can be used to define these region(s).

Loading and storing of previously defined ROI masks to and from files is easily done via the UI toolbar.

Multiple regions are labeled using the connected component labeling method (using the MATLAB built-in bwlabel function).

Generated Information:
- Binary Mask (obj.roi)
- Label Mask (obj.labels)
- number of independent regions (obj.number)

You can listen to the object's "MaskDefined" event to retrieve the ROI information generated (obj.getROIData) or get them directly from the objects public properties.

Run the example to see how it works.

Comments and Ratings (26)

saleh sali

Gloria Bauman

Jonas Reber

Jonas Reber (view profile)

Hi all
glad you people still like this submission. I often get similar questions on how to use the tool in a classical non-object oriented kind of matlab script. The answer is rather simple: use waitfor() and wait for the roi to be defined (apply button pressed).
This code should illustrate how to work with the tool without the callback (I will add this to the submission later)

function example2

myimage = imread('eight.tif');
roiwindow = CROIEditor(myimage);

% wait for roi to be assigned
if ~isvalid(roiwindow)
disp('you closed the window without applying a ROI, exiting...');

% get ROI information, like binary mask, labelled ROI, and number of
% ROIs defined
[mask, labels, n] = roiwindow.getROIData;

% do something with the ROIs, e.g. display some statistics
for i=1:n
roi_data = double(myimage(labels==i));
std_of_roi_i = std(roi_data);
var_of_roi_i = var(roi_data);
mean_of_roi_i = mean(roi_data);
fprintf('ROI %d has a stddev of %4.2f, a variance of %4.2f and a mean of %4.2f\n',...
i, std_of_roi_i, var_of_roi_i, mean_of_roi_i);

I hope that helps.
Best, Jonas

Hello. This code is exactly what I need but I do not understand how it works.
Running the example function no output exists. Ans is an empty object. Moreover, I need to stop program execution to get ROIs since they are necessary for subsequent step. However, program continuous to run during ROI selection.


I have bug for Apply button.
Warning: Error occurred while executing the listener callback for event MaskDefined defined for class CROIEditor:
Undefined variable "roiwindow" or class "roiwindow.getROIData".

Error in example>your_roi_defined_callback (line 9)
[mask, labels, n] = roiwindow.getROIData;

Error in CROIEditor/applyclick (line 255)
notify(this, 'MaskDefined');

Error in CROIEditor>@(h,e)this.applyclick(h,e) (line 333)

Could anyone kindly suggest how to fix it?


This has been very useful for me. But I have difficulty to save it.
I would like to use it as binary ground truth images. Could anyone suggest me how to save the roi binary mask as .jpg or .mat file.


want to use this mask as jpg image, please guide on how to save


JWall (view profile)

This has been very helpful for me - thanks very much! It would be even more useful if one could load previously defined masks and edit rois individually with the gui (i.e. delete or move some of the individual rois).


Thank you!

Cheng-Je Lee


Hi, Thank you for your great job. This is really nice.

I want to make multiple polygon ROIs in an image about 56 region but they join each ROIs.

For example. I drew 28 ROIs but only 11 ROI labels exist. When I check them in image, they joined in one ROI.

Peyman Obeidy

nice job, I have the same question as Fred, how can I add my ROI into the workspace, I need to calculate the area for different mask?

Weizhi Li


fred blue


How can I add my ROI on the workspace?


Yue Hou

Hi Jonas,
There is a bug for delete button at line 183:
"this.current = str2double(regexp(Tag,'\d','match'));"
If there are more than 10 ROIs, the delete button doesn't work for ROIs #10, 11, 12, etc.
so I correct it to
"this.current = str2double(regexp(Tag,'\d+','match'));"
And it works fine, now.


gilad (view profile)

hi Nizamettin,
If I understood you, after running the example, look at the ans.roi, and save it as a variable.

How can you extract the mask and obtain it as a variable in the workspace?



hamid (view profile)

thank you for your effort. i downloaded the file but after extracting i cant open it in matlab editor. it gives me the error of
"CROIEditor\example.m is not
a valid function name. This will generate an error in a future release.
> In open at 36
In uiopen at 196 "

is there any suggestion
many thanks


Excellent, you have save me so much time!!
Really really good!


Felix (view profile)

very good!


zaidan (view profile)

nice code


Very nice tool and extremely helpfull.


Damien (view profile)

The tool itself is very convenient.
Many comments make the code easy to read.
Can also be used as a good tutorial for : GUI, Classes, imroi

I myself was in need of a more versatile tool. For exemple being able to save not only the binary mask but also the imroi shapes themself.
With such a funcionality you can reuse the shapes on another image and modify it if needed.
I implemented this funcionality in an enhanced version of your tool. I may submit it soon

Thiago Lima

Very good and easy to use.

MATLAB Release
MATLAB 7.12 (R2011a)

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

» Watch video

Win prizes and improve your MATLAB skills

Play today