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

29 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2017-3-22
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 个评论
Asmalina Mohamed Saat
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 2020-9-19
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.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2017-3-22
nnz(TheBinaryImage)
or
sum(TheBinaryImage(:))

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by