ROI with hole, convert back to logical
조회 수: 3 (최근 30일)
이전 댓글 표시
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
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.
답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!