MATLAB: How to discover intersecting circles

concentric circlesintersecting circles

I have two types of intersecting circles in images, created by the Matlab imfindcircles function, which returns a matrix of centroid (x, y) and a vector with the radii of the circles (r):
1- Concentric circles;
2 – Circles intersecting.
I need to identify all of these cases and apply two functions to treat these intersections, one for case 1 (function1) and one for case 2 (function2). These functions will have as parameters a matrix containing the centroides and the radii of the circles in each case. I just need a program that reports the parameters of the two functions and when to call one or the other.

Best Answer

Try this:
% Create sample data.
numCircles = 10;
xy = rand(numCircles, 2);
radii = 0.1 * rand(numCircles, 1) + 0.1;
viscircles(xy, radii);
% Label them so we know what circle is what.
for k = 1 : numCircles
text(xy(k, 1), xy(k, 2), num2str(k));
end
axis square;
% See if any intersect
for k = 1 : numCircles
% Compute the distance between centers.
distances = sqrt((xy(k, 1) - xy(:, 1)).^2 + (xy(k, 2) - xy(:, 2)) .^2);
sumOfRadii = radii(k) + radii(:);
intersectingIndexes = distances < sumOfRadii;
% Don't compare circle to itself. Of course that one intersects!
intersectingIndexes(k) = false;
% Convert to linear indexes
intersectingIndexes = find(intersectingIndexes);
for k2 = 1 : length(intersectingIndexes)
fprintf('Circle %d intersects with circle #%d.\n', k, intersectingIndexes(k2));
end
end
You see
Circle 1 intersects with circle #2.
Circle 1 intersects with circle #5.
Circle 1 intersects with circle #7.
Circle 2 intersects with circle #1.
Circle 2 intersects with circle #5.
Circle 3 intersects with circle #7.
Circle 3 intersects with circle #9.
Circle 5 intersects with circle #1.
Circle 5 intersects with circle #2.
Circle 6 intersects with circle #10.
Circle 7 intersects with circle #1.
Circle 7 intersects with circle #3.
Circle 8 intersects with circle #9.
Circle 9 intersects with circle #3.
Circle 9 intersects with circle #8.
Circle 10 intersects with circle #6.
and it's trivial to find out if one circle is completely contained within another. See if you can do it.