Fundamentals of Computer Vision Lecture 3: Image Filtering and Edge Detection

Fundamentals of Computer Vision: Image Filtering

  • Overview of Lecture Topics

    • Image noise.

    • Linear shift-invariant image filtering.

    • Convolution and Correlation.

    • Edge detection.

Mathematical Foundations of Convolution

  • Convolution for 1D Continuous Signals

    • Filtering is defined as the convolution of an input signal gg and a filter ff.

    • Definition: Output (fg)(x)(f * g)(x).

    • Mechanism: The filter is flipped before being applied.

    • Example: A 1D continuous box filter applied to a signal results in a blurred version of the input signal gg.

  • Convolution for 2D Discrete Signals

    • Definition for filtered image hh: h[m,n]=k,lf[k,l]g[mk,nl]h[m, n] = \sum_{k, l} f[k, l] g[m - k, n - l].

    • The operation involves a horizontal and vertical flip of the filter kernel.

    • If the filter f[k,l]f[k, l] is non-zero only within the range Nk,lN-N \leq k, l \leq N, then:         h[m,n]=k=NNl=NNf[k,l]g[mk,nl]h[m, n] = \sum_{k=-N}^{N} \sum_{l=-N}^{N} f[k, l] g[m-k, n-l].

    • The kernel is typically represented as a 3×33 \times 3 matrix (or other N×NN \times N odd-dimension matrices).

Properties of Convolution

  • Linear Shift-Invariant (LSI) Properties

    • Shift Invariant: The operator behaves the same everywhere in the image. The output value depends entirely on the pattern in the image neighborhood, not the spatial position of that neighborhood.

    • Superposition: Convolution is a linear operator. h(f1+f2)=(hf1)+(hf2)h * (f_1 + f_2) = (h * f_1) + (h * f_2).

  • Algebraic Properties

    • Commutative: fg=gff * g = g * f.

    • Associative: (fg)h=f(gh)(f * g) * h = f * (g * h).

    • Distributive over Addition: f(g+h)=(fg)+(fh)f * (g + h) = (f * g) + (f * h).

    • Scalars Factor Out: kfg=fkg=k(fg)k f * g = f * kg = k(f * g).

    • Differentiation Rule: Differentiation can be applied to either the filter or the signal.

    • Identity: Convolution with a unit impulse e=[,0,0,1,0,0,]e = [\dots, 0, 0, 1, 0, 0, \dots] results in the original signal: fe=ff * e = f.

Convolution vs. Correlation

  • 2D Discrete Convolution: Requires flipping the filter in both dimensions (bottom to top, right to left) before applying the cross-correlation logic.

    • Notation: FHF * H.

  • 2D Discrete Correlation: Does not involve a flip.

    • Definition: k,lf[k,l]g[m+k,n+l]\sum_{k, l} f[k, l] g[m + k, n + l].

  • Practical Significance: For most computer vision applications, the distinction is negligible because many kernels (like Gaussian or Box filters) are symmetric. However, the flip is mathematically significant when discussing frequency-domain filtering.

Filtering an Impulse Signal

  • Conceptual Exercise: Filtering an image FF containing a single centered pixel with value 11 (surrounded by 00s) with an arbitrary kernel H=(aamp;bamp;cdamp;eamp;fgamp;hamp;i)H = \begin{pmatrix} a & b & c \\ d & e & f \\ g & h & i \end{pmatrix}.

  • Result: The output is the filter kernel itself, but flipped horizontally and vertically.

    • Output pattern trace:

      • Top row: i,h,gi, h, g

      • Middle row: f,e,df, e, d

      • Bottom row: c,b,ac, b, a

Separable Filters

  • Definition: A 2D filter is separable if its matrix representation can be decomposed into the product of a column vector and a row vector: Kernel=Column×RowKernel = Column \times Row.

  • Example: Box Filter:

    • A 3×33 \times 3 matrix of all 11s is the product of (111)\begin{pmatrix} 1 \\ 1 \\ 1 \end{pmatrix} and (1amp;1amp;1)\begin{pmatrix} 1 & 1 & 1 \end{pmatrix}.

    • The rank of a separable filter matrix is 11.

  • Computational Efficiency:

    • Standard 2D convolution cost for an M×MM \times M image and an N×NN \times N kernel is M2×N2M^2 \times N^2.

    • Separable 2D convolution is equivalent to two 1D convolutions (first across all rows, then across all columns).

    • Cost of separable convolution: 2×N×M22 \times N \times M^2.

Gaussian Filtering

  • Concept: Used when neighboring pixels should have more influence on the output than distant pixels. The weights fall off according to a Gaussian distribution.

  • Mathematical Characteristics:

    • In theory, Gaussian support is infinite.

    • In practice, it is truncated at a finite distance, usually at 2σ2\sigma to 3σ3\sigma.

  • Key Properties:

    • Convolution of a Gaussian with another Gaussian results in a third Gaussian.

    • Scale (σ\sigma): The variance σ\sigma determines the extent of smoothing (the width/spread of the kernel).

      • Example: σ=2\sigma = 2 with a 30×3030 \times 30 kernel vs. σ=5\sigma = 5 with a 30×3030 \times 30 kernel.

    • Kernel Size: While the Gaussian function is infinite, the discrete filter kernel size must be chosen appropriately. Truncating a σ=5\sigma = 5 Gaussian to a 10×1010 \times 10 kernel loses significant data compared to a 30×3030 \times 30 kernel.

  • MATLAB Implementation:

    • kernelsize = 10;

    • sigma = 5;

    • h = fspecial('gaussian', kernelsize, sigma);

    • outim = imfilter(im, h); (Correlation by default).

