title: Count how many pixels are in a binary image
body: <p>I want to count the pixels on a binary image in a row. But, i need to count all layers i have and the pixels black in the layer on black and the pixels white in that layers white.</p>
  
  <p>Sorry for my english ...
  My code is:</p>
  
  <pre><code>I = rgb2gray(imread('pass_p.png'));
  level = graythresh(I);
  bw = im2uint8(im2bw(I,level));
  
  imshow(bw);
  [Nx, Ny] = size(I);
  cP = 0;
  cB = 0;
  
  %Vectores
  B = zeros(1,9);
  P = zeros(1,9);
  for k = 2:Ny-1
      index = 1;
      if(bw(((Nx-1)/2),k ) == 0) %preto
          cP = cP + 1;
          if(bw(((Nx-1)/2)-1, (k-1)) == 255)
              B(1,index) = B(1,cB);
              cB = 0;
              index = index +1;  
          end
        end
        if(bw(((Nx-1)/2),k) == 255) %branco
            cB = cB + 1;
        if(bw(((Nx-1)/2)-1, (k-1)) == 0)
            P(1,index) = P(1,cP);
            cP = 0;
            index = index +1;
        end
       end
      end
  </code></pre>
  
  <p>My objective is detect a crosswalk. 
  Thanks for your spend time :) </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>This is an example image :</p>
  
  <p><img src="https://i.stack.imgur.com/MpgXO.png" alt="enter image description here"></p>