ROI with hole, convert back to logical

조회 수: 2 (최근 30일)
William Thielicke
William Thielicke 2023년 12월 1일
편집: William Thielicke 2023년 12월 7일
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
DGM
DGM 2023년 12월 5일
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 2023년 12월 7일
편집: William Thielicke 2023년 12월 7일
@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:

댓글을 달려면 로그인하십시오.

답변 (0개)

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by