Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw multiple regions on an image- imfreehand

Tags:

matlab

roi

I'd like to manually draw multiple regions on an image to create a binary mask of the drawn regions (ground truth).

I attached the code to do it using imfreehand that does the job for one region, but once in release the mouse button, binary mask for that single region is displayed. Is there a way to draw multiple regions and then display the binary mask? (Calling imfreehand multiple times may not work because the number of regions varies with each image).

h= imfreehand();

h = imfreehand(gca);
setColor(h,'red');

position = wait(h); 
BW = createMask(h);
figure,imshow(BW);
axis on;

Thanks.

like image 285
user3602648 Avatar asked Dec 29 '25 12:12

user3602648


1 Answers

You can loop until you get an empty mask - this will indicate that the user finished drawing all masks.
Let sz be the desired size of the output mask, then

totMask = false( sz ); % accumulate all single object masks to this one
h = imfreehand( gca ); setColor(h,'red');
position = wait( h );
BW = createMask( h );
while sum(BW(:)) > 10 % less than 10 pixels is considered empty mask
      totMask = totMask | BW; % add mask to global mask
      % you might want to consider removing the old imfreehand object:
      delete( h ); % try the effect of this line if it helps you or not.

      % ask user for another mask
      h = imfreehand( gca ); setColor(h,'red');
      position = wait( h );
      BW = createMask( h );
end
% show the resulting mask
figure; imshow( totMask ); title('multi-object mask');
like image 96
Shai Avatar answered Jan 03 '26 07:01

Shai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!