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

13 visualizaciones (últimos 30 días)
Anon
Anon el 9 de Feb. de 2020
Editada: DGM el 29 de Abr. de 2023
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.

Respuestas (1)

Subhadeep Koley
Subhadeep Koley el 10 de Feb. de 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 comentario
DGM
DGM el 29 de Abr. de 2023
Editada: DGM el 29 de Abr. de 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.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by