can I equation of a curve from an image?

Is it possible to find the equation of a curve from an image?
I want to find the equation of the line in green:

3 Comments

Are the two boundary lines to be assumed straight and parallel?
They are supposed to be parallel, and the green line is the middle (centerline). Simulate a route of a road that a vehicle should drive on the middle line.
Note that even if your lines are parallel in the 3D world, they will geneally not be parallel in the 2D camera image, depending on the camera perspective. Likewise, the centerline in the 2D image will not correspond to the centerline in 3D.

Sign in to comment.

Answers (1)

Matt J
Matt J on 25 Apr 2022
Edited: Matt J on 25 Apr 2022
This assumes your Image is already cast to type logical. The slope and intercept are for the center-line in matrix (row,column) coordinates.
reg=regionprops(Image,'PixelList');
x1=reg(1).PixelList(:,2);
y1=reg(1).PixelList(:,1);
x2=reg(2).PixelList(:,2);
y2=reg(2).PixelList(:,1);
A=[x1,x1.^0,0*x1;
x2,0*x2,x2.^0];
b=[y1;y2];
c=A\y;
slope=c(1);
intercept=mean(c(2:3));

7 Comments

Thanks for the response. Unfortunately I don't understand what you did. I want to represent the green line in the image (vehicle trajectory) as a polynomial function on which can be performed on it integral and derivative.
The green line is assumed to be a straight line with equation
y=slope*x+intercept
i asked you in the comments above if the lines are straight and you did not see they weren't.
The lines are not straight.
I suggest you write down the model equation for the lines, so that we have a fully mathematical description of the problem.
I identify the lines by the camera and then create the green line in the middle of the lines. I want to find the green line equation. Here is the code for finding the green line:
labeledImage = bwlabel(mask);
line1 = ismember(labeledImage, 1);
line2 = ismember(labeledImage, 2);
% Get rows and columns of each line.
[r1, c1] = find(line1);
[r2, c2] = find(line2);
for k = 1 : length(r1)
distances = sqrt((r1(k) - r2) .^ 2 + (c1(k) - c2) .^ 2);
[minDistance, index] = min(distances);
% Find the midPoint
midX(k) = mean([c1(k), c2(index)]);
midY(k) = mean([r1(k), r2(index)]);
% Burn into mask
mask(round(midY(k)), round(midX(k))) = true;
end
% Need to add a small amount of noise to x to make the values unique.
midX = midX + 0.001 * rand(size(midX));
midY = midY + 0.001 * rand(size(midY));
% Interpolate x and y to make sure there are no gaps.
kVec = 1 : length(midX);
kFit = linspace(1, kVec(end), 10000);
xFit = interp1(kVec, midX, kFit, 'linear');
yFit = interp1(kVec, midY, kFit, 'linear');
% Remove duplicate values
xy = unique(round([xFit(:), yFit(:)]), "rows");
% Extract individual x and y.
midX = xy(:, 1);
midY = xy(:, 2);
If you are fitting a single polynomial of known order, N, the generalization of my answer to N>1 is
reg=regionprops(Image,'PixelList');
x1=reg(1).PixelList(:,2);
y1=reg(1).PixelList(:,1);
x2=reg(2).PixelList(:,2);
y2=reg(2).PixelList(:,1);
e=N:-1:1;
A=[x1.^e, x1.^0, 0*x1;
x2.^e, 0*x2, x2.^0];
b=[y1;y2];
c=A\y;
coefficients=[c(1:N-2)',mean(c(N-1:1))]; %polynomial coefficients
Matt J
Matt J on 25 Apr 2022
Edited: Matt J on 25 Apr 2022
I identify the lines by the camera and then create the green line in the middle of the lines. I want to find the green line equation. Here is the code for finding the green line:
If you already have the points on the green line, what prevents you from using fit(), lsqcurvefit(), or similar? Also, you have articulated that you want to model this as a polynomial, but of what order? Are you sure you don't need a piecewise polynomial? In any case, fit() offers all of these.

Sign in to comment.

Asked:

on 25 Apr 2022

Commented:

on 25 Apr 2022

Community Treasure Hunt

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

Start Hunting!