Please can you tell me Algorithm to count the number of white pixel ?

4 views (last 30 days)
i want algorithm to count the number of white pixels, please .

Answers (2)

Image Analyst
Image Analyst on 11 Apr 2014
How do you define "white". Have you checked out my color segmentation demos in my File Exchange? http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
In the special specific case where you have a gray scale image (not color) and want to just count the number of pixels at exactly 255, you can do this:
whitePixels = grayImage == 255; % Produces a binary (logical) image.
count = sum(whitePixels(:))
  9 Comments
Jotheeshwar V
Jotheeshwar V on 4 Jul 2018
%
imread color.jpg
load color.jpg
redChannel = color(:, :, 1);
greenChannel = color(:, :, 2);
blueChannel = color(:, :, 3);
whitePixels = redChannel == 255 & ...
greenChannel == 255 & ...
blueChannel == 255;
count = sum(whitePixels(:));
Red1Pixels = redChannel == 255 & ...
greenChannel == 166 & ...
blueChannel == 166;
count = sum(Red1Pixels(:));
Red2Pixels = redChannel == 254 & ...
greenChannel == 85 & ...
blueChannel == 82;
count = sum(Red2Pixels(:))
Blue1Pixels = redChannel == 179 & ...
greenChannel == 169 & ...
blueChannel == 250;
count = sum(Blue1Pixels(:))
Blue2Pixels = redChannel == 255 & ...
greenChannel == 255 & ...
blueChannel == 255;
count = sum(Blue2Pixels(:))
This my complete code.
This might be useful for you people to help me.
-Jotheeshwar

Sign in to comment.


Walter Roberson
Walter Roberson on 11 Apr 2014
In a greyscale image, every pixel that is not absolute black is arguably white of some brightness or other. Therefore you can count the number of non-zero pixels to get the answer. Fortunately, MATLAB has a call nnz() that does that for you.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!