using contourc to calculate the length of a single level contour

23 views (last 30 days)
I'm still relatively new to matlab and don't quite know the right syntax to use as yet.
I have some given data in a file and I need to calculate the lengths of the contours produced at a specific level e.g. 100.
I've used the contourc function and understand what the matrix produces, but I'm struggling to calculate the lengths of each contour, basically extracting the data and manipulating it from the matrix. I know that contourc gives the coords for each -100 contour and that I should use something like:
sqrt((x2 - x1)^2 + (y2 - y1)^2)
to calculate the distance between each point and then add them all together to get the length for that contour. My problem is how do I use a nested for loop to calculate these lengths?
My code so far looks like this (and I know majority of it is not quite correct): b = contourc(gi3, [-120.0 -120.0]); disp(b);
% one loop for all the contours % another loop for the length of each contour x1 = 0; y1 = 0; distance = 0; I = 0; J = 0;
for I = 1:4409
for x = -120.0
distance = sqrt((x2 - x1)^2 + (y2 - y1)^2) + distance;
J = J + 1;
end;
x1 = x2;
I = I + 1;
end;
Thanks in advance :)

Answers (4)

Image Analyst
Image Analyst on 9 Apr 2013
Do you have the Image Processing Toolbox? If so, just threshold and call regionprops():
thresholdValue = 100; % or whatever
% Measure perimeters for all contours at 100.
blobMeasurements = regionprops(data>=thresholdValue, 'Perimeter');
% blobMeasurements is a structure.
% Put perimeters for all contours at 100 into a simple numerical array.
allPerimeters = [blobMeasurements.Perimeter];
Repeat for whatever level you want.

kibby
kibby on 9 Apr 2013
No unfortunately I don't.
I'm getting confused with how I would use a nested 'for' loop to compute the lengths.

Kelly Kearney
Kelly Kearney on 9 Apr 2013
You're on the right track. You could use two loops, but the vectorized version is cleaner. I've used the simple wrapper function contourcs (File Exchange #28447) to extract the coordinates of the contours.
[x,y,z] = peaks(100);
C = contourcs(x(1,:), y(:,1), z, [0 0]);
len = zeros(size(C));
for ic = 1:length(C)
dx = diff(C(ic).X);
dy = diff(C(ic).Y);
len(ic) = sum(sqrt(dx.^2 + dy.^2));
end
  3 Comments
Kelly Kearney
Kelly Kearney on 9 Apr 2013
To use contourcs, simply download the file from http://www.mathworks.com/matlabcentral/fileexchange/28447-contourcs-to-obtain-contourc-output-as-a-struct-array and place the .m file in the same folder that you're working in. It's a pretty simple function, so it should run under any version of Matlab. And it will take care of reformatting the output from contourc into a more user-friendly arrangement, so you can apply the distance calculations. Post the specific error you're getting if this doesn't work.
Ali
Ali on 29 Jun 2023
Hi, I am also looking for a way to determine the total length of all contour lines and tried the "contourcs" function. Please note that I am a complete rookie in MatLab. This is my output:
C =
1638×1 struct array with fields:
Level
Length
X
Y
My question is: what does the "1638x1 struct arrays" tell me? Is it the total length in pixel? And what about the "length" field? Is there supposed to be a value next to it?
Sorry for digging up this topic after 10 years, but there is no one else who I could ask with this function.

Sign in to comment.


kibby
kibby on 10 Apr 2013
Ah ok. The problem is I can't use other people's codes in my code; it has to be 'original'.
I just need a starting idea on how I should start the loops and from there I can carry on.
  1 Comment
Kelly Kearney
Kelly Kearney on 10 Apr 2013
By "start the loops", do you mean get the x/y coordinates of the contour lines?
As the contourc documentation says, it returns "a two-row matrix specifying all the contour lines. Each contour line defined in matrix C begins with a column that contains the value of the contour (specified by v and used by clabel), and the number of (x,y) vertices in the contour line. The remaining columns contain the data for the (x,y) pairs."
So what you need to do is loop over that matrix, reading one column to get the contour-line value and # of points, then reading specified number of columns to get the coordinates, then repeating the process.
For example, the following toy example defines two contour lines, both at level 0, one with 5 points and one with 2:
c = [0 x1 x2 x3 x4 x5 0 x1 x2]
5 y1 y2 y3 y4 y5 2 y1 y2]
You need to loop over the matrix and extract the x/y points. Which is exactly what contourcs does. Just open the code and study it. Then either use it or replicate it (letting your teacher know that you used someone else's code to learn would be more honest).

Sign in to comment.

Categories

Find more on Contour Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!