ROI with hole, convert back to logical
이전 댓글 표시
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월 1일
Can't you just do a logical combination of the derived masks?
theblob = createMask(hfhs(2));
thehole = createMask(hfhs(1));
blobwithhole = theblob & ~thehole;
imshow(~blobwithhole)
Or are you asking if there's a way to automatically determine whether an ROI represents a hole or not?
William Thielicke
2023년 12월 5일
Image Analyst
2023년 12월 5일
What happened to your first (badly named) "A"? Why can't you just keep that in memory? How did it get cleared? Can you just not clear it? Or else pass it into whatever function needs the original "A"?
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
2023년 12월 7일
편집: William Thielicke
2023년 12월 7일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 ROI-Based Processing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




