how do I filter noise and background objects from images?
Show older comments
i have images that have these cylindrical objects that i want removed before doing analysis? How do I remove them?
Thank you
Accepted Answer
More Answers (2)
Perhaps with bwpropfilt(____,'Eccentricity', range)
4 Comments
Matthew Worker
on 15 Jan 2023
Walter Roberson
on 15 Jan 2023
It looks to me as if you want a combination of higher eccentricty (remove the circles) and lower area (remove the large irregular blobs and the ones that are formed by several objects touching.)
Matthew Worker
on 15 Jan 2023
Image Analyst
on 15 Jan 2023
If you want a script to extract only the round blobs, try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'D45F33C9-CCA7-4815-B426-07D56A8657D7.jpeg';
folder = pwd;
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
%=======================================================================================
% Read in image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display image.
subplot(2, 2, 1);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original RGB Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
g = gcf;
g.WindowState = "maximized";
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
%--------------------------------------------------------------------------------------------------
% Create a logical image mask.
mask = rgbImage(:, :, 2) > 128;
% Get areas
props = regionprops(mask, 'Area')
allAreas = sort([props.Area])
% Display image.
subplot(2, 2, 2);
imshow(mask, []);
impixelinfo;
axis on;
caption = sprintf('Initial Mask with %d blobs', numel(allAreas));
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Get rid of blobs less than 100 pixels in area
mask = bwareaopen(mask, 100)
% Get rid of partial blobs (those touching the border).
mask = imclearborder(mask);
% Get areas
props = regionprops(mask, 'Area', 'Circularity')
allAreas = sort([props.Area])
allCircs = [props.Circularity]
% Display image.
subplot(2, 2, 3);
imshow(mask, []);
impixelinfo;
axis on;
caption = sprintf('Next Mask with %d blobs', numel(allAreas));
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Filter out non-circular blobs.
[labeledImage, numBlobs] = bwlabel(mask);
roundBlobsOnly = ismember(labeledImage, find(allCircs > 0.6));
% Display image.
subplot(2, 2, 4);
imshow(roundBlobsOnly, []);
impixelinfo;
axis on;
caption = sprintf('Final Mask with %d round-only blobs', sum(allCircs > 0.6));
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.

16 Comments
Matthew Worker
on 15 Jan 2023
Image Analyst
on 16 Jan 2023
Not sure what "extraction" means to you.
If you want to see how to extract out each blob to a small image that is the bounding box of the blobs, see my Image Processing Tutorial in my File Exchange:
Matthew Worker
on 16 Jan 2023
Image Analyst
on 16 Jan 2023
You can just multiply the mask by the image to erase the parts you don't want and then they won't get analyzed. Tell me what measurements you want to make. Most of the white parts of the mask are just background, not fluorescent blobs.
Matthew Worker
on 17 Jan 2023
Image Analyst
on 17 Jan 2023
You're confusing. At lease I'm confused. You have already segmented your image and have a mask -- that's what I understood. The white parts of the mask are the mostly black background. If you multiply by that mask, the green blobs will be erased. You can then somehow analyze what's left, which will be the dark background. If you have additional images I imagine you'd segment them also and have a mask for them also that applies only for the image it was generated for.
If you wanted the green blobs, you'd multiply by the inverse of your mask which would keep the green blobs and erase the background.
All that said, I still don't know what you really want - the background or the green blobs -- and what attributes/properties you want to measure in that/those region(s). Please clarify. Your language and descriptions are confusing and contradictory. And remember the foreground in the mask is white in MATLAB, which may be the opposite of what it is in other programs like ImageJ. At one point it seemed like you wanted to erase non-round blobs and keep only round blobs, and I showed you that. Another time (original question) it seemed like you wanted ALL black blobs in your segmented image to be erased, which would leave you only the dark background in the original picture. Just tell me what you want and be very very clear and specific.
Matthew Worker
on 17 Jan 2023
Image Analyst
on 17 Jan 2023
Edited: Image Analyst
on 17 Jan 2023
OK, this will do it but what do you want to measure from the green pixels? Just the mean?
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Get the name of the image the user wants to use.
baseFileName = 'hi.jpg';
folder = pwd;
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
%=======================================================================================
% Read in image.
rgbImage = imread(fullFileName);
% Get the dimensions of the image.
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display image.
subplot(2, 3, 1);
imshow(rgbImage, []);
impixelinfo;
axis on;
caption = sprintf('Original RGB Image\n%s', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
g = gcf;
g.WindowState = "maximized";
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
%--------------------------------------------------------------------------------------------------
% Get the green channel
grayImage = rgbImage(:, :, 2);
% Display image.
subplot(2, 3, 2);
imshow(grayImage, []);
impixelinfo;
axis on;
title('Green Channel', 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
drawnow;
%--------------------------------------------------------------------------------------------------
% Show the histogram
subplot(2, 3, 3);
imhist(grayImage);
grid on;
drawnow;
%--------------------------------------------------------------------------------------------------
% Create a logical image mask of the green parts.
lowThreshold = 14;
highThreshold = 255;
% [lowThreshold, highThreshold, lastThresholdedBand] = threshold(3, 255, grayImage);
mask = grayImage >= lowThreshold & grayImage <= highThreshold;
% Display image.
subplot(2, 3, 4);
imshow(mask, []);
impixelinfo;
axis on;
title('Initial Mask of Green Areas', 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
drawnow;
%--------------------------------------------------------------------------------------------------
% Get the mean of the green pixels
meanGreen = mean(grayImage(mask))
Matthew Worker
on 17 Jan 2023
Matthew Worker
on 17 Jan 2023
Chanille
on 25 Jan 2023
May I ask a related question?
Is there a way to COUNT the content per strip area then DIVIDE by the area in that strip?
Image Analyst
on 25 Jan 2023
@Chanille I have no idea what that means. What is the discrete, countable items that you want to count? If you can make a mask of blobs somehow, then yes you can count them with bwlabel or regionprops and then divide by the number of pixels.
[~, blobCount] = bwlabel(mask);
output = blobCount / numel(mask) % compute count density
Image Analyst
on 25 Jan 2023
Note, the area fraction = (# foreground pixels) / (total # of pixels in ROI) is not the same as the count density = (# distinct blobs) / (total # of pixels in ROI). You can get either and/or both - just depends on what will help you in your further analysis.
Categories
Find more on Image Segmentation and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!