Least Square Method for circle fitting

조회 수: 10 (최근 30일)
Rishika Agarwal
Rishika Agarwal 2020년 10월 3일
편집: Harsh Parikh 2020년 10월 6일
Hi, I am looking for a code that can help me guess how close the borders/edge of a image is to a circle using least sqaure method.
I = imread('i.jpg');
imshow(I)
BW = imbinarize(I(:,:,1),0.5);
% imshow(BW)
[B, L] = bwboundaries(BW,'noholes');
imshow(label2rgb(L))
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
using the above code, i am able to get a edge but now i have to plot these edge points and fit circle if possible to see its resemblence to the actuall circle, i am very new t matlab and is badly in need of help
thanks

답변 (2개)

Priysha LNU
Priysha LNU 2020년 10월 6일
To find the smallest distance between a circle and an edge, given that you have already got the edge of the image, you may calculate the perpendicular distance between the center of the circle and the edge. Let this distance be 'a'.
The ability to automatically calculate the shortest distance from a point to a line is not available in MATLAB. To work around this, see the following function:
function d = point_to_line(pt, v1, v2)
a = v1 - v2;
b = pt - v2;
d = norm(cross(a,b)) / norm(a);
In this function, pt, v1, and v2 are the three-dimensional coordinates of the point, one vertex on the line, and a second vertex on the line, respectively. The following example illustrates how this function would be called:
v1 = [0,0,0];
v2 = [3,0,0];
pt = [0,5,0];
a = point_to_line(pt,v1,v2)
You may then subtract the radius of the circle 'r' from 'a' to get the shortest distance between the circumfrence of the circle and edge of the image.
DISCLAIMER: These are my own views and in no way depict those of MathWorks.

Harsh Parikh
Harsh Parikh 2020년 10월 6일
편집: Harsh Parikh 2020년 10월 6일
Hi,
'bwboundaries()' returns a set of 'Edges' it finds in the input image. Given that, you can use the following piece of code to fit the points as least squares method.
I have used the following image (circle.png) for the testing purpose.
I = imread('circle.png');
imshow(I)
BW = imbinarize(I(:,:,1),0.5);
[B, L] = bwboundaries(BW,'noholes');
fgg = B{2}; % B has two edges: 1. Square 2. Circle from the image, you might need to do the below for the
x = fgg(:,1);
y = fgg(:,2);
ang = linspace(0,2*pi,length(fgg))'; % angles
plot(x,y,'+') % Plot the actual points
axis equal; hold on;
c = [x y ones(length(x),1)]\-(x.^2+y.^2); %least squares fit
xhat = -c(1)/2;
yhat = -c(2)/2;
rhat = sqrt(xhat^2+yhat^2-c(3));
plot(rhat*cos(ang)+xhat,rhat*sin(ang)+yhat,...
'g','linewidth',2) %plot the best fit circle from it's parameters
Both the plots would overlap in the case of Circular object or you can see how closely the points resemble to a circle.
Use the following ('ellipse.png') to check the same:
You might want to check out the following links which deals with the similar problem with different approaches:
Hope this helps.

카테고리

Help CenterFile Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by