Image Filtering with Local Neighbourhoods
Image filtering transforms an image by applying a rule to nearby pixel values. You use it to suppress fluctuations, reduce isolated extreme values, or emphasize changes in intensity before further computation. Treating an image as an array makes these operations precise: the result depends on the neighbourhood, the rule applied within it, and the treatment of locations outside the image. These choices can change scientific measurements made from the filtered image.
From image arrays to a local rule
A greyscale image can be represented by an array I[r,c], where r identifies a row and c a column. Its entries represent sampled intensities. Digital intensity values are often dimensionless; their physical meaning depends on the imaging system and calibration.
A kernel is a small array of coefficients used to combine neighbouring pixel values. For an odd-sized kernel, its central entry identifies the location associated with the output pixel. A 3×3 kernel uses nine input locations for each output location.
For a kernel indexed by −a≤u≤a and −b≤v≤b, convolution computes the output by the weighted sum
Here, K is the kernel and J is the output array. The minus signs reverse the kernel's spatial offsets. Operationally, you can rotate the displayed kernel by 180∘, align its centre with the desired input location, multiply corresponding entries, and add the products.
Some software instead multiplies the displayed kernel directly against the displayed neighbourhood. That operation is correlation. A kernel unchanged by a 180∘ rotation gives the same result under either operation. An asymmetric kernel can give different results, so check the documented operation.
Choosing what the filter preserves
A 3×3 mean filter assigns weight 1/9 to every location. Its weights are nonnegative and sum to one, so each result lies between the smallest and largest values in its neighbourhood. It preserves a constant intensity wherever the full neighbourhood contains that constant.
Example: If all nine entries in an interior neighbourhood equal 60, the mean-filtered value is 9(60)/9=60.
Local rule | Computation | Main effect or limitation |
|---|---|---|
Mean filter | Average all neighbourhood values | Smooths fluctuations but mixes intensities across edges |
Unequal nonnegative weighted average | Use coefficients summing to one | Gives selected locations more influence while preserving constants |
Median filter | Sort the neighbourhood and select its middle value | Limits the influence of isolated extreme values but can remove small features |
Signed difference filter | Combine positive and negative weights | Responds to intensity changes and can produce negative values |
A median filter is nonlinear: it selects an order statistic rather than applying fixed weighted sums. It therefore cannot generally be represented by a convolution kernel. A signed kernel whose coefficients sum to zero produces zero on a constant neighbourhood; it measures change rather than preserving the original intensity level.
Boundaries and numerical representation
Near an image edge, some requested locations fall outside the stored array. Zero padding assigns zero to those locations. Replication uses the nearest boundary pixel, while reflection obtains values by mirroring the image according to a specified rule.
Boundary handling is part of the computation. For a positive constant image, zero padding lowers mean-filtered values near the border. Replication preserves that constant. Ignoring missing entries and dividing by the number available is another possible rule, but it differs from zero padding with a fixed divisor.
If an output must return to a restricted intensity range, specify when rounding and clipping occur. Clipping changes the mathematical operation: a linear weighted filter followed by clipping is generally nonlinear.
What to remember
- Specify the neighbourhood, coefficients, and convolution or correlation convention.
- Nonnegative weights summing to one preserve constant interior regions.
- Median filtering uses sorted values rather than fixed weighted sums.
- Boundary handling and numeric representation affect the result, even when the kernel is unchanged.
Example: Filtering a Small Sensor Image
A laboratory camera exports the entire 3×3 greyscale image shown below. Its intensity values are dimensionless. Compare two local filters, then investigate the effect of boundary handling. Use the original image for every calculation, retain full precision during arithmetic, and report averages to one decimal place.
a) Apply a 3×3 mean filter to the central pixel.
A mean filter replaces the selected pixel by the equal-weight average of its neighbourhood. The centre has a complete neighbourhood containing ________ entries, whose total is ________. No boundary rule is needed here.
The central mean-filter output is 54.4. This lies between the smallest and largest input values, as an average should.
b) Apply a 3×3 median filter to the central pixel. Explain why its result differs substantially from the mean.
A median filter replaces the selected pixel by the middle value of its sorted neighbourhood. Sorting the nine intensities gives
For nine values, the middle position is (9+1)/2=5. The fifth value is 31, so the central median-filter output is 31.
The value 240 raises the mean because it contributes directly to the sum. For the median, it occupies the largest position and does not determine the middle value. This explains the difference without assuming which intensity is the physically correct measurement.
c) Apply the 3×3 mean filter to the top-left pixel using zero padding and then replication padding. In both cases, keep the divisor equal to nine.
With zero padding, outside locations contribute zero. The neighbourhood centred on the top-left pixel is
Its entries sum to ________, giving
Replication assigns each outside location the nearest boundary pixel. The corresponding neighbourhood is
Its entries sum to ________, giving
The top-left outputs are 37.0 with zero padding and 54.0 with replication padding. The same averaging kernel gives different results because the boundary rule changes the supplied neighbourhood.
A camera produces an image with 9 rows and 12 columns. A program applies a kernel with 3 rows and 5 columns using valid filtering: it produces an output only where the entire kernel fits inside the image. The kernel moves one pixel at a time horizontally and vertically, with no padding.
Complete the output dimensions: the filtered image has rows and columns.
A scientific imaging routine applies the displayed kernel to the centre of the displayed input patch. The kernel origin is its central entry, and the routine uses convolution as defined by
Compute the centre output without clipping or rescaling. Give the dimensionless response to the nearest integer.