drawing a best fit graph
Show older comments
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
on 2 Mar 2021
Can you provide data or code to produce the plot shown?
Answers (1)
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
on 2 Mar 2021
emeka onwochei
on 2 Mar 2021
Walter Roberson
on 2 Mar 2021
why did you have [y,x] = find(~I) ; instead of [x,y]?
If you have a table of data, such as a logarithm table

then would find data by referring to "row 7, column 3" -- vertical distance named first, and then horizontal distance named.
This is the order that MATLAB uses for arrays. The first index is vertical distance, and the second index is horizontal distance.
However, the convention for Cartesian coordinates is that the independent variable, x, is along the vertical axes and is named first, followed by the dependent variable on the vertical axes second.
Row is vertical distance. Vertical distance is y. Therefore, row coordinates correspond to y, and column coordinates correspond to x.
Walter Roberson
on 2 Mar 2021
why is it that when i remove the line imshow, the graph switches on the y axis.
imshow() does an automatic
set(gca,'Ydir','reverse')
unless hold is on for the axes. When you are dealing with images, it is very common to think of them in terms of distance down from the top, instead of in terms of distance up from the bottom (Cartesian coordinates.)
Yes, three incompatible conventions.
emeka onwochei
on 2 Mar 2021
KSSV
on 2 Mar 2021
Pick a point on circle, where you want a tangent. You have center and point in hand; get the slope, get the slope of tangent. Use point slope formula.
emeka onwochei
on 2 Mar 2021
emeka onwochei
on 3 Mar 2021
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')
Refer the link https://in.mathworks.com/matlabcentral/fileexchange/19083-calccircle for the function calcircle.
Categories
Find more on Matrix Indexing 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!
