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

Illustration
cgenes - 2022-03-04T10:53:25+00:00
Question: Create and save imfreehand() positions until the user clicks a button to finish

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 answered . 2025-11-20

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

 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!