How can I remove object in binary image by using a mask

10 次查看(过去 30 天)
Hi
I have an image below:
The object with a hole in yellow box is what I want to keep. The object without a hole in a red box is what I want to remove.
And I have a mask below. I use imcomplement() and imclearborder() to get this image.
But I don't really know how to use the mask to remove the object I don't want in the top image.
The number of objects in the two images is not the same. Dose function ismember() work for this condition?
  7 个评论
CW Hsu
CW Hsu 2022-10-13
编辑:CW Hsu 2022-10-13
Yeah, I have tried it and it works successfully.

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2022-9-26
编辑:Matt J 2022-10-13
You can use bwlmaskpropfiltn() from this FEX download,
BW=bwconvhull(yourImage,'objects');
mask=bwlmaskpropfiltn(BW,mask,'MaskedAbsArea',[1,inf]); %EDIT
yourImage=yourImage.*mask;
  7 个评论
Matt J
Matt J 2022-10-10
编辑:Matt J 2022-10-13
Here's what I get when I run it. Is this not what you want?
yourImage=load('bw_clear.mat').bw_clear;
mask=load('bw_mask.mat').bw_mask;
BW=bwconvhull(yourImage,'objects');
mask=bwlmaskpropfiltn(BW,mask,'MaskedAbsArea',[1,inf]);
yourImage=yourImage.*mask;
imshow(yourImage)

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2022-10-10
If you want blobs with holes in them, then you want to have regionprops measure the Euler Number of the blobs.
Try this well commented step-by-step demo:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 22;
markerSize = 40;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = [];
baseFileName = 'rings.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the blue channel.
grayImage = grayImage(:, :, 3);
end
% Update the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
%--------------------------------------------------------------------------------------------------------
% Threshold the image to get the bright blobs.
% https://www.mathworks.com/matlabcentral/fileexchange/29372-thresholding-an-image?s_tid=srchtitle
lowThreshold = 128;
highThreshold = 255;
% [lowThreshold, highThreshold, lastThresholdedBand] = threshold(lowThreshold, highThreshold, grayImage)
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
subplot(2, 2, 2);
imshow(mask)
title('Binary Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
%--------------------------------------------------------------------------------------------------------
% We want only blobs that have a hole in them
props = regionprops(mask, 'EulerNumber')
eulerNumbers = [props.EulerNumber]
% Extract only blobs that have an EulerNumber of 0 or less. These will be rings:
labeledImage = bwlabel(mask);
ringsOnly = ismember(labeledImage, find(eulerNumbers <= 0));
subplot(2, 2, 3);
imshow(ringsOnly)
title('Blobs With Holes Only', 'FontSize', fontSize, 'Interpreter', 'None');
% Extract only blobs that have an EulerNumber of 1. These will be solid blobs, NOT rings:
labeledImage = bwlabel(mask);
solidOnly = ismember(labeledImage, find(eulerNumbers >= 1));
subplot(2, 2, 4);
imshow(solidOnly)
title('Solid Blobs Only', 'FontSize', fontSize, 'Interpreter', 'None');

Community Treasure Hunt

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

Start Hunting!

Translated by