MATLAB: Count black and white pixels on a image

cellimage processingmatrix

Hello,
I have this 800×800 image and i want to count the number of black and white pixels in it. To do so i have read the image and divide it (the matrix) in 200×200 cells. But now i am with a bit of dificulty in making a scipt that can count B&W pixels in each cell and returns the values, something like this:
Cell(1,1)–> Black_pix=100; white_pix=300 Cell(1,2)–> Black_pix=100; white_pix=300
I already figured it out how to count B&W pixels in the entire image to do so, i've written the following script:
xmax=800;
ymax=800;
Image = imread('3080.jpg');
BW = im2bw(Image);
BW1=double(BW);
White_pix=0;
Floc=0;
for j=1:(xmax)-1
for i=1:(ymax)-1
if BW1(i,j)==0
White_pix=White_pix+1;
else
Black_pix=Black_pix+1;
end
end
end
My problem is making this process for all of the cells created with "mat2cell" function. Any help would be appreciated!
Thanks
Fearing that my english was so bad that i didn't explain myself very well, i'm posting a small schematic of the paper sheet divided in smaller elements.
I need to have something like this:
Element 1 as 3000 black pixels and 4500 white pixels Element 2 as 4000 black pixels and 9800 white pixels …

Best Answer

From your original question I understood, that you have a {200 x 200} cell, such that each object contains a [4x4] matrix of pixels. But changing this to a {4x4} cell containing [200x200] matrices is trivial.
You've split your BW matrix by MAT2CELL. My answer avoid this time-consuming step, because I do not think, that there is any need for such a cell.
I'm really astonished, that a 200x200 block of a BW image can contain 34 black and 92 white pixels. I'd expect that the sum of the numbers must be 40000 ?!
Now a version for [200x200] blocks:
BW2 = reshape(BW, 200, 4, 200, 4);
nBlack = reshape(sum(sum(BW2, 1), 3), 4, 4);
nWhite = 200*200 - nBlack;
Now "nBlack(1,1)" is the number of black pixels in the first block, "nWhite(2,3)" is the number of white points in the 2nd row and 3rd column, and so on... This method works without MAT2CELL, because you did not explain, why this is needed.