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 and a filter .
Definition: Output .
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 .
Convolution for 2D Discrete Signals
Definition for filtered image : .
The operation involves a horizontal and vertical flip of the filter kernel.
If the filter is non-zero only within the range , then: .
The kernel is typically represented as a matrix (or other 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. .
Algebraic Properties
Commutative: .
Associative: .
Distributive over Addition: .
Scalars Factor Out: .
Differentiation Rule: Differentiation can be applied to either the filter or the signal.
Identity: Convolution with a unit impulse results in the original signal: .
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: .
2D Discrete Correlation: Does not involve a flip.
Definition: .
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 containing a single centered pixel with value (surrounded by s) with an arbitrary kernel .
Result: The output is the filter kernel itself, but flipped horizontally and vertically.
Output pattern trace:
Top row:
Middle row:
Bottom row:
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: .
Example: Box Filter:
A matrix of all s is the product of and .
The rank of a separable filter matrix is .
Computational Efficiency:
Standard 2D convolution cost for an image and an kernel is .
Separable 2D convolution is equivalent to two 1D convolutions (first across all rows, then across all columns).
Cost of separable convolution: .
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 to .
Key Properties:
Convolution of a Gaussian with another Gaussian results in a third Gaussian.
Scale (): The variance determines the extent of smoothing (the width/spread of the kernel).
Example: with a kernel vs. with a kernel.
Kernel Size: While the Gaussian function is infinite, the discrete filter kernel size must be chosen appropriately. Truncating a Gaussian to a kernel loses significant data compared to a 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 leaves the image unchanged.
Shift Filter: Kernel 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: .
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 points in the direction of the most rapid increase in intensity.
Edge Strength: Given by the gradient magnitude .
Gradient Direction: .
Edges correspond to the extrema of the first derivative.
Finite Differences and Derivative Filters
Discrete Derivatives:
Forward Difference: .
Central Difference: For discrete signals, using results in the filter .
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 ():
This detects vertical edges.
Vertical Sobel ():
This detects horizontal edges.
The 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 .
Scharr: Uses weights like and (e.g., ) for better rotational invariance.
Prewitt: Uses uniform weights of (e.g., ).
Roberts: A operator for diagonal differences (e.g., and ).