TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Image Rotation (Nearest Neighbor)

Computer Vision
Medium

Rotate a two-dimensional image counterclockwise around its center using nearest-neighbor sampling. The output must have the same height and width as the input image.

For an image with H rows and W columns, define the center row coordinate:

cy=H−12c_y = \frac{H - 1}{2}cy​=2H−1​

Define the center column coordinate:

cx=W−12c_x = \frac{W - 1}{2}cx​=2W−1​

Let a be the supplied angle in degrees. Convert it to radians:

θ=aπ180\theta = a\frac{\pi}{180}θ=a180π​

For each output position with row i and column j, subtract the image center to obtain dy = i - c_y and dx = j - c_x. Use inverse rotation to locate the corresponding source row:

sy=cy+dycos⁡(θ)+dxsin⁡(θ)s_y = c_y + dy\cos(\theta) + dx\sin(\theta)sy​=cy​+dycos(θ)+dxsin(θ)

Compute the corresponding source column:

sx=cx−dysin⁡(θ)+dxcos⁡(θ)s_x = c_x - dy\sin(\theta) + dx\cos(\theta)sx​=cx​−dysin(θ)+dxcos(θ)

Round s_y and s_x to the nearest integers using Python round. If both rounded coordinates are inside the input image, copy that source pixel. Otherwise, place 0 at the output position.

Here, i and j are output coordinates, s_y and s_x are source coordinates, and theta is the counterclockwise rotation angle in radians.

Return the rotated image as a two-dimensional list with the same dimensions as the input image.

Loading visualization...

Examples

Input: image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], angle_degrees = 0

Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Explanation: A zero-degree inverse mapping selects every original pixel.

Input: image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], angle_degrees = 180

Output: [[9, 8, 7], [6, 5, 4], [3, 2, 1]]

Hint 1

Map each output coordinate backward through the inverse rotation.

Hint 2

Round the source coordinates and use zero when they fall outside the image.

Requirements

  • Rotate around the image center, not the top-left corner
  • Use inverse mapping: for each output pixel, find the source pixel
  • Apply round() for nearest neighbor selection
  • Fill out-of-bounds pixels with 0

Constraints

  • Image has at least one pixel
  • angle_degrees can be any number (positive = counterclockwise)
  • Output has the same dimensions as the input
  • Out-of-bounds pixels are filled with 0
  • Time limit: 300 ms
Try Similar Problems
Bilinear InterpolationRotate Around ZHomogeneous TransformGaussian Blur KernelConv2d Image Filtering

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.
PrevNext

Image Rotation (Nearest Neighbor)

Computer Vision
Medium

Rotate a two-dimensional image counterclockwise around its center using nearest-neighbor sampling. The output must have the same height and width as the input image.

For an image with H rows and W columns, define the center row coordinate:

cy=H−12c_y = \frac{H - 1}{2}cy​=2H−1​

Define the center column coordinate:

cx=W−12c_x = \frac{W - 1}{2}cx​=2W−1​

Let a be the supplied angle in degrees. Convert it to radians:

θ=aπ180\theta = a\frac{\pi}{180}θ=a180π​

For each output position with row i and column j, subtract the image center to obtain dy = i - c_y and dx = j - c_x. Use inverse rotation to locate the corresponding source row:

sy=cy+dycos⁡(θ)+dxsin⁡(θ)s_y = c_y + dy\cos(\theta) + dx\sin(\theta)sy​=cy​+dycos(θ)+dxsin(θ)

Compute the corresponding source column:

sx=cx−dysin⁡(θ)+dxcos⁡(θ)s_x = c_x - dy\sin(\theta) + dx\cos(\theta)sx​=cx​−dysin(θ)+dxcos(θ)

Round s_y and s_x to the nearest integers using Python round. If both rounded coordinates are inside the input image, copy that source pixel. Otherwise, place 0 at the output position.

Here, i and j are output coordinates, s_y and s_x are source coordinates, and theta is the counterclockwise rotation angle in radians.

Return the rotated image as a two-dimensional list with the same dimensions as the input image.

Loading visualization...

Examples

Input: image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], angle_degrees = 0

Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Explanation: A zero-degree inverse mapping selects every original pixel.

Input: image = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], angle_degrees = 180

Output: [[9, 8, 7], [6, 5, 4], [3, 2, 1]]

Hint 1

Map each output coordinate backward through the inverse rotation.

Hint 2

Round the source coordinates and use zero when they fall outside the image.

Requirements

  • Rotate around the image center, not the top-left corner
  • Use inverse mapping: for each output pixel, find the source pixel
  • Apply round() for nearest neighbor selection
  • Fill out-of-bounds pixels with 0

Constraints

  • Image has at least one pixel
  • angle_degrees can be any number (positive = counterclockwise)
  • Output has the same dimensions as the input
  • Out-of-bounds pixels are filled with 0
  • Time limit: 300 ms
Try Similar Problems
Bilinear InterpolationRotate Around ZHomogeneous TransformGaussian Blur KernelConv2d Image Filtering

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

Accepts: number

You must run your code first.