A problem with matlab image analysis

Hiya. I was just wondering if anyone could help me with some matlab image analysis stuff. This is the first time i've used matlab to view images etc so i'm struggling. Basically all i want to do is open an image, then using imread(image) view the pixel values in the matrix. Then i need to set a threshold so that they are binary values depending on their original value. So then i can count the number of black pixels and the number of white ones to get a ratio.

im = 'crack.jpg';

im = uint8(im);

level = graythresh(im);

BW = im2bw(im,level);

x = imread(BW);

blackcount = sum(BW(1,:))

whitecount = sum(BW(:,1))

This is what i've tried so far. I managed to use this way to count the amount of perfectly white and perfectly black pixels in the image but not the amount after setting a threshold.

Any help would be really appreciated, thanks a lot!

Jordan.