How can I draw on two ROI to create a mask on the same image?

20 views (last 30 days)
roi = images.roi.AssistedFreehand
draw(roi)
roi2 = images.roi.AssistedFreehand
draw(roi2)
mask = createMask(roi);
mask2 = createMask(roi2);
imshow(mask);
imshow(mask2);
%imshow(BW)
J=regionfill(I,mask)
H=regionfill(I,mask2)
figure
hold on
imshow(J)
imshow(H)
hold off
I want to draw two separate regions of interest within the same image to create a mask. Then use this mask with the regionfill function. However, I am able to draw two separate ROI with this script but it does not let me combine the two together as only one region is currently being filled.

Answers (1)

Subhadeep Koley
Subhadeep Koley on 10 Feb 2020
You can easily combine those two masks using the imadd() function. Refer the code below
close all ; clc;
I = imread('cameraman.tif');
figure; imshow(I, []);
roi = images.roi.AssistedFreehand;
draw(roi);
roi2 = images.roi.AssistedFreehand;
draw(roi2);
mask = createMask(roi);
mask2 = createMask(roi2);
% Combine 2 masks
combinedMask = imbinarize(imadd(mask, mask2));
% Apply the filter
J = regionfill(I, combinedMask);
% Display results
figure;
subplot(2, 2, 1); imshow(mask); title('Mask 1');
subplot(2, 2, 2); imshow(mask2); title('Mask 2');
subplot(2, 2, 3); imshow(combinedMask); title('Combined Mask');
subplot(2, 2, 4); imshow(J, []); title('Region filled image');
combinedMask.png
Hope this helps!
  1 Comment
DGM
DGM on 29 Apr 2023
Edited: DGM on 29 Apr 2023
I see no good reason to use imadd() and imbinarize() to calculate the union of logical-class masks.
combinedMask = mask | mask2;
If the rationale is that imadd() is safer because it will allow for the combination of binarized images of mixed class (logical and/or numeric), it's not. Tools such as imadd() have restrictions on what particular combinations of classes are allowed, and what order they're allowed in. Generally speaking imadd(), etc are not class-agnostic.
If you wanted something that is class-agnostic, you might do something like this
combinedMask = imbinarize(mask) | imbinarize(mask2);
... but in this specific example, it's unnecessary. The masks come straight from createMask(), and will always be logical-class.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!