You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Counting Colored Objects in an Image
2 views (last 30 days)
Show older comments
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.
Accepted Answer
Image Analyst
on 15 Oct 2013
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 Comments
Image Analyst
on 15 Oct 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.
Image Analyst
on 16 Oct 2013
% 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);
Image Analyst
on 16 Oct 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.
Image Analyst
on 17 Oct 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.
Image Analyst
on 18 Oct 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.
Image Analyst
on 23 Oct 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
on 24 Oct 2013
Edited: Cady
on 24 Oct 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.
Cady
on 24 Oct 2013
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);
sana saleeme
on 26 Apr 2016
image anylist this sign ~ always create problem for me.and stop running my code.kindly help me.
Image Analyst
on 26 Apr 2016
I'll try, but you forgot to attach your code.
More Answers (1)
Yatin
on 15 Oct 2013
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
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)