You are on page 1of 5

18.01.

2023 15:40 How to find the centreline of two curves - MATLAB Answers - MATLAB Central

How to find the centreline of two curves


 Follow 43 views (last 30 days) Show older comments

Hao Yu on 29 Feb 2020  Vote 0  Link

Edited: Image Analyst   on 29 Mar 2022

Take a track as an example, the X,Y coordinates of the inner and outer track boundary are given (no

MATLAB Answers
intersections, either closed or open). How can I calculate the centreline between the two boundaries or
two fitted curves. At each point on the centreline, the distance between the point and either curve
should be the same and can be called as the width of the track. My approach is to formulate a optimum
control problem but I want to know is there any other way to do that.

   2 Comments Show 1 older comment

Torsten   on 29 Mar 2022 

If (x1,y1) is a representation of the first curve and (x2,y2) is a representation of the second
curve, Image Analyst's suggestion is the simplest way to approach this problem.

Try his code with

 Theme  Copy
t = 0:100:2*pi;
x1 = 10*cos(t);
y1 = 10*sin(t);
x2 = 5*cos(t);
y2 = 5*sin(t);

Sign in to comment.

Sign in to answer this question.

 Answers (1)

Image Analyst    Vote 0  Link


on 29 Feb 2020
Edited: Image Analyst   on 29 Mar 2022

What I'd to is to scan along one curve and for each point, find the distance to every point
on the other curve. Find the min distance and the index for that and then get the point on
the other curve that is closest and take the midpoint of a line going from curve 1 to curve 2.
Something like (untested):
https://www.mathworks.com/matlabcentral/answers/508300-how-to-find-the-centreline-of-two-curves 1/5
18.01.2023 15:40 How to find the centreline of two curves - MATLAB Answers - MATLAB Central

 Theme  Copy
for k = 1 : length(x1)
distances = sqrt((x1(k) - x2) .^ 2 + (y1(k) - y2) .^ 2);
[minDistance, indexOfMin] = min(distances);
xCenter(k) = mean([x1(k), x2(indexOfMin)]);
yCenter(k) = mean([y1(k), y2(indexOfMin)]);
end
% Now plot the centerline curve
hold on;
plot(xCenter, yCenter, 'b-', 'LineWidth', 2);

x1 and y1 are vectors of the coordinates of one curve, and x2 and y2 are the coordinates
of the second curve. xCenter and yCenter are the coordinates of the line between curve
#1 and curve #2.

Here is a full demo:

 Theme  Copy
%========================================================================
% Initialization Steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
fontSize = 15;
markerSize = 4;
% Read in a standard MATLAB gray scale demo image.
fullFileName = 'cameraman.tif';
grayImage = imread(fullFileName);
imshow(grayImage, []);
axis on;
title('Original Grayscale Image', 'FontSize', fontSize);
g = gcf;
g.WindowState = 'maximized';
% User draws curve 1 on image here.
message = sprintf('Left click and hold to begin drawing a freehand path.\
uiwait(msgbox(message));
hFH = drawfreehand();
% Get the xy coordinates of where they drew.
xy = hFH.Position
% get rid of imfreehand remnant.
delete(hFH);
% Overlay what they drew onto the image.
hold on; % Keep image, and direction of y axis.
x1 = xy(:, 1);
y1 = xy(:, 2);
plot(x1, y1, 'r.', 'LineWidth', 2, 'MarkerSize', 12);

https://www.mathworks.com/matlabcentral/answers/508300-how-to-find-the-centreline-of-two-curves 2/5
18.01.2023 15:40 How to find the centreline of two curves - MATLAB Answers - MATLAB Central

caption = sprintf('Points may not lie on adjacent pixels, depends on your


title(caption, 'FontSize', fontSize);
% User draws curve 2 on image here.
uiwait(helpdlg('Draw a second curve across the image.'))
hFH = drawfreehand();
% Get the xy coordinates of where they drew.
xy = hFH.Position
% get rid of imfreehand remnant.
delete(hFH);
% Overlay what they drew onto the image.
hold on; % Keep image, and direction of y axis.
x2 = xy(:, 1);
y2 = xy(:, 2);
plot(x2, y2, 'r.', 'LineWidth', 2, 'MarkerSize', 12);
% Compute centerline (mid-line).
for k = 1 : length(x1)
distances = sqrt((x1(k) - x2) .^ 2 + (y1(k) - y2) .^ 2);
[minDistance, indexOfMin] = min(distances);
xCenter(k) = mean([x1(k), x2(indexOfMin)]);
yCenter(k) = mean([y1(k), y2(indexOfMin)]);
end
% Now plot the centerline curve
hold on;
plot(xCenter, yCenter, 'y-', 'LineWidth', 2);
caption = sprintf('Red curves are what you drew. Yellow curve is the com
title(caption, 'FontSize', fontSize);

https://www.mathworks.com/matlabcentral/answers/508300-how-to-find-the-centreline-of-two-curves 3/5
18.01.2023 15:40 How to find the centreline of two curves - MATLAB Answers - MATLAB Central

   0 Comments
Sign in to comment.

Sign in to answer this question.

See Also
MATLAB Answers
How can I separate the body from legs in the crab image
1 Answer

mean surface distance and residual mean square distance of 2 borders


1 Answer

how to find the height and width of an object


1 Answer

Entire Website
fasadi
File Exchange

Cartesian to Curvilinear coordinate forward and backward transformation


File Exchange

b-spline recursion
File Exchange

Categories
Image Processing and Computer Vision ❯ Computer Vision Toolbox ❯ Point Cloud Processing

https://www.mathworks.com/matlabcentral/answers/508300-how-to-find-the-centreline-of-two-curves 4/5
18.01.2023 15:40 How to find the centreline of two curves - MATLAB Answers - MATLAB Central
Find more on Point Cloud Processing in Help Center and File Exchange

Tags
curve geometry centreline curvature curve fitting iso curve midline between cu...

Poll

How often is your MATLAB code officially or unofficially reviewed?

Never, I don't typically share code

Never, even when my code is shared

Occasionally

Sometimes

More often than not

Always or almost always

8197 votes 10 comments 246 views

Vote

mathworks.com
© 1994-2022 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See mathworks.com/trademarks for a list of additional
trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders.

Join the conversation

https://www.mathworks.com/matlabcentral/answers/508300-how-to-find-the-centreline-of-two-curves 5/5

You might also like