How do I calculate area enclosed by contour lines?

100 次查看(过去 30 天)
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!

采纳的回答

Sara
Sara 2014-6-3
[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 个评论
Sara
Sara 2014-6-3
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 2017-11-7
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

请先登录,再进行评论。

更多回答(1 个)

A. Shokouhi
A. Shokouhi 2018-9-19
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 个评论
Sesha Sai Anudeep Karnam
Is this area enclosed by contour lines or area enclosed between contour lines?
S Ch
S Ch 2021-8-2
@Sesha Sai Anudeep Karnam Hello, did you have an answer ? I think it gives the area of the colors,
Is it the integral ?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by