Case Studies in Linear Filters

  • Identity Filter: Kernel [0,0,0;0,1,0;0,0,0][0, 0, 0; 0, 1, 0; 0, 0, 0] leaves the image unchanged.

  • Shift Filter: Kernel [0,0,0;0,0,1;0,0,0][0, 0, 0; 0, 0, 1; 0, 0, 0] shifts the image left by 1 pixel (using correlation).

  • Box Filter: Average of pixels in the neighborhood; results in blurring.

  • Sharpening Filter: Accentuates differences with the local average.

    • Strategy: Subtract a smoothed (blurred) version of the image from the original to find the "detail," then add that detail back to the original.

    • Formula: Sharpened=Original+α(OriginalSmoothed)Sharpened = Original + \alpha (Original - Smoothed).

    • It stresses intensity peaks and does nothing in flat areas.

Non-Linear Filtering: The Median Filter

  • Mechanism: Replaces the center pixel with the median value of the pixels in the local window.

  • Advantages:

    • No New Values: Does not introduce new pixel values (unlike blurring which averages).

    • Impulse Noise Removal: Extremely effective at removing "Salt and Pepper" noise (spikes).

    • Edge Preserving: Unlike Gaussian or Box blurs, the median filter maintains sharp edges.

  • MATLAB: output_im = medfilt2(im, [h, w]);.

Edge Detection Theory

  • Goal: Map 2D pixel arrays to a set of curves, line segments, or contours.

  • Causes of Edges:

    • Depth Discontinuity: Object boundaries.

    • Surface Orientation: Changes in shape.

    • Cast Shadows.

    • Reflectance Change: Appearance and texture information.

  • Mathematical Perspective: Edges correspond to "steep cliffs" or discontinuities in the image intensity function.

  • Gradient Principles:

    • The gradient vector f=(fxfy)\nabla f = \begin{pmatrix} \frac{\partial f}{\partial x} \\ \frac{\partial f}{\partial y} \end{pmatrix} points in the direction of the most rapid increase in intensity.

    • Edge Strength: Given by the gradient magnitude (fx)2+(fy)2\sqrt{(\frac{\partial f}{\partial x})^2 + (\frac{\partial f}{\partial y})^2}.

    • Gradient Direction: θ=tan1(f/yf/x)\theta = \tan^{-1}(\frac{\partial f / \partial y}{\partial f / \partial x}).

    • Edges correspond to the extrema of the first derivative.

Finite Differences and Derivative Filters

  • Discrete Derivatives:

    • Forward Difference: f(x)f(x+h)f(x)hf'(x) \approx \frac{f(x+h) - f(x)}{h}.

    • Central Difference: For discrete signals, using h=2h=2 results in the filter [1,0,1][1, 0, -1].

  • Derivative of Gaussian (DoG):

    • Differentiation and convolution are associative.

    • Instead of differentiating a noisy image directly, convolve the image with the derivative of a Gaussian filter.

    • This smooths the image and takes the derivative simultaneously.

Edge Detection Operators

  • The Sobel Filter:

    • Approximates the derivative of a Gaussian.

    • Can be decomposed into a blurring component and a derivative component.

    • Horizontal Sobel (SxS_x):         Sx=(1amp;0amp;12amp;0amp;21amp;0amp;1)=(121)(1amp;0amp;1)S_x = \begin{pmatrix} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \end{pmatrix} = \begin{pmatrix} 1 \\ 2 \\ 1 \end{pmatrix} * \begin{pmatrix} -1 & 0 & 1 \end{pmatrix}

      • This detects vertical edges.

    • Vertical Sobel (SyS_y):         Sy=(1amp;2amp;10amp;0amp;01amp;2amp;1)=(101)(1amp;2amp;1)S_y = \begin{pmatrix} 1 & 2 & 1 \\ 0 & 0 & 0 \\ -1 & -2 & -1 \end{pmatrix} = \begin{pmatrix} 1 \\ 0 \\ -1 \end{pmatrix} * \begin{pmatrix} 1 & 2 & 1 \end{pmatrix}

      • This detects horizontal edges.

    • The 1/81/8 normalization term is technically needed for correct gradient magnitude but often omitted for simple edge detection.

  • Comparison of Derivative Filters:

    • Sobel: Weights the center row/column by 22.

    • Scharr: Uses weights like 33 and 1010 (e.g., [3,0,3;10,0,10;3,0,3][3, 0, -3; 10, 0, -10; 3, 0, -3]) for better rotational invariance.

    • Prewitt: Uses uniform weights of 11 (e.g., [1,0,1;1,0,1;1,0,1][1, 0, -1; 1, 0, -1; 1, 0, -1]).

    • Roberts: A 2×22 \times 2 operator for diagonal differences (e.g., [0,1;1,0][0, 1; -1, 0] and [1,0;0,1][1, 0; 0, -1]).