Search code examples
matlabarea

Finding 2D contour area when contour contains edges of domain


Say we have the following in MATLAB:

xs = -50:50;
ys = -50:50;
[X,Y] = meshgrid(xs,ys);
rs = (X.^2 + Y.^2).^(1/2);
c = contourf(X,Y,rs,[60,60]);

How do I estimate the area of the white area?

This is a test case, usually I do not know the equation for the level set.

Note that the level set intersects with the boundary, and I cannot just increase the domain size to avoid that (in the non-test case).

enter image description here


Solution

  • EDIT: If you want to approximate the white area also beyond the boundaries, then the below approach obviously won't work. I just realized, there's scope of interpretation regarding your wording.


    If you at least know the threshold contour level (here it seems to be 60) than you can just count the amount of "pixels" below that threshold, and calculate the area with respect to x and y.

    Here's some exemplary code (I modified your code, too):

    x = -50:0.1:50;
    y = -50:0.1:50;
    [X, Y] = meshgrid(x, y);
    Z = (X.^2 + Y.^2).^(1/2);
    c = contourf(X, Y, Z, [60, 60]);
    colorbar();
    
    % Total amount of pixels representing the area
    a_pixels_total = length(x) * length(y)
    
    % Amount of pixels below given contour level
    a_pixels_below = sum(Z(:) <= 60)
    
    % Total area in units
    a_unit_total = (x(end) - x(1)) * (y(end) - y(1))
    
    % Percentage of area below the given contour level in units 
    a_unit_perc = a_pixels_below / a_pixels_total * a_unit_total
    

    The output then is:

    a_pixels_total =  1002001
    a_pixels_perc =  952233
    a_unit_total =  10000
    a_unit_perc =  9503.3
    

    Hope that helps!