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
Asmalina Mohamed Saat 2020년 9월 19일
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