![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/746874/image.png)
Issue while fitting semi-circle on a set of points
9 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/746519/image.png)
In this figure I would like to fit a semi-circle on the green curve
The ref code for plotting semi-circle is as follows
function Circ = semi_circle(xc,yc,R,flag)
theta=linspace(pi/2,-pi/2,100);
xcircle = R*cos(theta')+xc;
ycircle = R*sin(theta')+yc;
if flag
plot(xcircle,ycircle);
end
Circ=[xcircle,ycircle];
end
xc,yc and R are obtained as follows
First compute normals for left seg 1 using LineNormals2D,then project the points on left seg1 using the angles in LineNormals2D I will then look at where these projections intersect left_seg2 using InterX. The intersectin point will be the center and the distance between the center and the poin t on left seg 1 will be radius
The green points are enclosed left_seg1 and left_seg2.
Any help/suggestions will be appreciated.
0 个评论
采纳的回答
Matt J
2021-9-22
I recommend circularFit from
In addition to doing more careful data pre-normalization than the Bucher Izhak code routine, it contains methods for post-plotting the fit.
>> fobj=circularFit([left_seg,left_seg2])
fobj =
circularFit with properties:
center: [133.7487 51.8286]
radius: 4.3551
>> plot(fobj)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/746874/image.png)
2 个评论
Image Analyst
2021-9-22
To get it inside the points, you have to identify the 3 points that are closest to the middle. So use the original code to find the center. Then get the distances of all points to that center, then sort, and take the 3 shortest distances and fit a cirvle through only those 3 points.
更多回答(2 个)
Image Analyst
2021-9-22
I'd suggest you use the FAQ to fit a circle through the points you have circled:
function [xCenter, yCenter, radius, a] = circlefit(x, y)
% circlefit(): Fits a circle through a set of points in the x - y plane.
% USAGE :
% [xCenter, yCenter, radius, a] = circlefit(X, Y)
% The output is the center point (xCenter, yCenter) and the radius of the fitted circle.
% "a" is an optional output vector describing the coefficients in the circle's equation:
% x ^ 2 + y ^ 2 + a(1) * x + a(2) * y + a(3) = 0
% by Bucher Izhak 25 - Oct - 1991
numPoints = numel(x);
xx = x .* x;
yy = y .* y;
xy = x .* y;
A = [sum(x), sum(y), numPoints;
sum(xy), sum(yy), sum(y);
sum(xx), sum(xy), sum(x)];
B = [-sum(xx + yy) ;
-sum(xx .* y + yy .* y);
-sum(xx .* x + xy .* y)];
a = A \ B;
xCenter = -.5 * a(1);
yCenter = -.5 * a(2);
radius = sqrt((a(1) ^ 2 + a(2) ^ 2) / 4 - a(3));
3 个评论
Image Analyst
2021-9-22
Not sure what's in your mat files. It's not what you showed in your original post
s1 = load('left_seg1.mat')
s2 = load('left_seg2.mat')
x1 = s1.left_seg(1, :)
y1 = s1.left_seg(2, :)
x2 = s2.left_seg2(1, :)
y2 = s2.left_seg2(2, :)
plot(x1, y1, 'b-', 'LineWidth', 2);
hold on;
plot(x2, y2, 'r-', 'LineWidth', 2);
grid on;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/746779/image.png)
Matt J
2021-9-22
12 个评论
Image Analyst
2021-9-30
In your magnified example, the black line from the top/magenta point, through the center of the circle to the bottom of the circle IS "perpendicular to the tangent that passes through the bottom of the circle". By definition, any line from the center of a circle to the perimeter of the circle is necessarily perpendicular to a tangent of the circle at the point where the line crosses the perimeter. You are not showing the line tangent to the circle but if you did, you would see that it's perpendicular to the black line through the center.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Bounding Regions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!