How to create a circles (smallest and biggest circle) based on points in an image given, then find the center and radius?

조회 수: 5 (최근 30일)
Hi,
I have extract some points from Image Scanning, I need to create 2 circle (smallest circle and largest circle) in order to calculate the error of circularity (Rmax - Rmin), in order to do that I need to find the center and radius. Did any have done this kind of case?
Below are some image as reference:
  1. Points extract from image scanning
test.GIF
2. Example of circles need to be done
example.GIF
Any help will be appreciated.
Thanks in advance

채택된 답변

Image Analyst
Image Analyst 2019년 1월 15일
To get the minimal bounding outer circle, try this:
screenshot.jpg
I asked John for the largest interior circle, and he said that's a much more difficult problem and he doesn't have code for that.
  댓글 수: 3
Jafar Nur Arafah
Jafar Nur Arafah 2019년 1월 16일
Hi Image Analyst, thanks for your response and understanding.
In your code, your creating your own randoms points, as for me, I already have the image with the points, so I change a little bit the code (by removing the coomand for plotting the random points and put the code to show the image and find the center, C (which I found that the coordoinate for C(x,y) = (217, 178)). Then I continue with the code you give me, but this error came out,
Error.GIF
Can you check if my code is correct.
I = imread('test.GIF');
[ny,nx] = size(I) ;
imshow(I) ;
hold on
% Center
C = round([nx ny]/2) ;
plot(C(1),C(2),'*r') % mid point/ center of image
hold on
x = 217
y = 178
hold on
% First fit a circle
[xcFit, ycFit, RFit, a] = circfit(x,y)
% Now find the max and min fit
% Convert to a binary image
% Scale by 500 to get enough pixels to get 3 digits of precision.
scaleFactor = 500;
xScaled = scaleFactor*x;
yScaled = scaleFactor*y;
rows = ceil(scaleFactor * max(y));
columns = ceil(scaleFactor * max(x));
binaryImage = poly2mask(xScaled, yScaled, rows, columns);
subplot(2, 2, 3);
imshow(binaryImage);
title('binaryImage', 'fontSize', fontSize);
axis('on', 'image');
hold on;
plot(xScaled, yScaled, 'b+', 'MarkerSize', 15, 'LineWidth', 2);
% Get the Euclidean Distance Transform
edtImage = bwdist(~binaryImage);
% Display the image.
subplot(1, 2, 2);
imshow(edtImage, []);
title('Distance Transform', 'fontSize', fontSize);
axis('on', 'image');
% Find the max
minRadius = max(edtImage(:))
[rowMin, colMin] = find(edtImage == minRadius, 1, 'first');
hold on;
% Plot the center of the minimum interior circle.
plot(colMin, rowMin, 'r+', 'MarkerSize', 20);
% Plot the scaled x, y
plot(xScaled, yScaled, 'b+', 'MarkerSize', 15, 'LineWidth', 2);
% Plot the circles
viscircles([xcFit * scaleFactor, ycFit * scaleFactor; colMin, rowMin], [RFit * scaleFactor; minRadius]);
% Unscale to original dimensions
xCenterMin = colMin / scaleFactor
yCenterMin = rowMin / scaleFactor
minRadius = minRadius / scaleFactor
% Print out the average/fitted circle, and the min circle.
fprintf('Fitted circle : Radius = %f at (x, y) = (%f, %f).\n', RFit, xcFit, ycFit);
fprintf('Min circle : Radius = %f at (x, y) = (%f, %f).\n', minRadius, xCenterMin, yCenterMin);
function [xc,yc,R,a] = circfit(x,y)
%CIRCFIT Fits a circle in x,y plane
%
% [XC, YC, R, A] = CIRCFIT(X,Y)
% Result is center point (yc,xc) and radius R. A is an optional
% output describing the circle's equation:
%
% x^2+y^2+a(1)*x+a(2)*y+a(3)=0
% by Bucher izhak 25/oct/1991
n=length(x); xx=x.*x; yy=y.*y; xy=x.*y;
A=[sum(x) sum(y) n;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;
xc = -.5*a(1);
yc = -.5*a(2);
R = sqrt((a(1)^2+a(2)^2)/4-a(3));
end
I also have save a function for circfit.m as per shown by KSSV yesterday.
function [xc,yc,R,a] = circfit(x,y)
%CIRCFIT Fits a circle in x,y plane
%
% [XC, YC, R, A] = CIRCFIT(X,Y)
% Result is center point (yc,xc) and radius R. A is an optional
% output describing the circle's equation:
%
% x^2+y^2+a(1)*x+a(2)*y+a(3)=0
% by Bucher izhak 25/oct/1991
n=length(x); xx=x.*x; yy=y.*y; xy=x.*y;
A=[sum(x) sum(y) n;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;
xc = -.5*a(1);
yc = -.5*a(2);
R = sqrt((a(1)^2+a(2)^2)/4-a(3));
end
I attached also the image I used 'test.GIF', for your reference.
Looking forward to your reply.
Thanks alot.
Nur Arafah Jafar
Jafar Nur Arafah
Jafar Nur Arafah 2019년 1월 16일
Hi Image Analyst,
As I run the code, figure below appear as the result.
Result.GIF
Regards,
Nur Arafah

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2019년 1월 15일
YOu can try to fit a circle with the coordinates (x,y) you have. Check the below code:
function [xc,yc,R,a] = circfit(x,y)
%CIRCFIT Fits a circle in x,y plane
%
% [XC, YC, R, A] = CIRCFIT(X,Y)
% Result is center point (yc,xc) and radius R. A is an optional
% output describing the circle's equation:
%
% x^2+y^2+a(1)*x+a(2)*y+a(3)=0
% by Bucher izhak 25/oct/1991
n=length(x); xx=x.*x; yy=y.*y; xy=x.*y;
A=[sum(x) sum(y) n;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;
xc = -.5*a(1);
yc = -.5*a(2);
R = sqrt((a(1)^2+a(2)^2)/4-a(3));
  댓글 수: 3
KSSV
KSSV 2019년 1월 15일
Ou are running the code in a wrong way...you have to save the above code into a function and call the function.
Image Analyst
Image Analyst 2019년 1월 16일
편집: Image Analyst 2019년 1월 16일
This fits a circle through the data, which could be part of the solution, but it does not find the max bounding circle (like my Answer).

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Polar Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by