# ROI with hole, convert back to logical

6 vues (au cours des 30 derniers jours)
William Thielicke le 1 Déc 2023
Hi,
I have a binary mask that is converted to a ROI, then edited, then converted back to a binary mask. The binary mask contains objects with holes in it. The ROI contains objects for the holes, but when converting back to binary, these holes are lost. I am looking for a simple soution for this (code is mainly from Matlab help):
A=zeros(1000,1000,'logical');
A(200:800,200:800)=1;
A(300:400,300:400)=0;
blocations = bwboundaries(A,'holes');
figure
imshow(A, []);
for ind = 1:numel(blocations)
% Convert to x,y order.
pos = blocations{ind};
pos = fliplr(pos);
% Create a freehand ROI.
drawfreehand('Position', pos);
end
% Convert edited ROI back to masks.
hfhs = findobj(gca, 'Type', 'images.roi.Freehand');
for ind = 1:numel(hfhs)
% Accumulate the mask from each ROI
% Include the boundary of the ROI in the final mask.
% Here, we have a dense boundary, so we can take the slightly more
% performant approach of just including the boundary pixels directly in
boundaryLocation = hfhs(ind).Position;
bInds = sub2ind(size(A), boundaryLocation(:,2), boundaryLocation(:,1));
end
The aim is that "editedMask" looks like "A" after these two conversions. Thanks for your help!!
##### 5 commentairesAfficher 3 commentaires plus anciensMasquer 3 commentaires plus anciens
DGM le 5 Déc 2023
Will that be okay if you have two "holes" overlap or something? Should that create one large hole with an unattached island, or should it just be one large hole? I mean, these are design decisions for you, but I think your method is one possible solution.
William Thielicke le 7 Déc 2023
Modifié(e) : William Thielicke le 7 Déc 2023
@Image Analyst: These "masks" are meant to be used in a GUI to exclude certain regions of images from analysis. These masks can be generated by the user in different ways: Drawing by hand, or importing a user-generated binary image. In both cases, I want to keep the masks editable until they are used in the final image analysis. I also want to offer some basic image processing on the imported binary mask: imdilate, imerode, imclose etc. Therefore, I think the best way is to work with Matlabs ROI objects all the time, and convert them back to binary at the very end (because a binary mask is what the final analysis expects).
I am trying to find a good solution that really helps people to define masks, but as @DGM noticed, I have to take a lot of design decisions which feels pretty difficult at the moment.
Currently, the even/uneven method seems to be compatible with imported binary masks and with masks that are drawn with polygons. I personally think the behaviour makes sense, but it is difficult to assess how others feel about it.
User input
Conversion to pixels result:
User input (binary pixel image):
Conversion back to pixels result:

Connectez-vous pour commenter.

### Catégories

En savoir plus sur ROI-Based Processing dans Help Center et File Exchange

R2023a

### Community Treasure Hunt

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

Start Hunting!

Translated by