2

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?

Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
user1439660
  • 21
  • 1
  • 2
  • Is this a homework? If so, please retag it as homework. – petrichor Jun 06 '12 at 12:19
  • no its not a homework.. is a part of a personal project – user1439660 Jun 06 '12 at 12:25
  • @user1439660: Instead of doing color comparison in RGB, you could convert to a different colorspace to select your color. Something like this: [How can I convert an RGB image to grayscale but keep one color?](http://stackoverflow.com/a/4064205/97160) – Amro Jun 06 '12 at 15:56

1 Answers1

2

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);
Andrey Rubshtein
  • 20,795
  • 11
  • 69
  • 104
  • 2
    in the second solution, you could write: `S = all(bsxfun(@eq, A, pixel), 3);` instead – Amro Jun 06 '12 at 15:52