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]I[r,c], where rr identifies a row and cc 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×33\times3 kernel uses nine input locations for each output location.


An input image neighbourhood is connected to one output pixel.


For a kernel indexed by −a≤u≤a-a\leq u\leq a and −b≤v≤b-b\leq v\leq b, convolution computes the output by the weighted sum


J[r,c]=∑u=−aa∑v=−bbK[u,v]I[r−u,c−v].J[r,c]=\sum_{u=-a}^{a}\sum_{v=-b}^{b}K[u,v]I[r-u,c-v].


Here, KK is the kernel and JJ is the output array. The minus signs reverse the kernel's spatial offsets. Operationally, you can rotate the displayed kernel by 180∘180^\circ, 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∘180^\circ 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×33\times3 mean filter assigns weight 1/91/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 6060, the mean-filtered value is 9(60)/9=609(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×33\times3 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 three by three sensor image contains one high central intensity surrounded by smaller values.


a) Apply a 3×33\times3 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.


Jcentre=30+31+29+32+240+33+31+30+349=4909=54.444…J_{\text{centre}}=\frac{30+31+29+32+240+33+31+30+34}{9} =\frac{490}{9}=54.444\ldots


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×33\times3 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


29,30,30,31,31,32,33,34,240.29,30,30,31,31,32,33,34,240.


For nine values, the middle position is (9+1)/2=5(9+1)/2=5. The fifth value is 3131, so the central median-filter output is 31.


The value 240240 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×33\times3 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


(00003031032240).\begin{pmatrix} 0&0&0\\ 0&30&31\\ 0&32&240 \end{pmatrix}.


Its entries sum to ________, giving


Jzero=0+0+0+0+30+31+0+32+2409=37.0.J_{\text{zero}}=\frac{0+0+0+0+30+31+0+32+240}{9}=37.0.


Replication assigns each outside location the nearest boundary pixel. The corresponding neighbourhood is


(3030313030313232240).\begin{pmatrix} 30&30&31\\ 30&30&31\\ 32&32&240 \end{pmatrix}.


Its entries sum to ________, giving


Jreplication=4(30)+2(31)+2(32)+2409=54.0.J_{\text{replication}}=\frac{4(30)+2(31)+2(32)+240}{9}=54.0.


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 microscope image contains a sharp boundary between uniform dark and bright regions. Each interior pixel is replaced by the mean of its 3×33\times3 neighbourhood. Which effect should you expect near the boundary, away from the image border?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

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.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

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


J[r,c]=∑u=−11∑v=−11K[u,v]I[r−u,c−v].J[r,c]=\sum_{u=-1}^{1}\sum_{v=-1}^{1}K[u,v]I[r-u,c-v].


A three by three input patch appears beside an asymmetric three by three kernel.


Compute the centre output without clipping or rescaling. Give the dimensionless response to the nearest integer.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice