drawing a best fit graph

조회 수: 4 (최근 30일)
emeka onwochei
emeka onwochei 2021년 3월 2일
댓글: KSSV 2021년 3월 4일
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
KALYAN ACHARJYA
KALYAN ACHARJYA 2021년 3월 2일
Can you provide data or code to produce the plot shown?

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

답변 (1개)

KSSV
KSSV 2021년 3월 2일
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
emeka onwochei
emeka onwochei 2021년 3월 3일
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 2021년 3월 4일
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')

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by