How to calculate the pixels occured by white color in binary image

32 views (last 30 days)
After converting the image to binary image and my primary aim is count the area covered by the white color and the black color in the binary image. For Example, we have performed edge detection on the image and we got an output image in binary image. I want to calculate the area of the edge in the image which are in white.

Accepted Answer

Image Analyst
Image Analyst on 22 Mar 2017
To find the white and black pixels:
numWhitePixels = sum(binaryImage(:));
numBlackPixels = sum(~binaryImage(:));
To get the number of perimeter pixels of the white regions
perimImage = bwperim(binaryImage);
numPerimeterPixels = sum(perimImage(:));
An alternate way to get the area of the white is to use bwarea():
% A weighted sum of white pixels that depends on shape of perimeter.
area = bwarea(binaryImage);
  16 Comments
Asmalina Mohamed Saat
Asmalina Mohamed Saat on 19 Sep 2020
okay..thanks
and if I want to calculate the area of white region can I use this coding area = bwarea(numwhitepixel);?? andwhat is the unit of that area?
Image Analyst
Image Analyst on 19 Sep 2020
You use bwarea() on a binary image, not a scalar number that is the count of how many pixels there are.
% Method 1
area = nnz(binaryImage);
% Method 2
area = bwarea(binaryImage);
The units are pixels. It uses a different algorithm for computing area. See it's documentation. It's not simply a pixel count but weights the edge pixels according to the shape of the boundary locally.
If you want to report the area in real world units like square centimeters, you'll have to get a spatial calibration factor to convert linear pixels to cm, and areas to cm^2. See my attached spatial calibration demo.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 22 Mar 2017
nnz(TheBinaryImage)
or
sum(TheBinaryImage(:))

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!