Issue while fitting semi-circle on a set of points
Show older comments

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.
Accepted Answer
More Answers (2)
Image Analyst
on 22 Sep 2021
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 Comments
sparsh garg
on 22 Sep 2021
Image Analyst
on 22 Sep 2021
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;

sparsh garg
on 22 Sep 2021
Matt J
on 22 Sep 2021
0 votes
12 Comments
Image Analyst
on 22 Sep 2021
That's the outside/containing/bounding circle, not the inside circle. I asked John if he had a largest inside circle and he said that, for the general case, it was not easy to do so he hadn't done it.
Matt J
on 22 Sep 2021
That's not what the doc says:
function [C,R] = incircle(x,y)
% incircle: compute the maximal in-circle of the polygonal convex hull of a set of points in the plane
%
% [C,R] = incircle(x,y)
% Called with a pair of vectors of the same length,
% incircle computes the convex hull in 2-dimensions,
% then finds the maximal radius in-circle that lies
% entirely within the polygon of the convex hull.
Image Analyst
on 23 Sep 2021
Edited: Image Analyst
on 23 Sep 2021
Hmmm....Maybe he changed it since I talked with him. I'll give it a test.
But it sounds like it finds the largest circle in the convex hull, which is the outer points, not the largest circle on the interior.

So in the above diagram it would find a circle that lies in the band of dots near the outer convex perimeter, not within the clear space of the interior.
I couldn't test it. It requires a toolbox I don't have:
Error using optimset (line 147)
No default options available: the function 'linprog' does not exist on the path.
sparsh garg
on 23 Sep 2021
Image Analyst
on 23 Sep 2021
Once you have the radius and center, you can construct an arc going from the right most x to the left most x by extracting the y values above the center, then determine the starting and ending angles, and using the FAQ to construct an arc.
sparsh garg
on 29 Sep 2021
Edited: sparsh garg
on 29 Sep 2021
sparsh garg
on 29 Sep 2021
Edited: sparsh garg
on 29 Sep 2021
Image Analyst
on 29 Sep 2021
Well your data has a jagged perimeter but then you give an example for a perfect circle, which is not super helpful. Can you tell me where you'd draw chords, lines, and circles in this set of points. Do you have an outer circle, inner circle, fitted circle? Which circle do the chord segments end on???
% Demo by Image Analyst
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 long g;
format compact;
fontSize = 25;
fprintf('Beginning to run %s.m ...\n', mfilename)
numPoints = 200;
angles = linspace(0, 360, numPoints);
radius = 10;
x = radius * cosd(angles);
y = radius * sind(angles);
noiseFactor = 2.0;
x = x + noiseFactor * rand(1, numPoints);
y = y + noiseFactor * rand(1, numPoints);
% Sort by angles
angles = atan2d(y, x);
[sortedAngles, sortOrder] = sort(angles, 'ascend');
y = y(sortOrder);
x = x(sortOrder);
x(end+1) = x(1);
y(end+1) = y(1);
plot(x, y, 'b-', 'LineWidth', 3);
grid on;
axis equal;
g = gcf;
g.WindowState = 'maximized'
fprintf('Done running %s.m.\n', mfilename)

Also explain this line:
"the chord should be perpendicular to the line passing through that point of intersection."
What point? Where was the point??? As you know, all chords must automatically be perpendicular to a line drawn from the center of a perfect circle, so I'm not sure what you're envisioning where this might not be the case.
sparsh garg
on 30 Sep 2021
Edited: sparsh garg
on 30 Sep 2021
Image Analyst
on 30 Sep 2021
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.
sparsh garg
on 30 Sep 2021
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





