Created
March 29, 2014 07:11
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
informative