calculating angle between line of best fit and x axis

17 vues (au cours des 30 derniers jours)
C.G.
C.G. le 7 Oct 2021
Commenté : Star Strider le 7 Oct 2021
I have a set of coordinate points from a picture. I have used polyfit to find the line of best fit of these, but I want to find the angle between the line of best fit and the x axis, so the angle the pile of grains is sitting at. Can somebody tell me how I can do this?
%find the circles and get the coordinates
I = imread('testpic1.png');
imshow(I)
[X,Y]=meshgrid(1:size(I));
Rmin = 10;
Rmax = 20;
[cd, rd] = imfindcircles(I,[Rmin Rmax], 'ObjectPolarity', 'dark', 'Sensitivity', 0.94, 'Method','TwoStage');
viscircles(cd, rd, 'Color', 'b');
subplot(1,3,1)
imshow(I)
hold on
scatter(cd(:,1), cd(:,2),'r*')
cd = cd(cd(:,2) <315, :)
subplot(1,3,2)
imshow(I)
hold on
scatter(cd(:,1), cd(:,2),'r*')
%plot line of best fit
p = polyfit(cd(:,1),cd(:,2),1);
Bfit = polyval(p,cd(:,1));
subplot(1,3,3)
imshow(I)
hold on
scatter(cd(:,1),cd(:,2))
hold on
plot(cd(:,1),Bfit,'-r')

Réponse acceptée

Star Strider
Star Strider le 7 Oct 2021
The slope is simply the tangent, so —
slope_angle = atand(p(1)) % Slope Angle (°)
Here, that comes out to be about 14.6°. The slope appears negative in the image plot because of the axes orientation being different in the images. Plotting it with both axes in their normal orientation (added at the end) shows it to be positive.
%find the circles and get the coordinates
I = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/760726/testpic1.png');
imshow(I)
[X,Y]=meshgrid(1:size(I));
Rmin = 10;
Rmax = 20;
[cd, rd] = imfindcircles(I,[Rmin Rmax], 'ObjectPolarity', 'dark', 'Sensitivity', 0.94, 'Method','TwoStage');
viscircles(cd, rd, 'Color', 'b');
subplot(1,3,1)
imshow(I)
hold on
scatter(cd(:,1), cd(:,2),'r*')
cd = cd(cd(:,2) <315, :)
cd = 17×2
124.1955 220.2184 68.5603 219.1154 274.1195 266.7293 194.0057 236.0971 322.3071 281.7616 225.4457 250.7806 455.6494 314.4799 376.8527 296.7009 81.8340 218.4861 255.0560 263.2335
subplot(1,3,2)
imshow(I)
hold on
scatter(cd(:,1), cd(:,2),'r*')
%plot line of best fit
p = polyfit(cd(:,1),cd(:,2),1)
p = 1×2
0.2602 197.6515
Bfit = polyval(p,cd(:,1));
subplot(1,3,3)
imshow(I)
hold on
scatter(cd(:,1),cd(:,2))
hold on
plot(cd(:,1),Bfit,'-r')
slope_angle = atand(p(1)) % Slope Angle (°)
slope_angle = 14.5834
figure
plot(cd(:,1),cd(:,2),'.')
hold on
plot(cd(:,1),Bfit,'-r')
hold off
axis('equal')
title(sprintf('Slope = %.1f°',slope_angle))
.
  2 commentaires
C.G.
C.G. le 7 Oct 2021
Thank you!
Star Strider
Star Strider le 7 Oct 2021
As always, my pleasure!
.

Connectez-vous pour commenter.

Plus de réponses (1)

Alan Stevens
Alan Stevens le 7 Oct 2021
Modifié(e) : Alan Stevens le 7 Oct 2021
Take the arctangent of the gradient of the straight line.
  1 commentaire
C.G.
C.G. le 7 Oct 2021
how do i get the gradient from my code above?

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by