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

7 visualizaciones (últimos 30 días)
bassam imam
bassam imam el 8 de Mayo de 2016
Respondida: Ayush el 23 de Oct. de 2024
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

Respuestas (1)

Ayush
Ayush el 23 de Oct. de 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);

Categorías

Más información sobre Image Processing Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by