how to calculate the area of the black color and the white color

조회 수: 6 (최근 30일)
bassam imam
bassam imam 2016년 5월 8일
답변: Ayush 2024년 10월 23일 8:57
Greetings
Please I need help on how to calculate the area of black circles in the following
image
I have a code that generates a random scattered circles and I need to know how much area this circles take from the full given area 100*100

답변 (1개)

Ayush
Ayush 2024년 10월 23일 8:57
Hi,
To calculate the area of black circles, ensure that the image is in binary format, i.e., the circles are black (0) and the background is white (1). Divide the number of black pixels by the total number of pixels (10,000 for a 100x100 image) to get the fraction of the area occupied by the circles. Refer to an example code below for better understanding:
% Generate or load your binary image with black circles
image = imread('your_image.png'); % Replace with your image generation code
% Convert to binary if necessary
binaryImage = imbinarize(image);
% Invert if circles are white
binaryImage = ~binaryImage;
% Calculate area of black circles using nnz
blackPixels = nnz(~binaryImage); % Count black pixels
totalPixels = numel(binaryImage);
circleAreaPercentage = (blackPixels / totalPixels) * 100;
% Display the result
fprintf('The area occupied by the black circles is: %.2f%%\n', circleAreaPercentage);

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by