MATLAB: DrawingFinished event No response

eventslistener

imshow(I,[]); % show image from image data
drawroi = drawassisted; evs = events(class(drawroi)); % event source and events list for class
% test for action:
i = 7; % drawing finished
ev = evs{i}; addlistener(drawroi,ev,@(source,data) disp(data)); % event and its listener
Hello,
I'm trying to create a response when the drawing of an ROI is finished, DrawingFinished event, by adding a listener as shown in the commands.
The other events (such as i = 9 for ROI moved, i = 2 for waypoint added, etc.) are working but not for drawing started (i = 6) and finished (i = 7).
Thanks in advance!

Best Answer

drawassisted is a simple wrapper around the image.roi.AssistedFreehand object. The function drawassisted really only constructs the ROI object and calls the draw method on the object so the user can begin drawing immediately. The problem with using drawassisted is that you can't set up listeners for the ROI object until after the object has already been drawn. For most events this isn't an issue but, as you observed, you can't set up DrawingStarted and DrawingFinished because the draw interaction will occur before you get the chance.
My suggestion would be to use the formal interface images.roi.AssistedFreehand directly:
Step 1: Construct the ROI object and parent it to the axes
I = imread('peppers.png');
imshow(I);
ax = gca;
drawroi = images.roi.AssistedFreehand('Parent',ax);
Step 2: Set up the listeners
addlistener(drawroi,'DrawingStarted',@(~,~) disp('Drawing Has Started'));
addlistener(drawroi,'DrawingFinished',@(~,~) disp('Drawing Has Finished'));
Step 3: Call draw method to begin interactively drawing the ROI
draw(drawroi);