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
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
CW Hsu 2022 年 9 月 28 日
Do you mean that in addition to the image file, it is better to provide the infromation of each pixel of the image through the mat file? As I do in attachment this time. Am I correct?
Image Analyst
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
CW Hsu 2022 年 10 月 12 日
Yeah, it is a binary image. I want to keep rings and remove solid object in image.
Image Analyst
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:
CW Hsu
CW Hsu 2022 年 10 月 13 日
編集済み: CW Hsu 2022 年 10 月 13 日
Yeah, I have tried it and it works successfully.
Image Analyst
Image Analyst 2022 年 10 月 13 日
Well do you want to consider "Accepting" one of the answers?

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2022 年 9 月 26 日
編集済み: Matt J 2022 年 10 月 13 日

1 投票

You can use bwlmaskpropfiltn() from this FEX download,
BW=bwconvhull(yourImage,'objects');
mask=bwlmaskpropfiltn(BW,mask,'MaskedAbsArea',[1,inf]); %EDIT
yourImage=yourImage.*mask;

7 件のコメント

CW Hsu
CW Hsu 2022 年 9 月 28 日
@Matt J Thanks a lot. Your suggestion is really helpful. Really thanks for your help. This is definitely what I want. I get this image in the second line of your code above after using bwlmaskpropfiltn()
But I have a question about the code you gave in your answer above.
The third line, why do we have to multiply the original image? Is there something wrong if we stop at the second line?
Matt J
Matt J 2022 年 9 月 28 日
編集済み: Matt J 2022 年 9 月 28 日
In that image, the objects have no holes. Your post does not mention a desire to have the holes sealed.
CW Hsu
CW Hsu 2022 年 9 月 30 日
Sorry for my unclear question.
My purpose is using mask to save objects with holes in the first image.
Matt J
Matt J 2022 年 9 月 30 日
That should be what my code gives you if you run all three lines.
I'm confused why you think the first two lines would be sufficient. Did you look at that image? I don't think it contains what you want.
CW Hsu
CW Hsu 2022 年 10 月 10 日
編集済み: CW Hsu 2022 年 10 月 10 日
Actually, I didn't follow your program exactly because I got an error message when using your program.
I use bwlmaskpropfiltn(). And there is no "MaskedArea" in bwlmaskpropfiltn(). What I use is "MaskedAbsArea".
Will the results of your three line programs you give me end up like the image below?
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)
CW Hsu
CW Hsu 2022 年 10 月 14 日
Yeah, definetly. Thanks for your help.

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 10 月 10 日

2 投票

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');

質問済み:

2022 年 9 月 26 日

コメント済み:

2022 年 10 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by