Skip to content

Instantly share code, notes, and snippets.

@hezhao
Created March 29, 2014 07:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hezhao/9849977 to your computer and use it in GitHub Desktop.
Save hezhao/9849977 to your computer and use it in GitHub Desktop.
Count the percentage of black and white pixels in an RGB-converted-to-grayscale image
filename = 'irregular.bmp';
RGB = imread(filename);
grayscale = rgb2gray(RGB);
numTotalPixel = size(grayscale,1) * size(grayscale, 2);
numBlackPixel = sum(grayscale(:)==0);
numWhitePixel = sum(grayscale(:)==255);
percentBlackPixel = numBlackPixel / numTotalPixel * 100;
percentWhitePixel = numWhitePixel / numTotalPixel * 100;
if numBlackPixel+ numWhitePixel ~= numTotalPixel
disp 'Error\n'
else
sprintf('black pixel: %.2f%%, white pixel: %.2f%%\n', percentBlackPixel, percentWhitePixel)
end
@faqir2020
Copy link

informative

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment