# How to find the centreline of two curves

92 views (last 30 days)
Hao Yu on 29 Feb 2020
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 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 CommentsShowHide 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
t = 0:100:2*pi;
x1 = 10*cos(t);
y1 = 10*sin(t);
x2 = 5*cos(t);
y2 = 5*sin(t);

Image Analyst 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):
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:
%============================================================================================================================================
% 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';
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.\nSimply lift the mouse button to finish.');
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);
caption = sprintf('Points may not lie on adjacent pixels, depends on your speed of drawing!');
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 computed centerline.');
title(caption, 'FontSize', fontSize);