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

19 visualizzazioni (ultimi 30 giorni)
roi = images.roi.AssistedFreehand
roi2 = images.roi.AssistedFreehand
mask = createMask(roi);
mask2 = createMask(roi2);
hold on
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.

Risposte (1)

Subhadeep Koley
Subhadeep Koley il 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;
roi2 = images.roi.AssistedFreehand;
mask = createMask(roi);
mask2 = createMask(roi2);
% Combine 2 masks
combinedMask = imbinarize(imadd(mask, mask2));
% Apply the filter
J = regionfill(I, combinedMask);
% Display results
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');
Hope this helps!
  1 Commento
DGM il 29 Apr 2023
Modificato: DGM il 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.

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by