Please , how can I find the center and the radius of the inscribed circle of a set of points
22 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
daeze zezezeze
le 18 Jan 2018
Modifié(e) : Image Analyst
le 15 Mar 2020
Please , how can I find the center and the radius of the inscribed circle of a set of points
2 commentaires
John D'Errico
le 1 Fév 2018
Do not add answers just to ask a question again!
Moved an answer into a comment:
"Are there any solution to get the inscribed circle inside an irregular polygone? thnak you"
John D'Errico
le 1 Fév 2018
Modifié(e) : John D'Errico
le 1 Fév 2018
And my response to your question is that you never actually answered MY questions.
You need to define what it means for a circle to lie inside an irregular set of points. In some places, you state it as inside an irregular set of points. In others, it is now inside some general irregular polygon.
If you want an answer, you need to state CLEARLY what you need.
In any case, the computation you desire is NOT a trivial one.
Réponse acceptée
Image Analyst
le 1 Fév 2018
Modifié(e) : Image Analyst
le 15 Mar 2020
daeze, see if this is what you want. I solved it numerically by converting the data to a digital image, then using the Euclidean distance transform and finding the center of it. The max of the EDT is the center of the circle and its value there is the radius. If you need more than 3 digits of precision then you can increase the size of the image from 1000 to 10000 or larger.
% Initialization / clean-up code.
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;
x=[2.6381318;-18.66985584;-39.97784349;-69.8090262;-99.64020891;-92.131167993;-84.62315095;-57.4301004;-30.23704914;-3.65279788;22.93145337;34.09278024;45.2541071;23.94611945]
y=[75.28822303;63.72102973;52.15383644;43.02184173;33.88984702;19.48158871;5.07333039;5.07333039;5.07333039;5.07333039;5.07333039;25.77251893;46.4717064;60.87996471]
subplot(2, 2, 1);
plot(x, y, 'b.-', 'MarkerSize', 30);
grid on;
title('Original Points', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Make data into a 1000x1000 image.
xMin = min(x)
xMax = max(x)
yMin = min(y)
yMax = max(y)
scalingFactor = 1000 / min([xMax-xMin, yMax-yMin])
x2 = (x - xMin) * scalingFactor + 1;
y2 = (y - yMin) * scalingFactor + 1;
mask = poly2mask(x2, y2, ceil(max(y2)), ceil(max(x2)));
% Display the image.
p2 = subplot(2, 2, 2);
imshow(mask);
axis(p2, 'on', 'xy');
title('Mask Image', 'FontSize', fontSize);
% Compute the Euclidean Distance Transform
edtImage = bwdist(~mask);
% Display the image.
p3 = subplot(2, 2, 3);
imshow(edtImage, []);
axis(p3, 'on', 'xy');
% Find the max
radius = max(edtImage(:))
% Find the center
[yCenter, xCenter] = find(edtImage == radius)
% Display circles over edt image.
viscircles(p3, [xCenter, yCenter], radius);
% Display polygon over image also.
hold on;
plot(x2, y2, 'r.-', 'MarkerSize', 30, 'LineWidth', 2);
title('Euclidean Distance Transform with Circle on it', 'FontSize', fontSize);
% Display the plot again.
subplot(2, 2, 4);
plot(x, y, 'b.-', 'MarkerSize', 30);
grid on;
% Show the circle on it.
hold on;
% Scale and shift the center back to the original coordinates.
xCenter = (xCenter - 1)/ scalingFactor + xMin
yCenter = (yCenter - 1)/ scalingFactor + yMin
radius = radius / scalingFactor
rectangle('Position',[xCenter-radius, yCenter-radius, 2*radius, 2*radius],'Curvature',[1,1]);
title('Original Points with Inscribed Circle', 'FontSize', fontSize);
2 commentaires
Plus de réponses (6)
Jim Riggs
le 18 Jan 2018
Modifié(e) : Jim Riggs
le 18 Jan 2018
I have worked this problem in the past, and the only solution I could find was a numerical searching method. There are a number of different ways to do it, but here is a suggested algorithm: It is assumed that the points are defined by a list of (x,y) coordinates.
1) Find the min and max of the x and the y coordinates. This defines a rectangle from (Xmin, Ymin) to (Xmax, Ymax).
2) Use the center of this rectangle as the initial point for the center of the circle. C = {(Xmax-Xmin)/2 , (Ymax-Ymin)/2 } You can compute the circle radius from C to any vertex of the box (e.g. (Xmin, Ymin).
3) Compute the distance from C to each data point.
4) Sort the distances to find the largest value. (Call this point P.) The circle radius may be reduced to this value, and it will still contain all of the data points, with one point, P, lying on the circle.
5) In order to further reduce the radius, you must move the center position. Move the center of the circle a small distance toward point P (call this new center point C2) and compute the distance from C2 to every other point. Continue adjusting the center point along a line from C to P until you have a second point that lies on the circle. (Call this second point Q)
6) Now define the line that goes through the center C2 and for which all points are equidistant to P and Q. Now move the circle center along this new line (reducing the radius value) until you have a third point which lies on the circle.
Since three points define a circle, you cannot reduce it any further. You have the center, and radius of a circle that inscribes all of the points and contains three of them.
Is it the smallest circle that can be found? I don't know. But this algorithm always converges to a solution.
(Watch out for the case where P and Q define the diameter of the circle. In this case, there will be no third point.)
1 commentaire
John D'Errico
le 19 Jan 2018
I have a funny feeling that this scheme (I'd call it a greedy algorithm) usually works, but that it can be broken by careful choice of the point set, designed to push the iterations into the wrong circle. I've used methods like it myself. In fact, this scheme probably always works on a nicely convex point set. But throw an interior point into the mix...
Image Analyst
le 18 Jan 2018
Tell me if John's program does what you want: https://www.mathworks.com/matlabcentral/fileexchange/34767-a-suite-of-minimal-bounding-objects
3 commentaires
Image Analyst
le 18 Jan 2018
I wasn't sure, and am still not, whether the circle was one that was outside and contained a roughly circular cluster of points, or whether there was a "hole" in the points (like a dotted donut) and he wanted the circle to be inside the points but containing the edge of the hole (essentially outlining the hole of the donut). A diagram of what he wants would certainly clarify this.
John D'Errico
le 19 Jan 2018
It has been a while since I wrote incircle, so I had to check myself to be sure.
[C,R] = incircle([0 0 1 1],[0 1 0 1])
C =
0.5 0.5
R =
0.5
So incircle is definitely looking at the convex hull polygon.
John D'Errico
le 19 Jan 2018
The largest incircle of a scattered set may be difficult to find. A simple heuristic, like that suggested by Jim will work if the set is a convex one. I'd predict it can also do strange things if you choose a nasty set designed to break it. In fact, you can probably make life interesting for any algorithm with a set of points like this:
x = [0.1 -0.1 0 0 -1 -1 1 1];
y = [0 0 .1 -.1 -1 1 -1 1];
plot(x,y,'o')
axis equal
grid on
axis square
Be careful, since the largest circle that passes through three of those points and does not contain any other point has its center outside of the convex hull.
So careful definition of the desired inscribed circle may be needed to resolve the question. Must the center lie inside the convex hull?
5 commentaires
Jan
le 1 Fév 2018
Modifié(e) : Jan
le 1 Fév 2018
@John: "claiming there is a bug in the code is insanity" - no, it is not insane. It is a misunderstanding. Daeze tries to solve a problem and did not understand exactly, what the function incircle does.
I'd suggest to remove the code on incircle from this thread. The link to the FEX submission is better.
@daeze: Your questions are welcome in the forum. It is useful that you show your own effort, although the approach with incircle is not working for your problem. You try to solve your problem and the forum tries to help you. I appreciate your polite reaction.
daeze zezezeze
le 19 Jan 2018
Modifié(e) : Torsten
le 19 Jan 2018
1 commentaire
John D'Errico
le 1 Fév 2018
NO. It is not the code. ARGH. Listen to what I said. incircle does not solve the problem you want to solve.
Jan
le 1 Fév 2018
Your example contains a small set of points only. Then a brute force approach can solve the problem in a bearable time: Create all possible sets of 3 points, calculate the corresponding circles and choose the one, which matches all criteria.
But what exactly are the criteria? John has asked already: Must the center be inside the convex hull? Which circle is wanted for John's wonderful example above? As long as it is not defined mathematically, what an "inscribed circle of a set of points" is, posting solutions is based on guessing. So please define your problem exactly.
This is a global optimization problem: Starting at one specific point and trying to let a circle grow as far as possible can stuck in a local maximum. For a reliable search, you must start from each triangle of the triangulated area. And for this triangulation it matters, if you mean an alpha shape (which could contain wholes) or the convex hull.
0 commentaires
John D'Errico
le 1 Fév 2018
If I were going to solve the problem of finding an incircle for a general non-degenerate closed polygon, I MIGHT try the following algorithm:...
1. Verify the polygon is closed. If not, then form the closure.
2. Verify the polygon does not cross itself, or is non-degenerate in some other way.
3. Verify the polygon is not already convex. If is is convex, then incircle does the job efficiently, so you are done.
4. Triangulate the polygon. Ear clipping is entirely adequate here. Fast, efficient and stable as long as the polygon is not degenerate.
5. Generate a large set of random points inside the polygon. 10000 points should suffice, and is efficiently done given a triangulation.
6. Find the internal point (among all the random set) which has a maximum minimal distance to any edge of the polygon.
7. Use fminsearch to optimize the center of a circle which maximizes the minimum distance to any edge of the polygon. fminsearch will be entirely adequate here, since it is only a 2-dimensional problem. The start point will be the best point chosen in step 6.
0 commentaires
Voir également
Catégories
En savoir plus sur Bounding Regions dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!