how to detect objects in a box in a greyscale image

3 vues (au cours des 30 derniers jours)
Pravindkumaran Baskaran
Pravindkumaran Baskaran le 28 Sep 2022
how can i detect objects in a box in a greyscale image.Managed to make the objects standout but how can i detect the number of objects. Attached two images, original image and the binarised image.

Réponse acceptée

Image Analyst
Image Analyst le 28 Sep 2022
I'd use imabsdiff to find the difference between an image with no objects there and your image with objects there. Then threshold that difference image, call imfill, and then count blobs with bwlabel. Here's a start
diffImage = imabsdiff(grayImage, backgroundImage);
mask = diffImage > 10; % Or whatever.
mask = imfill(mask, 'holes');
% Get rid of specks less than 50 pixels in area
mask = bwareaopen(mask, 50);
[labeledImage, numObjects] = bwlabel(mask);
If you need more help, attach your reference background image - the one with no objects present.
  3 commentaires
Image Analyst
Image Analyst le 29 Sep 2022
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 = 18;
% Read in filled image.
baseFileName = 'example_filled2.jpg'
folder = pwd;
fullFileName = fullfile(folder, baseFileName)
%===============================================================================
% Check if file exists.
if ~isfile(fullFileName)
% 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);
% 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(:, :, 1); % Take red channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
% Display the image.
subplot(3, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
%===============================================================================
% Read in filled image.
baseFileName = 'example_empty.jpg'
folder = pwd;
fullFileName = fullfile(folder, baseFileName)
% Check if file exists.
if ~isfile(fullFileName)
% 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);
% 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.
emptyImage = rgbImage(:, :, 1); % Take red channel.
else
emptyImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
% Display the image.
subplot(3, 2, 2);
imshow(emptyImage, []);
title('Empty Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
%------------------------------------------------------------------------------
% 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;
diffImage = imabsdiff(grayImage, emptyImage);
% Display the image.
subplot(3, 2, 3);
imshow(diffImage, []);
impixelinfo
title('Difference Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
% Display the histogram
subplot(3, 2, [4, 6]);
imhist(diffImage);
grid on;
title('Histogram of Difference Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Binarize the image.
mask = diffImage > 24; % Or whatever.
mask = imfill(mask, 'holes');
% Get rid of specks less than 50 pixels in area
mask = bwareaopen(mask, 50);
[labeledImage, numObjects] = bwlabel(mask);
% Display the mask.
subplot(3, 2, 5);
imshow(mask, []);
caption = sprintf('Mask with %d objects', numObjects);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
msgbox('Done!');
One problem is that you're not using the same exposure for both images. You can't have the camera in auto mode where it changes the exposure depending on what is in the image. It must be in manual mode and have the same exposure for both snapshots. Then things will subtract to zero if they are the same.
Pravindkumaran Baskaran
Pravindkumaran Baskaran le 29 Sep 2022
Thank you very much! managed to solve it

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Deep Learning for Image Processing dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by