Image Filtering Notes

Image Filtering
  • Image filtering involves replacing each pixel with a weighted average of its neighborhood to reduce noise.

  • The weights are determined by a filter kernel.

1D Case
  • A 1D filter is applied to a 1D signal by convolving the filter with the signal.

    • For example, given a filter [1/3,1/3,1/3][1/3, 1/3, 1/3] and a signal [10,12,9,11,10,11,12][10, 12, 9, 11, 10, 11, 12], the output is calculated by taking the weighted average of the signal values under the filter.

  • When applying a filter, you lose pixels at the edges because the filter extends beyond the signal at these points.

  • Several strategies exist for handling boundaries:

    1. Zero Padding: Assume values outside the image are zero.

    2. Clamp to Edge: Replicate the edge pixels.

    3. Wrap Around: Treat the image as repeating (useful for periodic patterns).

    4. Crop: Simply crop the output to the valid region.

Image Filtering in 2D
  • In 2D, a similar concept applies where a 2D filter kernel is used to compute the weighted average of a pixel's neighborhood.

  • Given an image II and a filter kernel FF, the filtered image II' is given by: I(x,y)=<em>u=kk</em>v=kkF(u,v)I(x+u,y+v)I'(x, y) = \sum<em>{u=-k}^{k} \sum</em>{v=-k}^{k} F(u, v) \cdot I(x+u, y+v)

  • Where [k,k]\begin{bmatrix} -k, -k \end{bmatrix} to [k,k]\begin{bmatrix} k, k \end{bmatrix} defines the neighborhood around the pixel (x,y)(x, y).

Example Filters
  1. Box Filter

    • All weights are equal

    • F=1(2k+1)2[1amp;1amp;1 1amp;1amp;1 1amp;1amp;1]F = \frac{1}{(2k+1)^2} \begin{bmatrix} 1 &amp; 1 &amp; 1 \ 1 &amp; 1 &amp; 1 \ 1 &amp; 1 &amp; 1 \end{bmatrix}

    • This computes the average value in the neighborhood.

  2. Gaussian Filter

    • Weights are based on a Gaussian distribution.

    • F(x,y)=12πσ2ex2+y22σ2F(x, y) = \frac{1}{2 \pi \sigma^2} e^{-\frac{x^2 + y^2}{2 \sigma^2}}

    • This gives more weight to closer pixels.

    • Approximates the solution to the diffusion equation.

  3. Derivative Filters

    • Used to compute derivatives of the image.

    • Examples include the Sobel operator:

    • Dx=[1amp;0amp;1 2amp;0amp;2 1amp;0amp;1],Dy=[1amp;2amp;1 0amp;0amp;0 1amp;2amp;1]Dx = \begin{bmatrix} -1 &amp; 0 &amp; 1 \ -2 &amp; 0 &amp; 2 \ -1 &amp; 0 &amp; 1 \end{bmatrix}, Dy = \begin{bmatrix} -1 &amp; -2 &amp; -1 \ 0 &amp; 0 &amp; 0 \ 1 &amp; 2 &amp; 1 \end{bmatrix}

    • These filters are used in edge detection.

Properties of Linear Filters
  1. Linearity: Filtering is a linear operation.

    • if I3=I1+I2I3 = I1 + I2, then I3F=I1F+I2FI3 * F = I1 * F + I2 * F

  2. Shift Invariance: The result of filtering does not depend on the position in the image.

    • if I2(x,y)=I1(xx0,yy0)I2(x, y) = I1(x - x0, y - y0), then I2F=(I1F)(xx0,yy0)I2 * F = (I1 * F)(x - x0, y - y0)

  3. Commutativity: The order of filtering does not matter when multiple filters are applied.

    • I(F1F2)=(IF1)F2=(IF2)F1I * (F1 * F2) = (I * F1) * F2 = (I * F2) * F1

  4. Associativity: Filters can be applied in stages.

    • I(F1F2)=(IF1)F2I * (F1 * F2) = (I * F1) * F2

  5. Separability: Some filters can be separated into two 1D filters, which reduces computational complexity.

    • For example, a Gaussian filter is separable:

    • F(x,y)=ex2+y22σ2=ex22σ2ey22σ2=Fx(x)Fy(y)F(x, y) = e^{-\frac{x^2 + y^2}{2 \sigma^2}} = e^{-\frac{x^2}{2 \sigma^2}} \cdot e^{-\frac{y^2}{2 \sigma^2}} = Fx(x) \cdot Fy(y)

    • This means filtering can be done by first filtering all rows with FxFx and then filtering all columns with FyFy

Applications of Image Filtering
  1. Noise Reduction: Averaging filters (like the box filter) reduce noise but also blur the image.