PY-60

Track Image Coordinates through Crops and Flips

  • Medium
  • Image Geometry
  • Python

Task

Write transform_coordinates(coordinates, image_shape, crop, flip_vertical, flip_horizontal). coordinates is an (n, 2) integer NumPy array whose rows are (row, column). image_shape is (height, width). crop is the valid half-open rectangle (top, left, bottom, right) inside the image.

Keep only points inside the crop. Subtract the crop origin. If vertical flip is true, replace row r by crop_height - 1 - r; if horizontal flip is true, replace column c by crop_width - 1 - c. Apply vertical then horizontal.

Return (transformed, source_indices): an independent integer array of shape (kept, 2) and a one-dimensional integer array identifying each kept input row. Preserve source order. Empty results must have shapes (0, 2) and (0,).

Example

With crop (1, 2, 5, 7), point (2, 3) becomes (1, 1). After both flips it becomes (2, 3) because the cropped image has shape (4, 5).

Your implementation

You may import NumPy as np. Inputs satisfy the declared shapes and crop bounds. Do not modify an input, print, or ask for input.