I’m using matlab to get counts of specific pixel values in an image.
Images are RGBA <512x512x4 uint8> when read into matlab (although we can disregard the alpha channel).
Other than;
[width, height, depth] = size(im);
for x = 1 : width;
for y = 1: height;
r = im(x,y,1);
g = im(x,y,2);
b = im(x,y,3);
...
end
end
Is there a way I can do this using matrix operations? Something along the lines of:
X = find(im(:,:,1) == 255 && im(:,:,2) == 255 && im(:,:,3) == 255);
count = length(X);
% Count being the number of pixels with RGB value (255,255,255) in the image.
I’m guessing there’s more than a few ways to do this (looking at intersect, unique functions) but I’m not clever enough with matlab to do this yet. Any help?
It’s actually pretty simple. Something like this
will give you the count of such pixels. Replace
sumwithfindto get the indices of those pixels if you need that.