Find contour width in x and y through contour center point

8 visualizzazioni (ultimi 30 giorni)
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)

Risposta accettata

Image Analyst
Image Analyst il 7 Ott 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.
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 Commenti
Image Analyst
Image Analyst il 8 Ott 2022
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);
Konvictus177
Konvictus177 il 9 Ott 2022
Modificato: Konvictus177 il 9 Ott 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?
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))

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Contour Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by