Milestone 5 of 8
Implement and hand-check the 3 × 3 mean rule
Repeat nearest edge values, accumulate each channel in a wider integer dtype, divide by nine with floor division, and check center, edge, and corner pixels.
The local mean filter is a small neighborhood rule that can be calculated by hand. State its edge behavior before writing the loop.
Goal
Implement the supplied 3 × 3 mean rule for grayscale and RGB arrays and
hand-check center, edge, and corner pixels.
Rule
For every pixel and channel, take the surrounding 3 × 3 values. Outside the
image, repeat the nearest edge value. Add in a wider integer dtype and use
floor division by 9. Return an independent uint8 array with the same shape.
This is a local averaging rule supplied for practice. It is not a learned filter, an optimal filter, or a replacement for the later lesson on convolution.
Deliverables
Implement mean_3x3(image) in src/operations.py. Save hand calculations and
pixel checks for tiny grayscale and RGB fixtures, including the center, all
four corners, and at least one edge pixel. Record input and output shape,
dtype, range, and selected values.
Checks
Use a one-pixel image, a narrow image, a constant image, a gradient, and a checker pattern. Check edge repetition at corners and edges, channel independence, wider accumulation, and floor division. For a constant image, the output should remain constant. Check that applying the operation twice does not mutate the first result or its source.
Compare every selected pixel with a hand-computed value. Reject arrays outside the toolkit shape, dtype, or range contract. Check grayscale and RGB output shapes exactly.
Workspace
Keep the filter and its numerical fixtures in src/operations.py. Do not add
convolution terminology, learned parameters, or model comparisons.
Hints
HintCount nine values
HintChannels do not mix
Review
Calculate the output at the top-left corner of a two-by-two pattern by hand. Which source value is repeated, and why? What does this rule say about its local arithmetic, and what does it not say about visual quality?
How to check your work
Checks compare center, edge, and corner values, shape, dtype, range, and source immutability with the hand fixtures. The rule is exact and small by design.