drawing a best fit graph

5 views (last 30 days)
emeka onwochei
emeka onwochei on 2 Mar 2021
Commented: KSSV on 4 Mar 2021
is there a way to draw a best fit graph from a cut off circle, i have the [x y] coordinates for the first black points. now i want to use these coordinates to draw the exact shape on a graph. i tried polyfit but it doesnt give me the required shape
  1 Comment
KALYAN ACHARJYA
KALYAN ACHARJYA on 2 Mar 2021
Can you provide data or code to produce the plot shown?

Sign in to comment.

Answers (1)

KSSV
KSSV on 2 Mar 2021
I = imread('image.png') ;
I = rgb2gray(I) ;
[y,x] = find(~I) ;
% Get center
idx = boundary(x,y) ;
x = x(idx) ;
y = y(idx) ;
imshow(I)
hold on
plot(x,y,'r')
  9 Comments
emeka onwochei
emeka onwochei on 3 Mar 2021
this is the code i used to get the center, it works for other test images but for this. the output does not give me a tangent across the blue line. does your code pasted earlier have a way of getting the center of the ellipse?
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [xc,yc,R,a] = circfit(x,y)
%
% [xc yx R] = circfit(x,y)
%
% fits a circle in x,y plane in a more accurate
% (less prone to ill condition )
% procedure than circfit2 but using more memory
% x,y are column vector where (x(i),y(i)) is a measured point
%
% result is center point (yc,xc) and radius R
% an optional output is the vector of coeficient a
% describing the circle's equation
%
% x^2+y^2+a(1)*x+a(2)*y+a(3)=0
%
% By: Izhak bucher 25/oct /1991,
x=x(:); y=y(:);
a=[x y ones(size(x))]\[-(x.^2+y.^2)];
xc = -.5*a(1);
yc = -.5*a(2);
R = sqrt((a(1)^2+a(2)^2)/4-a(3));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[xfit,yfit,Rfit] = circfit(x,y);
plot(xfit,yfit,'*r','MarkerSize',10);
plinex=[xfit min(x)];
pliney=[yfit max(y)];
coefficients=polyfit(plinex,pliney,1);
slope=coefficients(1)
[x1p, index1] = min(plinex)
[x2p, index2] = max(plinex)
% Get y at those endpoints
y1p = pliney(index1);
y2p = pliney(index2);
% Compute the slope
perpSlope = -1 / slope
% Equation of line going through the left most x (at x1p) is
% (y - y1p) = perpSlope * (x - x1p)
y1 = perpSlope * (x1p - plinex(index1)) + y1p
y2 = perpSlope * (x2p - plinex(index1)) + y1p
line([x1p, x2p], [y1, y2], 'Color', 'r', 'LineWidth', 0.5)
KSSV
KSSV on 4 Mar 2021
I would suggest you this:
I = imread('image.png') ;
I = rgb2gray(I) ;
[y,x] = find(~I) ;
% Get center
idx = boundary(x,y) ;
x = x(idx) ;
y = y(idx) ;
%% Get circle radius and centre ;
% select three points randomly on circle
idx = [10 100 150] ;
pt1 = [x(idx(1)) y(idx(1))] ;
pt2 = [x(idx(2)) y(idx(2))] ;
pt3 = [x(idx(3)) y(idx(3))] ;
[C, R] = calcCircle(pt1, pt2, pt3) ;
%% Draw circle
th = linspace(0,2*pi) ;
xc = C(1)+R*cos(th) ;
yc = C(2)+R*sin(th) ;
imshow(I)
hold on
plot(xc,yc,'r')

Sign in to comment.

Categories

Find more on Graph and Network Algorithms 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!