MATLAB Answers

how to get black & white pixels count from grayscale image...???

59 views (last 30 days)
Drashti on 1 May 2014
Commented: Image Analyst on 26 Oct 2018
input image is grayscale image. how can i get black & white no. of pixels


Sign in to comment.

Answers (1)

Nitin on 1 May 2014
black corresponds to zeros in your image and white corresponds to ones if your image is double format.
I_double = im2double(img);
% Find black pixels
b = find(I_double==0);
% Find white pixels
w = find(I_double==1);


Image Analyst
Image Analyst on 26 Oct 2018
No it does not.
You'd have to do
numBlackPixels = numel(b); % Count the number of linear indexes returned.
numWhitePixels = numel(w);
For a uint8 image, you'd do
pureWhitePixels = grayImage == 255;
numberOfWhitePixels = sum(pureWhitePixels(:));
pureBlackPixels = grayImage == 0;
numberOfBlackPixels = sum(pureBlackPixels(:));

Sign in to comment.

Sign in to answer this question.