PY-81

Apply and Check a Neighbourhood Mean

  • Medium–Hard
  • Image Filters
  • Python

Task

Write mean_3x3(image). The image is a non-empty three-dimensional NumPy array with shape (height, width, channels), dtype uint8, and axes in that order. For every output pixel and channel, take the nine values in its 3 × 3 neighbourhood. When a neighbour would be outside the image, use the nearest edge pixel instead (repeat the top, bottom, left, or right edge as needed).

Add the nine values in a wider integer dtype, use floor division by 9, and return an independent uint8 array with the same shape. This is a local mean rule, not a learned filter. Invalid dimensions or dtype may raise ValueError. Do not modify the input.

Example

At the top-left corner, the neighbourhood repeats the top-left value in the missing top and left positions. The center pixel therefore receives the ordinary nine-cell mean, while a corner uses repeated edge values rather than discarding neighbours.

Your implementation

You may import NumPy as np. Do not print or ask for input.