# How to find the centreline of two curves

50 views (last 30 days)

Show older comments

##### 2 Comments

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);

### Answers (1)

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';

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.\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);

##### 0 Comments

### See Also

### Community Treasure Hunt

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

Start Hunting!