MATLAB: How to get a count of all the pixels having a certain RGB value in an image

color segmentationimage processingImage Processing Toolboximage segmentationpixelrgb

I have a black and white image with a green arrow. I need a count of all the green pixels alone from the image. OR, I need a way to select just the green pixels in order to measure the length and midpoint of the arrow. Can someone help?
gcount=0;
for i=1:width
for j=1:height
% this gives me just the green channel and the ans is incorrect since the rest of my image is black and white
% if I(i,j,1)==0 & I(i,j,2)==255 & I(i,j,3)==0
if impixel(I,i,j)= =[0 255 0] %this logic is correct but takes way too long
gcount=gcount+1;
end
end
end

Best Answer

sum(sum(YourImage(:,:,1) == 0 & YourImage(:,:,2) == 255 & YourImage(:,:,3) == 0))