function ROI_gui(data1)
global handles
global data
global rectROI
global RectDraw
global setFrame
data = data1;
q = setFrame(2);
s = setFrame(1);
imNo = length(data1);
dispSize = get(0,'ScreenSize');
figSize = [800, 500];
mainW = figure('pos', [(dispSize(3)-dispSize(1))/10 ...
    (dispSize(4)-dispSize(2))/10 figSize(1) figSize(2)]);
set(mainW,'Name','Setting the region of interest', 'Color',[0.9 0.9 0.9],...
    'NumberTitle','off');
im_data = axes('Units', 'Normalized', 'Position', [.001 .1 .8 .8]);
imshow(data1{1}{q,s},[])
if ~isempty(rectROI)
    [y,x] = find(rectROI,1,'first');
    [n,m] = find(rectROI,1,'last');
    w = m-x;
    h = n-y;
    rect = [x,y,w,h];
    RectDraw = rectangle('Position',rect,'EdgeColor',[1 0 0],'LineWidth',1.5);
end
if imNo > 2
    uicontrol('Style','Text','Units','Normalized','FontUnits','Normalized', ...
        'Position', [.7 .7 .2 .03],'String','Select a scan','FontSize',0.8,...
        'BackgroundColor',[.9 .9 .9],'ForegroundColor',[0.015, 0.149, 0.250]);
    slide_im = uicontrol('Style','Slider','Units', 'Normalized','Position',[.7 .63 .2 .03],...
        'Max',imNo,'Min',1,'SliderStep',[1/(imNo-1) 1/(imNo-1)],'Value',1,...
        'CallBack',@SlideChange,'BackgroundColor',[0.015, 0.149, 0.250],...
        'ForegroundColor',[0.015, 0.149, 0.250]);
    handles.slide = struct('slide_im',slide_im);
end
uicontrol('Style','Pushbutton','Units','Normalized','Position',[.7 .5 .1 .05],...
    'String','Select ROI','CallBack',@selROI,'BackgroundColor',[0.015, 0.149, 0.250],...
    'ForegroundColor',[1 1 1])
SetROI = uicontrol('Style','Pushbutton','Units','Normalized','Position',[.7 .4 .1 .05],...
    'String','Set ROI','CallBack',@setROI,'BackgroundColor',[0.015, 0.149, 0.250],...
    'ForegroundColor',[1 1 1],'Enable','on');
uicontrol('Style','Pushbutton','Units','Normalized','Position',[.7 .3 .1 .05],...
    'String','Close Window','CallBack',@exit,'BackgroundColor',[0.015, 0.149, 0.250],...
    'ForegroundColor',[1 1 1])
handles.ROI = struct('im_data', im_data,'mainW',mainW,'SetROI',SetROI);
end

function SlideChange(hObject,eventdata)
global handles
global data
global rect
global RectDraw
global setFrame
q = setFrame(2);
s = setFrame(1);
newIm = get(handles.slide.slide_im,'Value');
imshow(data{newIm}{q,s},[])
if ~isempty(rect)
    RectDraw = rectangle('Position',rect,'EdgeColor',[1 0 0],'LineWidth',1.5);
end
end

function exit(hObject,eventdata)
global rectROI
global rectROIh
global data
global handles
global RectDraw
if isempty(rectROIh)
    ImSize = size(data{1}{1});
    rectROI = ones(ImSize);
end
clear RectDraw
delete(handles.ROI.mainW)
end

function selROI(hObject,eventdata)
global rectROIh
global handles
% clear global rectROIh
rectROIh = imrect;
% set(handles.ROI.SetROI,'Enable','on')
end

function setROI(hObject,eventdata)
global rectROIh
global rectROI
global rect
global RectDraw
if ~isempty(RectDraw)
    set(RectDraw,'EdgeColor','none')
end
rectROI = createMask(rectROIh);
[y,x] = find(rectROI,1,'first');
[n,m] = find(rectROI,1,'last');
w = m-x;
h = n-y;
rect = [x,y,w,h];
RectDraw = rectangle('Position',rect,'EdgeColor',[1 0 0],'LineWidth',1.5);
end