v9.1.1
Menu

Pixel Neighborhood

easyFREEWhat is Computer Vision?Image ProcessingCV: Introduction to Computer Vision

Implement a function to extract the neighborhood of pixels around a given position in an image. This task is essential in image processing as it involves analyzing local regions of an image.

In computer vision, many operations rely on considering local neighborhoods, such as filtering, where the value of a pixel is determined by its neighboring pixels. The neighborhood of the pixel at position (row,col)(\text{row}, \text{col}) in an image can be represented as a square region of size n×nn \times n centered at that pixel. To handle boundary conditions, zero-padding is used, where pixels outside the image boundaries are assumed to have a value of 00.

The neighborhood is defined by:

  1. The half-size ⌊n/2⌋\lfloor n/2 \rfloor, which sets how far the window reaches from the center in each direction.
  2. The window covers every row from row−half-size\text{row} - \text{half-size} to row+half-size\text{row} + \text{half-size} and every column from col−half-size\text{col} - \text{half-size} to col+half-size\text{col} + \text{half-size}, both ends inclusive.
  3. A window position inside the image takes the image's value there; a position outside the image takes 00.
n=neighborhood sizehalf-size=⌊n/2⌋bounds=(row−half-size,row+half-size),(col−half-size,col+half-size)n = \text{neighborhood size} \\ \text{half-size} = \lfloor n/2 \rfloor \\ \text{bounds} = (\text{row} - \text{half-size}, \text{row} + \text{half-size}), (\text{col} - \text{half-size}, \text{col} + \text{half-size})

Return the neighborhood as an n×nn \times n list of lists (one inner list per row, top to bottom), with the center pixel in the middle.

This technique is widely used in image filtering applications.

Example

Input
get_neighborhood([[1,2,3],[4,5,6],[7,8,9]], 1, 1, 3)
Output
[[1,2,3],[4,5,6],[7,8,9]]
Reasoning
  • Inputs: image is a 3×33 \times 3 grid of intensities 11–99 (row 00 at the top), row =1= 1, col =1= 1 pick the centre pixel (value 55), and size n=3n = 3 is the window's side length.
  • Bounds: the half-size is h=⌊3/2⌋=1h = \lfloor 3/2 \rfloor = 1, so the window covers rows 00 through 22 and columns 00 through 22, inclusive: 2h+1=32h + 1 = 3 positions each way.
  • Padding: valid indices here are 00–22, so all 99 window positions are inside the image. Nothing is zero-padded; each entry is the image value there.
  • Output: a 3×33 \times 3 list of lists, [[1,2,3],[4,5,6],[7,8,9]], with 55 in the middle. Here it equals the whole image.
  • Notice the edge: centred at (0,0)(0, 0) the window spans indices −1-1 to 11. The 55 positions with a −1-1 index lie outside and become 00, giving [[0, 0, 0], [0, 1, 2], [0, 4, 5]]. The window stays n×nn \times n wherever it sits.

Constraints

  • Image is a 2D array
  • Position (row, col) may be near edges
  • Neighborhood size n is odd (1, 3, 5, etc.)
  • Use zero-padding for out-of-bounds pixels

Learn this in

More from CV: Introduction to Computer Vision

solution.py

Test results

Sign in to run your code against the test cases.