Counting Colored Objects in an Image
1 次查看(过去 30 天)
显示 更早的评论
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.
采纳的回答
Image Analyst
2013-10-15
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 个评论
Cady
2013-10-15
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
2013-10-15
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
2013-10-16
% 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
2013-10-16
Thank you very much. Can you go into more detail on how to detect blue and get a binary image?
Image Analyst
2013-10-16
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
2013-10-17
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
2013-10-17
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
2013-10-17
Is there a way to do this without manually drawing some region and finding the binary image?
Image Analyst
2013-10-18
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
2013-10-22
Which one of your demo best displays figuring out thresholds and thresh-holding appropriate color channels?
Image Analyst
2013-10-23
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
2013-10-24
编辑:Cady
2013-10-24
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
2013-10-24
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
2016-4-26
image anylist this sign ~ always create problem for me.and stop running my code.kindly help me.
更多回答(1 个)
Yatin
2013-10-15
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing and Computer Vision 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)