from Region of Interest analysis by Florence Kussener
This example has been developed to add capabilities in ROI analysis: add, delete, change location,

ROItool(varargin)
function varargout = ROItool(varargin)
% ROITOOL M-file for ROItool.fig
%      ROITOOL, by itself, creates a new ROITOOL or raises the existing
%      singleton*.
%
%      H = ROITOOL returns the handle to a new ROITOOL or the handle to
%      the existing singleton*.
%
%      ROITOOL('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in ROITOOL.M with the given input arguments.
%
%      ROITOOL('Property','Value',...) creates a new ROITOOL or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ROItool_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ROItool_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ROItool

% Last Modified by GUIDE v2.5 17-Nov-2006 10:26:54

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ROItool_OpeningFcn, ...
                   'gui_OutputFcn',  @ROItool_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before ROItool is made visible.
function ROItool_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ROItool (see VARARGIN)

% Choose default command line output for ROItool
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ROItool wait for user response (see UIRESUME)
% uiwait(handles.figure1);

I=imread('coins.png');
I(:,:,1)=I;
I(:,:,2)=I;
I(:,:,3)=I(:,:,1);
imshow(I)
setappdata(handles.figure1,'data',I)
% --- Outputs from this function are returned to the command line.
function varargout = ROItool_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in Create.
function Create_Callback(hObject, eventdata, handles)
% hObject    handle to Create (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[Mask,xi,yi]=roipoly;
I=getappdata(handles.figure1,'data');
I(:,:,3)=255*Mask;
imshow(I)

setappdata(handles.figure1,'mask',Mask);
setappdata(handles.figure1,'xi',xi);
setappdata(handles.figure1,'yi',yi);


% --- Executes on button press in Translate.
function Translate_Callback(hObject, eventdata, handles)
% hObject    handle to Translate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Mask=getappdata(handles.figure1,'mask');


[x,y]=ginput(2);
dx=round(x(2)-x(1));
dy=round(y(2)-y(1));
[m,n]=size(Mask);
if dx>0
    Mask=[zeros(m,dx+1) Mask];
    Mask(:,end-dx:end)=[];
else
    Mask(:,1:-dx)=[];
    Mask(:,end:end-dx)=0;
end
if dy<0
    Mask(1:-dy,:)=[];
    Mask(end:end-dy,:)=0;
else
    Mask(end-dy:end,:)=[];
    Mask=[zeros(dy+1,n);Mask];
end

I=getappdata(handles.figure1,'data');
xi=getappdata(handles.figure1,'xi');
yi=getappdata(handles.figure1,'yi');
I(:,:,3)=255*Mask;
imshow(I);
setappdata(handles.figure1,'data',I)
setappdata(handles.figure1,'mask',Mask)
setappdata(handles.figure1,'xi',xi+dx);
setappdata(handles.figure1,'yi',yi+dy);

% --- Executes on button press in Change.
function Change_Callback(hObject, eventdata, handles)
% hObject    handle to Change (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[x,y]=ginput(1);
xi=getappdata(handles.figure1,'xi');
yi=getappdata(handles.figure1,'yi');
[m,ind]=min(sqrt((xi-x).*(xi-x)+(yi-y).*(yi-y)));
hold on
plot(xi(ind),yi(ind),'ro')
hold off
[xp,yp]=ginput(1);
xi(ind)=xp;
yi(ind)=yp;
I=getappdata(handles.figure1,'data');
[Mask,xj,yj]=roipoly(I,xi,yi);
I(:,:,3)=255*Mask;
imshow(I)
setappdata(handles.figure1,'data',I);
setappdata(handles.figure1,'mask',Mask)
setappdata(handles.figure1,'xi',xi);
setappdata(handles.figure1,'yi',yi);


% --- Executes on button press in Add.
function Add_Callback(hObject, eventdata, handles)
% hObject    handle to Add (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

if get(handles.Graph,'value')==1
    [x,y]=ginput(1);
    Addfcn(x,y,handles);
elseif get(handles.Numeric,'value')==1
    prompt = {'x:','y:'};
    dlg_title = 'Add';
    num_lines = 1;
    def = {'10','10'};
    answer = inputdlg(prompt,dlg_title,num_lines,def);
    Addfcn(str2num(answer{1}),str2num(answer{2}),handles);
    pause
else
    errordlg('Please, choose an option')
end
    

% --- Executes on button press in Remove.
function Remove_Callback(hObject, eventdata, handles)
% hObject    handle to Remove (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[x,y]=ginput(1);
xi=getappdata(handles.figure1,'xi');
yi=getappdata(handles.figure1,'yi');
[m,ind]=min(sqrt((xi-x).*(xi-x)+(yi-y).*(yi-y)));
xi(ind)=[];
yi(ind)=[];
I=getappdata(handles.figure1,'data');
[Mask,xj,yj]=roipoly(I,xi,yi);
I(:,:,3)=255*Mask;
imshow(I)
setappdata(handles.figure1,'data',I);
setappdata(handles.figure1,'mask',Mask)
setappdata(handles.figure1,'xi',xi);
setappdata(handles.figure1,'yi',yi);


% --- Executes on button press in Calculate.
function Calculate_Callback(hObject, eventdata, handles)
% hObject    handle to Calculate (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


Mask=getappdata(handles.figure1,'mask');
I=getappdata(handles.figure1,'data');
M=I(:,:,1);
M=M(Mask);
moyenne=mean(M(:));
maximum=max(M(:));
minimum=min(M(:));

set(handles.Max,'string',['Max=' num2str(maximum)]);
set(handles.Mean,'string',['Mean=' num2str(moyenne)]);
set(handles.Min,'string',['Min=' num2str(minimum)]);






function Addfcn(x,y,handles)


xi=getappdata(handles.figure1,'xi');
yi=getappdata(handles.figure1,'yi');
I=getappdata(handles.figure1,'data');
[m,ind]=min(sqrt((xi-x).*(xi-x)+(yi-y).*(yi-y)));
xi1=[xi(1:ind) ;x ;xi(ind+1:end)];
yi1=[yi(1:ind); y; yi(ind+1:end)];
xi2=[xi(1:ind-1) ;x ;xi(ind:end)];
yi2=[yi(1:ind-1); y; yi(ind:end)];

d1=sum(sqrt((xi1(1:end)-[xi1(2:end);xi1(1)]).^2+(yi1(1:end)-[yi1(2:end);yi1(1)]).^2));
d2=sum(sqrt((xi2(1:end)-[xi2(2:end);xi1(1)]).^2+(yi2(1:end)-[yi2(2:end);yi2(1)]).^2));
if d1>d2
    xi=xi2;yi=yi2;
else
    xi=xi1;yi=yi1;
end
Mask=roipoly(I,xi,yi);
setappdata(handles.figure1,'mask',Mask);
I(:,:,3)=255*Mask;
setappdata(handles.figure1,'data',I);
setappdata(handles.figure1,'xi',xi);
setappdata(handles.figure1,'yi',yi);
imshow(I)


Contact us at files@mathworks.com