function [rectangle] = pick_rec(im1)

% May 5, 1994, Radim Sara, CVL Prague
% corrected July 2, 1996 RS
% Greg mod ....


figure(gcf);		% to show up
ax = gca;		% current axis
xlim = get(ax, 'Xlim');
ylim = get(ax, 'Ylim');
[imy,imx] = size(im1);

val = [];
pts = [];
done = 0;
color = [1 0 0];
first = 1;
h = 0;

% suppress zooming
if strcmp(get(gcf,'windowbuttondownfcn'),'zoom(''down'')')
    zoom off
    zooming = 1;
else
    zooming = 0;
end

while (~done)
    [mx,my,button]=ginput(1);
    done = (button>1);
    if done; break; end
    
    mx = round(mx);
    my = round(my);
    mx = max(mx, 1);
    my = max(my, 1);
    mx = min(mx, imx);
    my = min(my, imy);
    
    % draw a circle around the last pick
    if first, first=0; else, delete(h); end
    
    pts = [pts, my,mx];
    %h = line(mx,my,'linestyle','s','erasemode','xor');
    l_x = [mx;imx];
    l_y = [my;my];
    h = line(l_x,l_y,'Color','r','LineWidth',4);
    l_x = [mx;mx];
    l_y = [my;imy];
    h = line(l_x,l_y,'Color','r','LineWidth',4);
    %h = line(l_x,l_y,'Color','r','LineWidth',4,'erasemode','xor');
    % mx = floor((mx-xlim(1))/(xlim(2)-xlim(1))*(imx)+1);
    % my = floor((my-ylim(1))/(ylim(2)-ylim(1))*(imy)+1);
    
    % get values in matrices im1, im2,...
    params=['im1(my,mx)'];
    for n=2:nargin
        params = [params, ',im', int2str(n), '(my,mx)'];
    end
    eval(['val1=[' params '];']);
    val = [val;val1];
    
    % write the current vector into the figure caption
    vstr = sprintf('%x',int2str(val1(1)));
    if length(val1) > 1
        vstr = [vstr,sprintf(', %x', int2str(val1(2:length(val1))))];
    end
    title(['pick: (', num2str(my), ', ' num2str(mx), ') = [', vstr, ']']);
    
    % only allow one window to be selected
    [num_pts,d] = size(pts);
    if d >= 4
        done = 1;
    end
end

if (~first), delete(h); end

if zooming
    zoom on
end


rectangle = pts;