# Find contour width in x and y through contour center point

17 views (last 30 days)
Konvictus177 on 7 Oct 2022
Edited: Konvictus177 on 9 Oct 2022
Hi,
How can I find the width in x and y of each contour. The width in x and y goes through the center of the contour (see image below).
Thanks.
[X,Y,Z] = peaks;
v = [1,1];
contour(X,Y,Z,v)

Image Analyst on 7 Oct 2022
What kind of data do you have? A digital image with a line drawing in it? If so just use regionprops to compute the Centroid, then use find() to find the first and last line pixel at the Centroid.
middleRow = round(props.Centroid(2))
middleCol = round(props.Centroid(1))
Konvictus177 on 9 Oct 2022
Edited: Konvictus177 on 9 Oct 2022
Thanks for the hints. I completed the code with your help and this is how it looks. I am able to calculate the heigth and width.
Do you see any issues with his methode or are there easier ways to come to the same result?
x = s.xdata;
y = s.ydata;
figure()
plot(x, y, 'b.-', 'LineWidth', 2, 'MarkerSize', 30);
grid on;
polyin = polyshape(x, y);
[xCentroid, yCentroid] = centroid(polyin);
hold on;
plot(xCentroid, yCentroid, 'r+', 'LineWidth', 2, 'MarkerSize', 250);
figure
subplot(2, 1, 1);
plot(x, 'r.-', 'LineWidth', 2, 'MarkerSize', 30);
grid on;
yline(xCentroid, 'g', 'LineWidth', 2);
subplot(2, 1, 2);
plot(y, 'b.-', 'LineWidth', 2, 'MarkerSize', 30)
grid on;
yline(yCentroid, 'g', 'LineWidth', 2);
% ----------------------------------------------------------------------------------%
[xi_h,yi_h] = polyxpoly(1:length(x),x,1:length(x),repmat(xCentroid,length(x),1));
[xi_w,yi_w] = polyxpoly(1:length(y),y,1:length(y),repmat(yCentroid,length(y),1));
figure;
hold on
subplot(2, 1, 1);
plot(x, 'r.-', 'LineWidth', 2, 'MarkerSize', 30);
grid on;
yline(xCentroid, 'g', 'LineWidth', 2);
arrayfun(@xline,xi_w)
subplot(2, 1, 2);
plot(y, 'b.-', 'LineWidth', 2, 'MarkerSize', 30)
grid on;
yline(yCentroid, 'g', 'LineWidth', 2);
arrayfun(@xline,xi_h)
Y_w = interp1(1:length(x),x,xi_w);
Y_h = interp1(1:length(y),y,xi_h);
Width = abs(diff(Y_w))
Heigth = abs(diff(Y_h))

### 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!

Translated by