Counting Colored Objects in an Image

Hello, I need help creating a code that can count and keep track of red and green colors in an image. Every time the program detects a red or green object it will put it a vector to be stored. If anyone can help me with this I would appreciate it because I am a novice to Matlab programming. Thank you for your time.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 15 Okt. 2013

1 Stimme

See my File Exchange for color segmentation demos: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
Also check the File Exchange where there are lots of tracking demos, like video tracking of red lasers or red balls, etc.

17 Kommentare

Cady
Cady am 15 Okt. 2013
Is there a way to set a number value when a color is identified? For example, If red is identified by the program it outputs the number 2
Image Analyst
Image Analyst am 15 Okt. 2013
Yes. You can detect each color that you want and get a binary image for each color. Then multiply each binary image by a number that you want to label those pixels with.
Cady
Cady am 15 Okt. 2013
can you show a basic example
% Detect blue and get a binary image.
% Then detect white and get a binary image.
% Then detect brown and get a binary image.
% Now create my custom classified image
classifiedImage = 1 * int32(blueBinary) + 2 * int32(whiteBinary) + 3 * int32(brownBinaryImage);
Cady
Cady am 16 Okt. 2013
Thank you very much. Can you go into more detail on how to detect blue and get a binary image?
Image Analyst
Image Analyst am 16 Okt. 2013
Well I did, in the File Exchange demos. Did you run any of them, for example the delta E one? You can draw out a blue area and it will find all blue areas in the image. It does go into a lot of detail and it's very well commented.
Cady
Cady am 17 Okt. 2013
I'm sorry for bothering you like this. I'm not very fluent with Matlab and did not understand your comment of detect blue and get a binary image. Can you please show an example of this?
Image Analyst
Image Analyst am 17 Okt. 2013
I'm not there with you so all I can say is to run my color demos, like the delta E one. Manually draw some region of the standard pepper image in the blue background and see it find the binary image that defines where all the blue pixels are.
Cady
Cady am 17 Okt. 2013
Is there a way to do this without manually drawing some region and finding the binary image?
Image Analyst
Image Analyst am 18 Okt. 2013
Sure - you can figure out thresholds and just threshold the appropriate color channel(s) with the appropriate threshold(s). Thresholding can be manual or automatic.
Cady
Cady am 22 Okt. 2013
Which one of your demo best displays figuring out thresholds and thresh-holding appropriate color channels?
Image Analyst
Image Analyst am 23 Okt. 2013
You have to figure it out by looking at the histograms and 3D gamut and deciding. There is no best way. You can use Otsu, like in bwthresh, but there's no guarantee that gives you a good threshold. You might have to develop a custom algorithm that works with your kind of images.
Cady
Cady am 24 Okt. 2013
Bearbeitet: Cady am 24 Okt. 2013
This is the photo that I am using. Its a front view CAD drawing of a newsstand. In the image, the dark grey squares are representative of products that would be on their designated shelves. The green and red colors are tags that would be behind products. If green is shown, 2 products are missing. If red is shown, 3 products are missing. I want to identify this in the photo and put them in a vector: product 1 would be the first position in the vector and so on. Can you please help me with the code that I currently have.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Read in a standard MATLAB color demo image.
folder = 'C:\Users\FIQ\Documents\Temporary';
baseFileName = 'FrontNewsStand.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
I = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(I);
% Display the original color image.
subplot(4, 3, 1);
imshow(I);
axis on;
hold on;
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = I(:, :, 1);
greenChannel = I(:, :, 2);
BinaryRed = redChannel > 100;
BinaryGreen = greenChannel > 100;
classifiedImageG = 1 * int32(BinaryGreen);
classifiedImageR = 1 * int32(BinaryRed) ;
Demand_Vector = [0 0 0 0 0 0 0 0 0];
%Cropping of Row 1, Column 1
I1=imcrop(I,[20 7 170 165]);
subplot(4, 3, 4);
imshow(I1);
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(1) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(1) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(1) = 0;
end
%Cropping of Row 1 Column 2
I2=imcrop(I,[185 7 185 165]);
subplot(4, 3, 5);
imshow(I2);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(2) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(2) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(2) = 0;
end
%Cropping of Row 1 Column 3
I3=imcrop(I,[365 7 170 165]);
subplot(4, 3, 6);
imshow(I3);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(3) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(3) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(3) = 0;
end
%Cropping of Row 2 Column 1
I4=imcrop(I,[20 170 170 165]);
subplot(4, 3, 7);
imshow(I4);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(4) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(4) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(4) = 0;
end
%Cropping of Row 2 Column 2
I5=imcrop(I,[185 170 185 165]);
subplot(4, 3, 8);
imshow(I5);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(5) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(5) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(5) = 0;
end
%Cropping of Row 2 Column 3
I6=imcrop(I,[365 170 170 165]);
subplot(4, 3, 9);
imshow(I6);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(6) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(6) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(6) = 0;
end
%Cropping of Row 3 Column 1
I7=imcrop(I,[20 335 170 165]);
subplot(4, 3, 10);
imshow(I7);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(7) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(7) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(7) = 0;
end
%Cropping of Row 3 Column 2
I8=imcrop(I,[185 335 185 165]);
subplot(4, 3, 11);
imshow(I8);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(8) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(8) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(8) = 0;
end
%Cropping of Row 3 Column 3
I9=imcrop(I,[365 335 170 165]);
subplot(4, 3, 12);
imshow(I9);
%When Green is shown, there are 2 products needed. When Red is shown, there
%are 3 products needed. Otherwise, no products are needed.
if classifiedImageG == 1 & classifiedImageR ~= 1
Demand_Vector(9) = classifiedImageG + 2;
elseif classifiedImageG == 1 & classifiedImageR == 1
Demand_Vector(9) = classifiedImageR + 3;
elseif classifiedImageG ~= 1 & classifiedImageR ~= 1
Demand_Vector(9) = 0;
end
disp(Demand_Vector);
Cady
Cady am 25 Okt. 2013
If you can respond to this as soon as possible it would be greatly appreciated.
sana saleeme
sana saleeme am 26 Apr. 2016
image anylist this sign ~ always create problem for me.and stop running my code.kindly help me.
Image Analyst
Image Analyst am 26 Apr. 2016
I'll try, but you forgot to attach your code.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Yatin
Yatin am 15 Okt. 2013

0 Stimmen

Hello,
May be the link below will be useful. It is based on the segmentation of the image based on colors. The link is : http://www.mathworks.com/matlabcentral/newsreader/view_thread/287764

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by