Reputation: 1173
I have an image and several 2D points [(x, y), ...]
. I would like to rotate both the image and the points by 90 degrees.
For the image I can simply use np.rot90
. For the points I figured I have to rotate them 90 degrees around the center of the image to make them align correctly (for this I used https://stackoverflow.com/a/58781388/2445065)
Original image (note: red point is not part of the image):
After rotating the image and the point:
Whereas I would expect the red dot to be at the bottom left corner of the yellow square... Does anyone know what I'm doing wrong? Thanks in advance
Upvotes: 0
Views: 883
Reputation: 3677
This python function will do rot90 for points:
def rot90points(x, y, k, hw):
"""
a = np.zeros((5, 12), dtype="uint8");
x, y = np.array([2, 5, 7]), np.array([4, 4, 2])
a[y, x] = 200;
for k in range(10): # test
a1 = np.rot90(a, k);
x1, y1 = rot90points(x, y, k, a.shape);
assert np.all(a1[y1, x1] == 200)
"""
k = k % 4
if k == 0: return x, y
elif k == 1: return y, hw[1] - 1 - x
elif k == 2: return hw[1] - 1 - x, hw[0] - 1 - y
elif k == 3: return hw[0] - 1 - y, x
else: raise ValueError(f"k error {k}")
Upvotes: 1
Reputation: 1173
I finally found it:
To align the points and additional translation is require. np.rot90
does not equal a translation around the center but rather a rotation around the center PLUS a translation such that the top left corner of the rotated image is located at (0, 0)
.
You can get the translation by subtracting |origin_rotated.x - origin_original.x|
from the x-coordinate and |origin_rotated.y - origin_original.y|
from the y-coordinate.
Upvotes: 1