MATLAB: Cancelling call to imfreehand before shape is drawn

guiimage processingImage Processing Toolboximfreehand

Hello,
I am making a gui for generating ground truth from a series of images. As one of several available tools, I want the user to be able to click a button, then freely select a region to add or subtract from the ground truth. Currently, I am using imfreehand, with the following callback function allowing the user to select a region and have it added to the ground truth.
% --- Enables Manual Selection of Regions
function buttonadd_Callback(hObject, eventdata, handles)
global truth;
fig = get(handles.imageaxes,'children'); % Get the image object's handle
set(fig,'HitTest','on');
h = imfreehand(handles.imageaxes); % Let the user select the shape
BW = createMask(h);
truth = max(truth,BW); % Add the region to the ground truth
delete(h); % Remove the polygon object as it is no longer required
set(fig,'HitTest','off');
UpdatePlot(fig); % Function for updating the axes to reflect the addition of ground truth
My issue is that I want to be able to have the user be able to cancel the imfreehand generation by having the button as a toggle, this would also let me have the user be able to select multiple regions without having to click the button every time. However, there doesn't seem to be a way to cause imfreehand to end before the user selects anything. Is there a way around this? I'm not particularly attached to this method so if there is another way to let the user select a freehand region which can be cancelled I would be more than happy to use that.

Best Answer

What is the value of h.GetPosition if the user immediately double-left-clicks? If it's a 2 element array, the user didn't draw anything and you can then assume he wants to quit.
% User draws curve on image here.
hFH = imfreehand();
% Get the xy coordinates of where they drew.
xy = hFH.getPosition
if numel(xy) == 2
% User wants to quit
end