how to rotate an array about the best fit line

2 views (last 30 days)
Hi,
I have some data taken from analysing circles in an image and want to be able to straighten the line as its being on a slope is an artefact of the scanning. I have fitted a best fit line and wanted to know if I could rotate the data until the best fit line was parallel with the x-axis? Any ideas or is this not possible?
Best regards,
Steve
close all
clear all
clc
centers=[1296.28613359921,15.9423957420192;1244.60658858455,16.5186862922104;390.379594009879,19;775.563580322536,16.9590472427110;256.101009617993,18.7276757275163;658.253941157542,18.2915405544281;289.482449670956,18.5636285011201;507.304238398890,20.3078955046754;692.113672662873,16.6656418306859;976.498832075336,16.4348647865777;222.603443863265,19.4801906970668;591.449235930304,18.8160349909942;624.831583420586,18.8490766293560;122.251099674639,18.5291400151088;1077.71234033461,17.1523494179665;4.98592612631937,20.4982856165640;893.025740410572,15.8528339538963;1112,16;876.389956316530,16.7010325410232;675.391032220651,18.9364395893170];
format short
%%Data Analysis
%using the data in centers and radii
hFig2=figure('units','normalized','outerposition',[0 0 1 1]);
set(0,'CurrentFigure',hFig2)
subplot(4,1,1);
%plot of centers position
SortedCenters=sortrows(centers,1)
x=SortedCenters(:,1);
y=SortedCenters(:,2);
title('Centers x y')
box on
grid minor
hold on
scatter(x,y)
set(0,'CurrentFigure',hFig2)
subplot(4,1,2);
scatter(x,y);
hold on
p = polyfit(x,y,1)
f = polyval(p,x)
plot(x,f)
xlabel('X pos')
ylabel('Ypos')
title('2.basic centers data with a least squares fit line')
box on
grid minor
hold on
format short
% how to straighten the plot?

Accepted Answer

Star Strider
Star Strider on 9 May 2018
If you want to eliminate the slope of the regression and plot your results parallel to the x-axis, subtract the fit at each point from the data at each point:
p = polyfit(x,y,1);
f = polyval(p,x);
plot(x, y-f)
If I understand correctly what you want to do, that should work.

More Answers (1)

Image Analyst
Image Analyst on 9 May 2018
Use p to get the angle that you can then use in imrotate() to straighten your image. I think it should be something like:
slope = p(1);
intercept = p(2);
angle = atan2d(slope);
rotatedImage = imrotate(originalImage, -angle);

Community Treasure Hunt

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

Start Hunting!