Reputation: 21
I want to read an RGB image(.jpg) from a folder in MATLAB, scan each pixel of the image and check if it has a specific color (for example if it is Violet:R 128,G 0, B 255
) and count how many pixels have this specific color.
Do you have an idea?
Upvotes: 2
Views: 8092
Reputation: 20915
Assuming that the image is loaded into variable named A
:
pixelMask = A(:,:,1) == 128 & A(:,:,2) == 0 & A(:,:,3) == 255;
count = nnz(pixelMask);
Another way is to use bxsfun
and singleton expansion:
pixel = cat(3,128,0,255);
S = all(bsxfun(@eq, A, pixel), 3);
count = nnz(S);
Upvotes: 2