Create and save imfreehand() positions until the user clicks a button to finish

cgenes · Mar 4, 2022 · 2K views
Question
Hi,   I want an image to open and the user to draw ROI with imfreehand() until they are finished - with the co-ordinates saved into structured array - - the following code does it for 3 regions of interest - - but how can i do this until the user is finished? Would be good to have an undo button too - which not only deletes the region of interest but also deletes xy co-ordinates in the array. Thanks!!   img = imread('myfig.jpg') imshow(img) i = 1; for i = 1:3 hFH(i) = imfreehand(); xy{i} = hFH(i).getPosition; end  
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Aug 13, 2026

You could use

 

img = imread('myfig.jpg')
imshow(img)
i = 1;
finished = 'NO';
i = 1;
while strcmpi(finished,'NO')
  hFH(i) = imfreehand();
  finished = questdlg('Finished?', ...
      'confirmation', ...
      'YES', 'NO', 'UNDO', 'NO');
  if strcmpi(finished, 'UNDO')
      delete(hFH(i))
      finished = 'NO';
  else
      xy{i}  = hFH(i).getPosition;
      i = i + 1;
  end
end

 

100% Run Guarantee 3-Hour Fast-Track Delivery

Need a Custom Version or Complete Simulation for This Problem?

Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.

Tested on MATLAB R2024b / R2026a
Turnitin 0% Plagiarism Report
Free 7-Day Revisions Guarantee
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!