user1439660
user1439660

Reputation: 21

check an image pixel by pixel for specific rgb values in matlab

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

Answers (1)

Andrey Rubshtein
Andrey Rubshtein

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

Related Questions