How can I remove object in binary image by using a mask
古いコメントを表示
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 件のコメント
Matt J
2022 年 9 月 26 日
You should attach both your image and your mask in a .mat file, so that we can more easily demonstrate solutions.
CW Hsu
2022 年 9 月 28 日
Image Analyst
2022 年 10 月 10 日
What is it exactly about the blobs in the yellow box that you want to keep, and in the red box that you want to throw away? And what about the blobs not in a box - what to do about them? So do you want only the ring in the yellow box extracted out into it's own image? I presume that you are starting with just the binary image, not an RGB image with red and yellow pixels in it, right?
CW Hsu
2022 年 10 月 12 日
Image Analyst
2022 年 10 月 12 日
Well the code I posted below does exactly that. Did you try it? Did you even see it? Scroll down or click on this link:
Image Analyst
2022 年 10 月 13 日
Well do you want to consider "Accepting" one of the answers?
採用された回答
その他の回答 (1 件)
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');

カテゴリ
ヘルプ センター および File Exchange で Display 2-D Images についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


