Clear Filters
Clear Filters

how to calculate area of different components in an image?

3 views (last 30 days)
I created a matrix by using the Randi function. I then converted the matrix to an image using mat2gray. The resulting image was black and white. Can you please tell me how to get the area and define the shape of all the white components of the image?
i used the following code:
A = randi([0 1], [10 10]);
B= mat2gray(A);
i used the regionprops to get its area, but i want the individual area of the components.

Answers (1)

Prabhan Purwar
Prabhan Purwar on 11 Feb 2020
Using the matrix B provided and assuming connectivity to be along the horizontal or vertical direction only.
Following code may help:
A = randi([0 1], [10 10]);
B = mat2gray(A);
% To consider the horizontal or vertical connectivity set connectivity parameter = 4.
cc = bwconncomp(B, 4);
% Calculate area of individual connected component.
Area = regionprops(cc,'Area');
Area= 14 6 4 17 3 2 1 3 1 1 ;
Refer to the following links for more information:




Community Treasure Hunt

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

Start Hunting!