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

7 views (last 30 days)
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

Answers (1)

Ayush
Ayush on 23 Oct 2024
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);

Categories

Find more on Image Processing Toolbox 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!