MATLAB: How to extract black pixels and white ones in a grayscale image

Image Processing Toolboximage segmentationpixels

Hello, i need to know how I can extract from several grayscale images, the number of black pixels and white ones. Please help me

Best Answer

If black pixels are considered those with values equal to or less than "threshold", you can do
numBlackPixels = nnz(grayImage <= threshold);
If white pixels are considered those with values equal to or greater than "threshold", you can do
numWhitePixels = nnz(grayImage >= threshold);