Click4Ai

139.

Easy

Data Augmentation (Image Rotation)

Implement image rotation as a data augmentation technique. Data augmentation artificially expands the training dataset by applying random transformations to existing images. Rotation is one of the most common augmentations -- it helps the model learn rotation-invariant features and reduces overfitting.

Algorithm:

1. Choose a rotation angle: 90, 180, or 270 degrees

2. Apply the rotation to the image matrix

Rotation by 90° counter-clockwise:

For a matrix A of shape (H, W):

A_rotated[i, j] = A[j, H-1-i]

Using NumPy: np.rot90(image, k=1)

Rotation effects:

90°: transpose then flip left-right

180°: flip both axes

270°: transpose then flip up-down

Example:

Input image (3x3):

[[1, 2, 3],

[4, 5, 6],

[7, 8, 9]]

Rotate 90° counter-clockwise (k=1):

[[3, 6, 9],

[2, 5, 8],

[1, 4, 7]]

Rotate 180° (k=2):

[[9, 8, 7],

[6, 5, 4],

[3, 2, 1]]

Rotate 270° counter-clockwise (k=3) = 90° clockwise:

[[7, 4, 1],

[8, 5, 2],

[9, 6, 3]]

**Explanation:** Data augmentation is crucial for training robust deep learning models, especially when training data is limited. By applying transformations like rotation, flipping, scaling, and color jittering, the model sees varied versions of each training image. This teaches the model to be invariant to these transformations, improving generalization to unseen data. Rotation augmentation is particularly useful for tasks like satellite image classification, medical imaging, and object detection where objects can appear at any orientation.

Constraints:

  • Input image is a 2D numpy array of shape (H, W)
  • Implement rotation by 90 degrees clockwise (equivalent to np.rot90 with k=3 or transposing and flipping)
  • Return the rotated image as a 2D numpy array
  • Use np.rot90 for the rotation operation
  • Test Cases

    Test Case 1
    Input: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    Expected: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]
    Test Case 2
    Input: [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
    Expected: [[70, 40, 10], [80, 50, 20], [90, 60, 30]]
    + 3 hidden test cases