How to draw a line through a set of points?

Hi, I have an image and a set of points present in the image. These points are not aligned with each other and I would like to draw a line that intersects the points

3 Commenti

Do you have the coordinates of these points?
question continued here
@Valerio: please don't open a new equivalent question.
PLEASE DON'T ASK THE SAME QUESTION REPEATEDLY!
I've deleted my answer. since you already have answers to your last question.

Accedi per commentare.

 Risposta accettata

You forgot to attach your points, which would have been helpful.
Since your definition of "through" is a regression rather than to have the line connect all the points, use polyfit()
coefficients = polyfit(x, y, 1);
%xFit = x; % Option 1 : same number of points as the training set.
xFit = linspace(min(x), max(x), 2000); % Option 2 : lots of points, and not just where the training points are.
yFit = polyval(coefficients, xFit);
plot(x, y, 'r+', 'LineWidth', 2, 'Markersize', 15)
hold on;
plot(xFit, yFit, 'm-', 'LineWidth', 2)

7 Commenti

I have some points in an immage, the point have x,y coordinate.
If i have 70 point what option i have to do?
The code should work with x and y each having 70 points, though I didn't test it. Did you try it? If it doesn't work, attach your data (x and y points in a .mat file). If you want the points to be plotted over an image instead of just a graph, then attach your image also.
How many output locations do you want your regression fit to be evaluated at? At the same 70 x values as the training set? At more? If so, how many more?
I have an immage, i have indivituated some points in the immage where the intensity is more than 210.
The immage that i have is this:
Now i want to do a regression with this point to obtain a line. I used yout method and it worked but I'm asking if this is the corret things to do.
The code will fit a line through those. If that's what you want to do, then it's the correct thing to do. Why do you have doubts? Why do you want a line anyway?
Ok niìow i dont have any dubts! I want a line becouse i have to intersecate this line with an other and obtain the angle
Please attach your original image without the cyan markers on it, so I can get the points via thresholding:
mask = grayImage >= 200;
[y, x] = find(mask);
hold on
plot(x, y, 'c.');
hold off;
or else, supply me with a list of the x,y points you want to use.
And tell me the definition of the other line so I can find the angles of both of them and the acute angle between them.
No it's all right i find what i nedd thanks a lott!!1

Accedi per commentare.

Più risposte (1)

If you have the coordinates then you can use plot()
img; % your image
x; % x-coordinates of points
y; % y-coordinates of points
imshow(img);
hold on
plot(x, y)

6 Commenti

And if i have more than one point, for example the coordinate of a set of point.
A=[x,x,x,x,x,x,x]
B=[y,y,y,y,y,y,y]
what i have to do?
The answer assumes that x and y are vectors with several points. You can try
plot(A, B)
With plot(A,B) i'm nott plottinng a line but only the point. I want to plot the line
This is strange behaviour. Normallt
plot(A, B)
should draw a line. But If you do something like this
plot(A, B, '+')
then only markers are shown.
Can you show your code?
how can i store that curve produced using plot(x,y) in a variable.
You can store a handle to the graphics object. Is that what you mean by storing the curve?

Accedi per commentare.

Categorie

Community Treasure Hunt

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

Start Hunting!

Translated by