Rotation is a common augmentation: a cat tilted by ten degrees is still a cat. The obvious way to implement it is to take every input pixel and write it to its rotated position, but that leaves holes and collisions in the output, because rotated positions rarely land on pixel centres. The standard fix is inverse mapping: loop over the output pixels, rotate each coordinate backwards to find where it came from in the input, and read the input there. With nearest-neighbour sampling, round the source coordinate to the closest pixel. Output pixels whose source falls outside the image are filled with zero.
Implement rotate_nearest.
img has shape (H, W). Rotate counterclockwise by angle_deg degrees about the image centre ((H - 1) / 2, (W - 1) / 2). The output has the same shape.(r, c) with dy = r - cy and dx = c - cx, the source is sy = cos(a) * dy + sin(a) * dx + cy and sx = -sin(a) * dy + cos(a) * dx + cx, rounded with np.rint.0. For a square image, rotate_nearest(img, 90) equals np.rot90(img).Build the coordinate grids with np.meshgrid(..., indexing='ij') and gather with a boolean validity mask.
Rotating by 0 degrees must reproduce the image exactly.
No results yet
Run your code to check it against the examples, or submit to run every test.