Rotate a Matrix by Finding an Axis

조회 수: 2 (최근 30일)
Pedro Sousa
Pedro Sousa 2018년 12월 31일
댓글: Walter Roberson 2019년 1월 2일
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.

채택된 답변

Bruno Luong
Bruno Luong 2019년 1월 2일
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')

추가 답변 (1개)

Walter Roberson
Walter Roberson 2018년 12월 31일
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
Pedro Sousa
Pedro Sousa 2019년 1월 2일
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 2019년 1월 2일
Are you looking for connected distance or euclidean distance ?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by