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.
Sorry for my english ... My code is:
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
My objective is detect a crosswalk. Thanks for your spend time :)
EDIT
This is an example image :
I'm not sure to understand the question but there something to count number of black pixels in a bw image.
I = rgb2gray(imread('pass_p.png'));
bw = im2bw(I,graythresh(I));
figure;imshow(bw);
[Nx,Ny] = size(bw);
BlackPixelCount = zeros(Nx,1);
WhitePixelCount = zeros(Nx,1);
for it = 1:Nx
blackPixel = find(bw(it,:) == 0);
BlackPixelCount(it) = size(blackPixel,1);
WhitePixelCount(it) = Ny - BlackPixelCount(it);
end
EDIT
This is for a dumb example. So we expect to have only 1 row or column selected on the way of the crosswalk.
v = [1 1 1 1 0 0 0 0 1 1 0 0 1 1 1 0 0 0 0];
w =[ 1 v 1];
InvV = ~v;
w2 = [1 InvV 1];
runs_zeros = find(diff(w)==1)-find(diff(w)==-1); % lenghts of runs of 0's
runs_ones= find(diff(w2)==1)-find(diff(w2)==-1); % lenghts of runs of 1's
nbBlackBand = size(runs_zeros,2);
nbWhiteBand = size(runs_ones,2);
So after that you know you have the lenght of every band and the number of band of each sort. You can maybe take multiple line in your image and do the mean of that to get the mean of pixel per band. Otherwise you just have 1 sample.