How do I calculate area enclosed by contour lines?
116 views (last 30 days)
Show older comments
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!
0 Comments
Accepted Answer
Sara
on 3 Jun 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 Comments
Sara
on 3 Jun 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
on 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
More Answers (1)
A. Shokouhi
on 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 Comments
Sesha Sai Anudeep Karnam
on 20 Mar 2020
Is this area enclosed by contour lines or area enclosed between contour lines?
S Ch
on 2 Aug 2021
@Sesha Sai Anudeep Karnam Hello, did you have an answer ? I think it gives the area of the colors,
Is it the integral ?
See Also
Categories
Find more on Contour Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!