How do I get a count of all the pixels having a certain RGB value in an image?
    10 views (last 30 days)
  
       Show older comments
    
    Coder Bee
 on 26 Apr 2016
  
    
    
    
    
    Commented: Walter Roberson
      
      
 on 27 Apr 2016
            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
0 Comments
Accepted Answer
  Walter Roberson
      
      
 on 26 Apr 2016
        sum(sum(YourImage(:,:,1) == 0 & YourImage(:,:,2) == 255 & YourImage(:,:,3) == 0))
3 Comments
  Walter Roberson
      
      
 on 27 Apr 2016
				mask = YourImage(:,:,1) == 0 & YourImage(:,:,2) == 255 & YourImage(:,:,3) == 0;
Now you can calculate the centroid of mask
More Answers (1)
  Image Analyst
      
      
 on 27 Apr 2016
        There is something called the "color frequency image" and the code is here http://www.mathworks.com/matlabcentral/fileexchange/28164-color-frequency-image
You can also do color segmentation using demos from my File Exchange and compute pixel count or area fraction.  http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

