# how to calculate area of different components in an image?

3 views (last 30 days)
Victoria Austin on 6 Feb 2020
Answered: Prabhan Purwar on 11 Feb 2020
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.

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');
Output:
Area= 14 6 4 17 3 2 1 3 1 1 ;

### Categories

Find more on Image Arithmetic in Help Center and File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!