rotating a matrix without interpolation
显示 更早的评论
Hey,
I want to rotate a mtrix by a certain degree without interpolating the intermediate pixels and just place NaN in them.
I tried to use imrotate but there is no option to do it without interpolating.
Any suggestions?
Thank in advence.
Oded
回答(1 个)
Walter Roberson
2020-12-2
1 个投票
When you rotate a matrix, then the only pixels that can be calculated without any interpolation are the ones whose coordinates are such that the ratio of coordinates is exactly an integer multiple of tan() of the rotation angle. For example if the rotation is 42.7093899573615 degrees then there are some pixels whose coordinates happen to fall on the famous (5, 12, 13) pythagorean triple, and those single pixels could be copied without any interpolation. Every other pixel would require interpolation.
Are you sure you want to construct a matrix of nan that is the appropriate size to hold the rotated matrix, with the entire matrix nan except for the few scattered pixels that just happen to be at exactly the right coordinates to be carried from integer position to integer position by the rotation?
7 个评论
Oded Scharf
2020-12-2
M=[1:3;4:6;7:9]
M_after_45_rot=[0 0 1 0 0; 0 4 NaN 2 0; 7 NaN 5 NaN 3; 0 8 NaN 6 0; 0 0 9 0 0]
You appear to be rotating around the center, 5.
Rotations preserve distances.
The 1 starts at at distance sqrt(2) away from the 5. After the rotation, it has to end up at distance sqrt(2). The points that are distance sqrt(2) away from the central 5 in your result matrix are
0 0 1 0 0
0 * NaN * 0
7 NaN 5 NaN 3
0 * NaN * 0
0 0 9 0 0
Those points (marked with *) are 0 degrees, 90 degrees, 180 degrees, and 270 degrees rotation relative to the original 1. Therefore, by symmetry, the original 1, 3, 9, and 7 must map to NaN -- if they go anywhere discrete they do not preserve distances as rotations must.
OKay, how about the original 2, where does it map to? Rotations preserve distances, and it starts 1 unit away, and the output locations that are 1 unit away from the 5 are
0 0 1 0 0
0 4 * 2 0
7 * 5 * 3
0 8 * 6 0
0 0 9 0 0
but those are 0, 90, 180, 270 degrees from the original 2 position. Therefore, by symmetry, the original 2, 6, 8, 4 must go to NaN as the locations you would have them move to do not preserve distances.
In the expanded 5 x 5 square, the outer values correspond to distances 2, sqrt(5) and sqrt(8) from the center 5. No points are that distance from the central 5 in the original image, so all of them would require interpolation, so you require that all of them would be NaN as well.
Thus, the 3 x 3 case would have to expand to 5 x 5 all nan except for the central 5 that does not move.
The rotation matrix is
[cos(theta), -sin(theta); sin(theta), cos(theta)]
with theta = pi/2 that would be
[sqrt(2), -sqrt(2); sqrt(2), sqrt(2)]
For cordinates (x, y) relative to center (xc, yc), and translating back after rotation, the coordinates would map to
[xc + sqrt(2)/2*(x - xc) + sqrt(2)/2*(y - yc),
yc - sqrt(2)/2*(x - xc) + sqrt(2)/2*(y - yc)]
By examination, we can see that the first coordinate is an integer either when x == xc and y == yc, or when (x - xc = -(y - yc). The first of those conditions is the condition of being located right at the center. For the second possibilty, let us consider 3 x 3 with center (2,2), then for example consider (1,3), then (x-xc) = -1 and (y-yc) = 1, and we see that [1,3] rotates around to a position with x coordinate exactly 2.
But, the second coordinate we can see by examination is an integer either when x == xc and y == yc, or when (x - xc) = (y - yc) -- that (1,3) original coordinate in the 3 x 3 is going to rotate around to y coordinate 2 + sqrt(2) which is not an integer.
Is it possible for (x-xc) = -(y - yc) to be an integer in the second coordinate?
yc - sqrt(2)/2*(x - xc) + sqrt(2)/2*(-(x-xc)) ==>
yc - 2*sqrt(2)/2*(x-xc) ==>
yc - sqrt(2)*(x-xc)
and that can only be an integer with integer coordinates if x - xc = 0. But if x - xc = 0 then with x-xc = -(y-yc) then 0 = -(y-yc) implies y - yc = 0... which is back to the coordinates of the center only.
Therefore if you rotate by 45 degrees or -45 degrees, the only integer coordinates that can map to integer coordinates are the coordinates of the center of rotation. And thus the output has to be nan except for the center piece as no interpolation is permitted.
Rotating by 90 or -90 degrees is, of course, a very different matter, rot90(A,1), rot90(A,-1) give those to you, and every integer coordinate maps to an integer coordinate.
Oded Scharf
2020-12-2
Oded Scharf
2020-12-5
Walter Roberson
2020-12-5
Sorry, I did not notice your earlier reply.
At the moment I do not see enough to establish a consistent pattern, but if you can show the result you would want for transforming a 5x5 perhaps that would be enough.
Walter Roberson
2020-12-5
I notice that in your desired result that each location ends up sqrt(2) times the original distance. Items that were 1 unit end up on the diagonal sqrt(2) away. Items that were sqrt(2) away end up 2 away. Is that the pattern? We might be able to work with that.
Oded Scharf
2020-12-6
类别
在 帮助中心 和 File Exchange 中查找有关 Interpolation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!