how can I remove the background this image , I do thresholding but I can't remove the background( I want only the dark object and determine their location) ?

15 visualizaciones (últimos 30 días)
<<
>>
  1 comentario
Jan
Jan el 10 de Mzo. de 2018
Editada: Rena Berman el 21 de Mzo. de 2018
Please use flags only to inform admins and editors about inappropriate contributions, e.g. spam or rudeness.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 4 de Mzo. de 2018
Try this:
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 = 20;
% Check that user has the specified Toolbox installed and licensed.
hasLicenseForToolbox = license('test', 'image_toolbox'); % license('test','Statistics_toolbox'), license('test','Signal_toolbox')
if ~hasLicenseForToolbox
% User does not have the toolbox installed, or if it is, there is no available license for it.
% For example, there is a pool of 10 licenses and all 10 have been checked out by other people already.
ver % List what toolboxes the user has licenses available for.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?');
reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes');
if strcmpi(reply, 'No')
% User said No, so exit.
return;
end
end
%===============================================================================
% Read in gray scale demo image.
folder = pwd; % Determine where demo folder is (works with all versions).
baseFileName = 'bio1.jpg';
% Get the full filename, with path prepended.
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
rgbImage = imread(fullFileName);
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
hp = impixelinfo();
% 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(rgbImage);
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 3); % Take blue channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
% Display the histogram
subplot(2, 2, 2);
imhist(grayImage);
grid on;
title('Histogram of Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% 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;
% Define a threshold.
thresholdValue = 85;
% Put up line over histogram at that level
line([thresholdValue, thresholdValue], ylim, 'Color', 'r', 'LineWidth', 3);
% Get binary image.
binaryImage = grayImage <= thresholdValue;
% Fill holes.
binaryImage = imfill(binaryImage, 'holes');
% Get rid of blobs less than 500 pixels in area
binaryImage = bwareaopen(binaryImage, 500);
subplot(2, 2, 3);
imshow(binaryImage, []);
caption = sprintf('Binary Image Using Threshold of %d', thresholdValue);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
hold on;
% Next step, separate blobs using
% https://blogs.mathworks.com/steve/2013/11/19/watershed-transform-question-from-tech-support/
% NOTE: I AM NOT DOING THIS. THIS IS LEFT TO THE USER TO IMPLEMENT.
% Now measure areas of blobs in the binary image.
props = regionprops(binaryImage, 'Area');
allAreas = [props.Area]
% Show histogram of blob areas.
subplot(2, 2, 4);
histogram(allAreas);
grid on;
title('Histogram of Cell Areas', 'FontSize', fontSize, 'Interpreter', 'None');
  6 comentarios
Kimo Kalip
Kimo Kalip el 13 de Jun. de 2018
Is there some sort of mathematical derivation for the best threshold to use when you're getting those binary images? I feel like the histogram is the key to a lot of my problems, but I'm not seeing how it connects exactly - what is the histogram representing?
Image Analyst
Image Analyst el 13 de Jun. de 2018
Sorry, I've lost the context of the discussion because Matthew unkindly removed all of his comments and questions. You should start your own question with your own image, but only if you will not do what Matthew did (that does not leave us with a nice impression of the poster).

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Image Processing and Computer Vision en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by