2
votes

I'm trying to compute the sum of the level (z axis) values inside a contour: enter image description here

I've managed to obtain the lines (or edges) of the contour, so I have the limits of each line:

enter image description here

What I want is to sum all the levels in the z axis of the contour that are inside the outer blue line in the second plot, to compare it with the sum of the values of outside the blue line. Is there any way to do this? The code I have so far is:

   C = contourc(f, t, abs(tfr));

%extracts info from contour    
    sz = size(C,2);     % Size of the contour matrix c
    ii = 1;             % Index to keep track of current location
    jj = 1;             % Counter to keep track of # of contour lines

    while ii < sz       % While we haven't exhausted the array
        n = C(2,ii);    % How many points in this contour?
        s(jj).v = C(1,ii);        % Value of the contour
        s(jj).x = C(1,ii+1:ii+n); % X coordinates
        s(jj).y = C(2,ii+1:ii+n); % Y coordinates
        ii = ii + n + 1;          % Skip ahead to next contour line
        jj = jj + 1;              % Increment number of contours
    end
1
What do you mean the sum of the contours? If the lines are in 1,2 and 3 you want 6? Or you want to tale into account the area? Or the length of the contour itself? I ask because of all possible things you may be triying to do, I am quite sure there is an easier solution. You need to explain a bit better first to choose from those - Ander Biguri
I mean, for the line 1 (outer part of the contour of the second image), I want the sum of each value "in te perimeter". The contour shows a "mountain" and I want to compare the sum of the values that compose this mountain with the ground floor. - Pep
I dont get it. If its a contour, all values are the same in the perimeter, as it is the definition of a contour, a line in a domain where all values are the same. - Ander Biguri
Am I right in thinking that you want the sum of all elements inside the contour line (not on the contour line)? That is what my answer below will do. - EdR
Thats right EdR, that's exactly what I wanted to do! Thank you so much! - Pep

1 Answers

2
votes

So after you run the code in the question you have the coordinates of each contour in the array S. Assuming you have variables f and t of the form f = 94:0.1:101 and t = 0:1000 or similar, and the that value you want to sum are abs(tfr) you should be able to use

[fg, tg] = meshgrid(f,t)
is_inside = inpolygon(fg,tg, S(1).x, S(1).y)
integral = sum(abs(tfr(is_inside))

And similarly for other entries in S. See help for inpolygon for more examples. You can use ~is_inside for the points outside the curve