Search code examples
matlabareacolor-mappingcontourf

Calculating area with regionprops matlab


I have this plot that I made using contourfenter image description here

This is a microscopic picture where I am trying to get the orientation of each ply. I have made a colormap where i have a constant color for each 10 degree. So basically i am kind of assuming a constant color correspond to a constant ply. Now I would like to calculate the area of each ply. I have no idea how to do that. So first i want to be able to ask matlab to tell me ok the area for 0 to 10 degree is XX(in pixel^2). But second I want to be able to specify a single ply. because there are several regions for 0 to 10, but I want to know the area of each one.

Do you know if that is possible? Cheers D

EDIT So i used your suggestion, and I get something like this Ok so I did it, with one picture and like I get this for example

R = 

  Columns 1 through 3

    [32x1 struct]    [450x1 struct]    [1110x1 struct]

  Columns 4 through 6

    [1978x1 struct]    [2778x1 struct]    [3392x1 struct]

  Columns 7 through 9

    [5249x1 struct]    [8215x1 struct]    [15711x1 struct]

  Columns 10 through 12

    [12019x1 struct]    [5335x1 struct]    [2643x1 struct]

  Columns 13 through 15

    [1804x1 struct]    [1018x1 struct]    [670x1 struct]

  Columns 16 through 18

    [579x1 struct]    [344x1 struct]    [50x1 struct]

I want to be able to get something like this (from matlab help) but for the area

stats = 

        Centroid        MajorAxisLength    MinorAxisLength
    ________________    _______________    _______________

     256.5     256.5    834.46             834.46         
       300       120    81.759             81.759         
    330.47    369.83    111.78             110.36         
       450       240    101.72             101.72         

Also I saw on matlab you can replot around the regions, like here Get centers and radii of the circles.

centers = stats.Centroid;
diameters = mean([stats.MajorAxisLength stats.MinorAxisLength],2);
radii = diameters/2;
Plot the circles.

hold on
viscircles(centers,radii);
hold off

and they go from this image to that: enter image description here enter image description here

I would quite like to do that as well for each region.


Solution

  • contourf is for display, and in my opinion it is not the best solution for your problem. Here is a solution based on regionprops (from the Image Processing Toolbox):

    Suppose A contains your original array / image

    angs = -90:10:90;
    
    for i = 1:numel(angs)-1
    
        BW = A>=angs(i) & A<angs(i+1);
    
        R{i} = regionprops(BW, 'Area');
    
    end
    

    For each interval, R{i} will contain a structure with as many elements as distinct regions and a field Area, in number of pixels.

    Hope this helps,