PIXELBANKv8.2.1
Menu

Crop Image

Implement a function to extract a rectangular region from an image, given the top-left corner coordinates (row,col)(row, col) and the dimensions (height,width)(height, width). This task involves image processing and region of interest extraction.

The concept of cropping an image is fundamental in computer vision, as it allows for focusing on specific parts of an image, reducing noise, and improving processing efficiency. In mathematical terms, the cropped image can be represented as a subset of the original image pixels, defined by the bounds rowyrow+heightrow \leq y \leq row + height and colxcol+widthcol \leq x \leq col + width.

To achieve this, consider the following steps:

  1. Identify the top-left corner coordinates (row,col)(row, col).
  2. Determine the dimensions (height,width)(height, width) of the region to be extracted.
  3. Extract the sub-image using the specified bounds.
Cropped Image={(x,y)rowyrow+height,colxcol+width}\text{Cropped Image} = \{(x, y) | row \leq y \leq row + height, col \leq x \leq col + width\}

This technique is widely used in image editing and object detection applications.

Example:

Input:
crop([[1,2,3],[4,5,6],[7,8,9]], 0, 0, 2, 2)
Output:
[[1,2],[4,5]]
Reasoning:

Extract 2x2 region from top-left corner

Constraints:

  • The crop region must be within image bounds
  • Return the cropped sub-image
Editor

Test Results

0/0
Run code to see test results.