Rotate a Matrix by Finding an Axis

2 views (last 30 days)
Hey, sorry to bother but I 'm having issues in a project. Long story short, I must find an axis on the 2D binary matrix (which is done by finding the maximum distance between two of the black points in the matrix) and then rotate said image to make it as straight as possible. I cannot use imrotate though I guess that part can be done with a rotation matrix and so, is pretty easy (still don't know how it works but I can look into that).
If anyone can help me at finding the axis please, it'd be most aprecciated. However, I do understand it is quite the unusual task.
Anyway, thank you and have yourself a jolly new year's eve.

Accepted Answer

Bruno Luong
Bruno Luong on 2 Jan 2019
Here is the code to find the largest distance between 2 white pixels.
Once finding it you can compute the angle and rotate the image (left as exercice)
largestdistance.png
% Generate test image data
Im = peaks(1000)>0.5;
% Find the largest distance of *white* pixels
[y,x] = find(Im);
K = convhull(x,y);
xk = x(K);
yk = y(K);
i2 = 1;
nk = length(K);
d2max = -Inf;
for i1=1:nk
xk_i1 = xk(i1);
yk_i1 = yk(i1);
d2 = (xk(i2)-xk_i1).^2+(yk(i2)-yk_i1).^2;
while true
i2temp = mod(i2,nk) + 1;
d2temp = (xk(i2temp)-xk_i1).^2+(yk(i2temp)-yk_i1).^2;
if d2temp <= d2
break
end
d2 = d2temp;
i2 = i2temp;
end
if d2 >= d2max
i1max = i1;
i2max = i2;
d2max = d2;
end
end
% Here are the pair of white pixels with largest size
x1 = xk(i1max);
y1 = yk(i1max);
x2 = xk(i2max);
y2 = yk(i2max);
% Graphical check
figure(1);
clf
imagesc(Im);
axis equal
hold on
plot([x1 x2],[y1 y2],'r-o')

More Answers (1)

Walter Roberson
Walter Roberson on 31 Dec 2018
find an axis on the 2D binary matrix (which is done by finding the maximum distance between two of the black points in the matrix) and then rotate said image to make it as straight as possible.
Those are not inherently compatible objectives.
Consider for example,
A
+
B+++++++++++++++++++++++++++++++++++++++++++++++++
Now the maximum distance between two of the black points is between A and B, so you would identify AB or BA as your axes. You would then rotate so that AB was the horizontal or vertical axes, which would require that the long line was at an angle. That disagrees with your requirement to "make it as straight as possible".
Once you have identified the angle of your axes, using atan2(dy, dx) then you can use makehgtform to construct a transform matrix, which you can then use to rotate your points. That will tell you the new location for the points... but the new locations are unlikely to be integral, so you need to figure out how you want to handle that to create a new image.
  3 Comments
Pedro Sousa
Pedro Sousa on 2 Jan 2019
My real issue is finding the most distant black points in the 2d binary matrix and making that an axis to be used after that.
Walter Roberson
Walter Roberson on 2 Jan 2019
Are you looking for connected distance or euclidean distance ?

Sign in to comment.

Categories

Find more on Line Plots 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!