ROI with hole, convert back to logical

3 visualizzazioni (ultimi 30 giorni)
William Thielicke
William Thielicke il 1 Dic 2023
Hi,
I have a task that has probably been asked before, but I do not find something about this issue.
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');
editedMask = false(size(A));
for ind = 1:numel(hfhs)
% Accumulate the mask from each ROI
editedMask = editedMask | hfhs(ind).createMask();
% Include the boundary of the ROI in the final mask.
% Ref: https://blogs.mathworks.com/steve/2014/03/27/comparing-the-geometries-of-bwboundaries-and-poly2mask/
% Here, we have a dense boundary, so we can take the slightly more
% performant approach of just including the boundary pixels directly in
% the mask.
boundaryLocation = hfhs(ind).Position;
bInds = sub2ind(size(A), boundaryLocation(:,2), boundaryLocation(:,1));
editedMask(bInds) = true;
end
The aim is that "editedMask" looks like "A" after these two conversions. Thanks for your help!!
  5 Commenti
DGM
DGM il 5 Dic 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
William Thielicke il 7 Dic 2023
Modificato: William Thielicke il 7 Dic 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.
Polygon mask behaviour:
User input
Conversion to pixels result:
Pixel mask behaviour:
User input (binary pixel image):
Conversion from pixel mask to polygon mask (editable):
Conversion back to pixels result:

Accedi per commentare.

Risposte (0)

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by