Pixel Neighborhood
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 a pixel at position (x,y) in an image can be represented as a square region of size n×n centered at (x,y). To handle boundary conditions, zero-padding is used, where pixels outside the image boundaries are assumed to have a value of 0.
To extract the neighborhood, consider the following steps:
- Calculate the half-size of the neighborhood (n/2) to determine the bounds.
- Iterate over the rows and columns within the calculated bounds.
- Check if each position is within the image boundaries.
This technique is widely used in image filtering applications.
Example:
get_neighborhood([[1,2,3],[4,5,6],[7,8,9]], 1, 1, 3)
[[1,2,3],[4,5,6],[7,8,9]]
3×3 neighborhood centered at (1,1) contains the entire 3×3 image
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