Find contour width in x and y through contour center point

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)

 Accepted Answer

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.
mask = grayImage < 128;
mask = bwareafilt(mask, 1);
props = regionprops(mask, 'Centroid')
middleRow = round(props.Centroid(2))
middleCol = round(props.Centroid(1))
width = find(mask(middleRow, :), 1, 'last') - find(mask(middleRow, :), 1, 'first')
height = find(mask(:, middleCol), 1, 'last') - find(mask(:, middleCol), 1, 'first')

5 Comments

Thanks for your answer. I will test this out but I am not sure if this is the optimal way to do it with the data I have.
My data is a actually a heatmap or surface map of a real object measured with a distance sensor.
Uh, yeah, we have no idea. Probably because you didn't read the Community Guidelines. It's still not clear what form your data is in, so please attach it in a .mat file. Your sample code using peaks made a grayscale digital image, which I guess is not what you really have. You thought it would simplify it for us, but apparently not.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
I attached x and y data of a closed contour here.
figure()
plot(xdata,ydata)
I would like to find the height and width of this closed contour whereas the width and height are horizontal and vertical lines going trough the center to the two edges of this contour.
Why? What's the use case? To me it seems just like what a homework problem would be. If so, you can't turn in my code as your own so I'll give you some hints. See if you can continue it and solve it yourself:
s = load('data.mat')
s = struct with fields:
xdata: [7×1 double] ydata: [7×1 double]
x = s.xdata
x = 7×1
457.8799 457.6970 457.4545 457.2531 457.4545 457.6970 457.8799
y = s.ydata
y = 7×1
35.7639 35.5596 35.5425 35.7639 35.9688 35.9526 35.7639
plot(x, y, 'b.-', 'LineWidth', 2, 'MarkerSize', 30);
grid on;
polyin = polyshape(x, y);
[xCentroid, yCentroid] = centroid(polyin)
xCentroid = 457.5662
yCentroid = 35.7571
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);
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?
s = load('data.mat');
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))

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!