How do I calculate area enclosed by contour lines?

99 vues (au cours des 30 derniers jours)
Kushagra Saxena
Kushagra Saxena le 3 Juin 2014
Commenté : S Ch le 2 Août 2021
I have
[x, y, z] = peaks; [C, h] = contour(x, y, z, 20);
Kindly suggest an algorithm or necessary functions to find the area enclosed by, and between, contour lines.
Thanks!

Réponse acceptée

Sara
Sara le 3 Juin 2014
[x, y, z] = peaks;
[C, h] = contour(x, y, z, 20);
ch = get(h,'children');
area = zeros(numel(ch),1);
for i = 1:numel(ch)
x = get(ch(i),'xdata');
y = get(ch(i),'ydata');
[x,y] = poly2cw(x,y);
area(i) = polyarea(x,y);
end
  3 commentaires
Sara
Sara le 3 Juin 2014
It's in the mapping toolbox. It orders the points in a contour in clockwise order, so that the contour is external and an area can be computed (counterclockwise contours are considered internal contours). This is ArcGIS convention and it's followed also by Matlab (not sure who started first). I personally don't have a version working with the basic matlab license and never looked if there is something similar in the file exchange since I have the mapping toolbox.
XING ZHENG
XING ZHENG le 7 Nov 2017
Dear Sara,
Now I am using your function in the version 2017b: [C, h] = contour(x, y, z, 20); ch = get(h,'children');
However, I can not get 'h' from this sentence. therefore, there is No result of ch. I think this is the difference between the version 2014b and 2017b.
How can I fix this problem? Thanks
Xing

Connectez-vous pour commenter.

Plus de réponses (1)

A. Shokouhi
A. Shokouhi le 19 Sep 2018
I believe some commands in the code provided by Sara is not available in new versions here a code I wrote down that does the same job.
[x, y, z] = peaks;
[C, h] = contour(x, y, z, 5);
n = 0;
i = 1;
sz = size(h.ContourMatrix,2);
nn(1) = h.ContourMatrix(2,1);
xx = h.ContourMatrix(1,2:nn(1)+1);
yy = h.ContourMatrix(2,2:nn(1)+1);
area(1) = polyarea(xx,yy);
while n+nn(i)+i < sz
n = n + nn(i);
i = i + 1;
nn(i)=h.ContourMatrix(2,n+i);
xx = h.ContourMatrix(1,n+i+1:n+nn(i)+i);
yy = h.ContourMatrix(2,n+i+1:n+nn(i)+i);
area(i) = polyarea(xx,yy);
end
  4 commentaires
Sesha Sai Anudeep Karnam
Sesha Sai Anudeep Karnam le 20 Mar 2020
Is this area enclosed by contour lines or area enclosed between contour lines?
S Ch
S Ch le 2 Août 2021
@Sesha Sai Anudeep Karnam Hello, did you have an answer ? I think it gives the area of the colors,
Is it the integral ?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Contour Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by