A robot camera pipeline may rotate a captured image by 90 degrees before displaying it or passing it to a later vision stage. If the image has pixel annotations, the same transformation must move those annotations. Rotating the pixel array alone can leave a feature marker at an incorrect coordinate, even though the marker remains inside the new image.
This example uses a synthetic two-row image and was checked with NumPy 2.5.2. It demonstrates an exact permutation of the stored pixels. The example does not alter the optical pose of a camera or perform a physical recalibration.
Start with row and column indexing
For the input image, use u for the horizontal column index and v for the vertical row index. Both indices start at zero. The array is indexed as image[v, u]. With an upper-left origin, increasing u moves right and increasing v moves down. The example has height two and width three. A marker at (u, v) = (2, 0) belongs to the pixel with value 30 in the upper-right corner.
import numpy as np
image = np.array([[10, 20, 30],
[40, 50, 60]])
height, width = image.shape
u, v = 2, 0
assert image[v, u] == 30
Make the direction and spatial axes explicit
The default positive rotation of a two-dimensional array using NumPy's rot90 is counterclockwise with the usual displayed orientation. It moves the original right column into the first output row. After the turn, there are three rows and two columns. The original upper-right value is in the upper-left corner.
turned = np.rot90(image, k=1, axes=(0, 1))
assert turned.shape == (3, 2)
assert turned.tolist() == [[30, 60],
[20, 50],
[10, 40]]
For this convention, the rotated column index is v and the rotated row index is the original width minus one minus u. The original first row runs along the output's first column, and the reversal of horizontal position determines the output row. The width comes from the input, so the same formula applies even when the dimensions swap.
new_u = v
new_v = width - 1 - u
assert (new_u, new_v) == (0, 0)
assert turned[new_v, new_u] == image[v, u]
Using height in place of width would give a row index of -1 for this marker. NumPy permits negative indices and would select the last row. Bounds checks should therefore enforce a nonnegative coordinate before indexing when the image convention uses only forward pixel indices. An annotation check that compares the source pixel identity with its destination catches a mismatch even when the outputs otherwise have plausible values.
Move masks and other pixel-aligned fields together
An annotation stored as a pixel mask can be rotated using the same spatial axes. In the fixture, a mask with one true value at the original upper-right corner has its true value at the rotated upper-left corner. The image values and mask still use the same row and column coordinates.
mask = np.zeros_like(image, dtype=bool)
mask[v, u] = True
turned_mask = np.rot90(mask, axes=(0, 1))
assert turned_mask.tolist() == [[True, False],
[False, False],
[False, False]]
An image batch adds another ordering decision. If its axes are frame, height, width, and channel, the spatial plane consists of axes one and two. The default axes zero and one would instead involve frames. Label the array axes in the interface and choose the proper pair when rotating.
batch = np.stack([image, image + 100])[..., None]
turned_batch = np.rot90(batch, axes=(1, 2))
assert turned_batch.shape == (2, 3, 2, 1)
The pixel plane is the same in both frames, and the single channel stays attached to its original frame. A downstream interface may also require contiguous storage. NumPy documents that rot90 returns a view, so copy it if an interface requires contiguous independent storage or if the original array may be modified independently.
Check the coordinate convention for each annotation
The mapping above uses pixel-center indices from zero through width minus one. Bounding boxes may use continuous edges or exclusive endpoints. Verify the annotation format and transform its corners under the rule for that format before recomputing the box limits. Applying a pixel-center formula to an edge coordinate can shift the resulting box. If the next stage back-projects pixels using the original camera model, map the rotated coordinates back to the original image convention or update the model consistently.
The inverse mapping for the point convention is also useful. If an annotation is returned in the rotated image at (new_u, new_v), the original coordinates are (width minus one minus new_v, new_u). Store the original image dimensions when the feature is sent to a separate service. The image height at the receiving stage may already refer to the rotated array, so explicit origin metadata makes that dependency easier to audit.
for row in range(height):
for col in range(width):
assert turned[width - 1 - col, row] == image[row, col]
assert np.array_equal(np.rot90(turned, k=-1, axes=(0, 1)), image)
A non-square test image shows that the dimensions swap and helps catch the wrong extent in the coordinate mapping. Distinct corner values expose the direction of the rotation. The fixture also checks that the opposite rotation recovers the original image. Pair that check with an explicit forward coordinate assertion. A round trip can succeed even when two consistently reversed operations were applied relative to the intended display direction.
Sources checked November 28, 2026
NumPy, numpy.rot90
https://numpy.org/doc/stable/reference/generated/numpy.rot90.html
NumPy, Indexing on ndarrays
https://numpy.org/doc/stable/user/basics.indexing.html
Illustration. AI-generated concept showing a marked upper-right cell becoming the upper-left cell after an image quarter-turn.