MATLAB Answers

Does anyone know how to use the matlab to calculate the minimu distance between a point outside oval and the oval surface?

17 views (last 30 days)
huazai2020
huazai2020 on 28 Jun 2020
Edited: huazai2020 on 18 Jul 2020 at 21:43
Does anyone know how to use the matlab to calculate the minimu distance between a point outside oval and the oval surface?

  3 Comments

huazai2020
huazai2020 on 29 Jun 2020
The oual can be both given in the form of an equation or data points. How to use the bwdist(), could you share me the code?

Sign in to comment.

Accepted Answer

Matt J
Matt J on 30 Jun 2020
Edited: Matt J on 30 Jun 2020
You can use trustregprob from the File Exchange
For example, consider the following ellipse and external point y,
A=[2 1;1,2]; %Ellipse equation matrix
y=[1;0.5]; %External point
z=[0.584808315593597 ; 0.201052451754066]; %Closest point
ellipsefun=@(p,q) sum([p;q].*(A*[p;q]))-1 ;
hold on
fimplicit(ellipsefun, [-1 1.3 -1 1.3])
plot(y(1),y(2),'rx',z(1),z(2),'bo');
axis equal
hold off
I found the closest point z using the code below,
Aroot=chol(A);
L=Aroot\eye(2);
z=Aroot\trustregprob(L.'*L, L.'*y,1);
and the minimum distance is just,
>> distance=norm(z-y)
distance =
0.5116

  20 Comments

Bruno Luong
Bruno Luong on 7 Jul 2020
In the FEX I post bellow there are 2 functions that convert between ellipse parametric explicit eqauation to quadratic implicit equation used by Matt.
Read the H1 line of those functions to get a flavor, and read what it does exactly in their body. The math behind is quadratic form.
huazai2020
huazai2020 on 7 Jul 2020
What is FEX AND H1? Are they these?
Find the projection of point P in R^n on the ellipsoid
E = { x = x0 + U*(z.*radii) : |z| = 1 }, where U is orthogonal matrix of the orientation of E, radii are the axis lengths, and x0 is the center.
Or on generalized conic E = { x : x'*A*x + b'*x + c = 0 }.
The projection is the minimization problem:
min | x - P | (or max | x - P|) for x in E.
Method: solve the Euler Lagrange equation with respect to the Lagrange multiplier, which can be written as polynomial equation (from an idea by Roger Stafford)
Bruno Luong
Bruno Luong on 8 Jul 2020
FEX short word for File Exchange. Click on it I put the link on my message.
H1: https://www.mathworks.com/help/matlab/matlab_prog/add-help-for-your-program.html

Sign in to comment.

More Answers (2)

Image Analyst
Image Analyst on 4 Jul 2020
"The oual can be both given in the form of an equation or data points." <== if you have data points (xb, yb) on the boundary of an ellipse/oval, you can use sqrt() to find the distances to some other point (xp, yp). Then use in() to find the minimum distance.
distances = sqrt(xb-xp).^2 + (yb-yp).^2);
minDistance = min(distances)
If you have an image then you need to get the boundary points first:
boundaries = bwboundaries(binaryImageOfEllipse);
boundaries = boundaries{1}; % Extract double array from cell array.
xb = boundaries(:, 2);
yb = boundaries(:, 1);

  10 Comments

Show 7 older comments
huazai2020
huazai2020 on 18 Jul 2020 at 12:10
Hi. This is the code that I write to find the minimum distance between the points (column A and column B in the data1.xlsx) with the wall (column C and column D in the data1.xlsx), but it has errors. Who can help me? Thank you so much.
clc;
clear all;
A = xlsread('data1.xlsx');
xpoint=A(:,1);
ypoint=A(:,2);
Num1=length(xpoint);
xwoint=A(:,3);
ywoint=A(:,4);
Num2=length(xwoint);
for i=1:1:Num1;
x1=xpoint(i);
y1=ypoint(i);
for j=1:1:Num2;
x2=xwoint(j);
y2=ywoint(j);
distances = sqrt((x2-x1).^2 + (y2-y1).^2);
minDistance(i) = min(distances)*1000;
end
fdata=minDistance;
end
xlswrite('thick.xlsx',fdata,'sheet1');
%%
The below is the errors:
Error using xlswrite (line 220)
Excel returned: Error: Object returned error code: 0x800A03EC.
Image Analyst
Image Analyst on 18 Jul 2020 at 13:58
No, you shouldn't use an inner loop because your minDistance will just be the distance between (x1,y1) and xwoint(Num2) and ywoint(Num2) instead of the whole array. Do it like this:
clc;
clear all;
A = xlsread('data1.xlsx');
xpoint=A(:,1);
ypoint=A(:,2);
Num1=length(xpoint);
xwoint=A(:,3);
ywoint=A(:,4);
Num2=length(xwoint);
for k=1:1:Num1
x1 = xpoint(k);
y1 = ypoint(k);
distances = sqrt((xwoint-x1).^2 + (ywoint-y1).^2);
minDistance(k) = min(distances)*1000;
end
plot(minDistance, 'b-');
% xlswrite('thick.xlsx',minDistance,'sheet1');
Not sure why you wanted to multiply by 1000 though.

Sign in to comment.



Translated by