이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Counting Colored Objects in an Image
조회 수: 6 (최근 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 Toolbox에 대해 자세히 알아보기
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 (한국어)