Loading problem...
In computer vision and image processing, mirror transformations (also known as reflection or flip operations) are fundamental geometric operations that create a mirrored version of an image along a specified axis. These transformations are extensively used in data augmentation pipelines for deep learning, where they artificially expand training datasets by generating semantically equivalent variations of existing images.
A mirror transformation operates on a 2D or 3D array representing pixel values:
Types of Mirror Transformations:
1. Horizontal Mirror (Left-Right Reflection):
The image is reflected along the vertical axis running through the center. Each row of pixels is reversed, causing the left side to swap with the right side. Mathematically, for a row [p₁, p₂, ..., pₙ], the result is [pₙ, ..., p₂, p₁].
2. Vertical Mirror (Top-Bottom Reflection):
The image is reflected along the horizontal axis running through the center. The order of rows is reversed, causing the top to swap with the bottom. For an image with rows [row₁, row₂, ..., rowₘ], the result is [rowₘ, ..., row₂, row₁].
Your Task: Implement a function that applies a mirror transformation to an input image based on the specified direction. The function must handle both grayscale (2D) and color (3D) images, validate all inputs, and return appropriate error indicators for invalid cases.
Validation Requirements:
image = [[1, 2, 3], [4, 5, 6]]
direction = 'horizontal'[[3, 2, 1], [6, 5, 4]]A horizontal mirror reverses each row independently. The original 2×3 grayscale image has rows [1, 2, 3] and [4, 5, 6]. After applying the horizontal reflection:
• Row 1: [1, 2, 3] → [3, 2, 1] (reversed) • Row 2: [4, 5, 6] → [6, 5, 4] (reversed)
The pixel originally at position (0, 0) with value 1 is now at position (0, 2), demonstrating the left-right swap behavior.
image = [[1, 2, 3], [4, 5, 6]]
direction = 'vertical'[[4, 5, 6], [1, 2, 3]]A vertical mirror reverses the order of rows while keeping each row's internal order unchanged. The original image has rows [1, 2, 3] (top) and [4, 5, 6] (bottom). After vertical reflection:
• The bottom row [4, 5, 6] moves to the top position • The top row [1, 2, 3] moves to the bottom position
This creates an upside-down version of the original image where the top-bottom orientation is inverted.
image = [[[255, 0, 0], [0, 255, 0]], [[0, 0, 255], [255, 255, 0]]]
direction = 'horizontal'[[[0, 255, 0], [255, 0, 0]], [[255, 255, 0], [0, 0, 255]]]This example demonstrates horizontal mirroring on a 2×2 RGB color image. Each pixel contains three values representing [Red, Green, Blue] intensity channels.
Original image layout: • Position (0,0): [255, 0, 0] = Pure Red • Position (0,1): [0, 255, 0] = Pure Green • Position (1,0): [0, 0, 255] = Pure Blue • Position (1,1): [255, 255, 0] = Yellow
After horizontal flip, pixels within each row are swapped: • Row 0: Red and Green swap positions • Row 1: Blue and Yellow swap positions
The result maintains the RGB structure of each pixel while reversing their horizontal arrangement.
Constraints