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

7 views (last 30 days)
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 Comments
CW Hsu
CW Hsu on 13 Oct 2022
Edited: CW Hsu on 13 Oct 2022
Yeah, I have tried it and it works successfully.
Image Analyst
Image Analyst on 13 Oct 2022
Well do you want to consider "Accepting" one of the answers?

Sign in to comment.

Accepted Answer

Matt J
Matt J on 26 Sep 2022
Edited: Matt J on 13 Oct 2022
You can use bwlmaskpropfiltn() from this FEX download,
BW=bwconvhull(yourImage,'objects');
mask=bwlmaskpropfiltn(BW,mask,'MaskedAbsArea',[1,inf]); %EDIT
yourImage=yourImage.*mask;
  7 Comments
Matt J
Matt J on 10 Oct 2022
Edited: Matt J on 13 Oct 2022
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)

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 10 Oct 2022
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!