72 min total

Computer Vision & OpenCV

Convolution and filters

Convolution and filters

Example 3×3 filters

Moving the single nonzero identity coefficient to a neighboring position shifts the sampled image by one pixel; the direction depends on the convolution/correlation convention.

One-pixel translation filters

Under cross-correlation, sampling the pixel to the right shifts the visible image content left; sampling to the left shifts it right. Convolution flips these kernels first, reversing that convention.

Derivative of images

Derivative: represents rate of change of function

Finite differences and gradients

ApproximationUnit-step expressionMask
Forward differencef(x+1) − f(x)[−1, 1]
Backward differencef(x) − f(x−1)[−1, 1], shifted
Central difference(f(x+1) − f(x−1))/2[−1, 0, 1]/2

Two-dimensional derivative masks

These masks combine a central difference in one direction with accumulation in the perpendicular direction. Dividing the 1D central difference by two estimates the derivative per unit spacing. Unnormalized masks instead retain a scale factor. Reversing a mask changes the derivative sign.

Linear filter properties

Linearity

Filter of a sum of images = sum of filter on individual image

Shift invariance

If shifting the image does not change filtering operation

If any filter is linear and shift-invariant, it can be used in convolutions

  • commutative: a∗b=b∗a
  • associative: a∗(b∗c)=(a∗b)∗c
  • Distributive Over Addition: a∗(b+c)=(a∗b)+(a∗c)
  • Scalars Factor out: ka∗b=a∗kb=k(a∗b)
  • Identity: a∗e=a

*: convolution

a, b, c: signals

k: scalar

Median Filter

Is a non-linear filter, for each pixel, filter examines a neighborhood around it and replaces the pixel with the median value of intensities in that window. Convolution is a linear operation and median filter is a non-linear operation as it replaces pixel values with median of neighborhood

Considerations

  • Larger filters can smooth more noise but may blur edges
  • Smaller filters can preserve edges but may not remove enough noise
  • Rule of thumb for gaussian filter is to set filter half-width to about 3σ

Border Pixel practices

When applying filter to an image, for pixels near edges filter window might extend beyond image boundaries but there are no pixel values to consider those regions, we can:

  • Zero padding: treat pixels outside the image as zero.
  • Wrap around: if filter extends, it wraps to other side of image
  • Copy edge: edge pixels are duplicated to fill missing areas
Mean and median filtering at image boundaries
  • Reflect across edge: pixels near edge are mirrored into missing region

First- and second-difference example

Using backward differences, set the first difference at the boundary to zero.

Every finite-difference step

IndexInput fFirst differenceSecond difference
0100 (boundary)0 (boundary)
11515 − 10 = 55 − (0) = 5
21010 − 15 = -5-5 − (5) = -10
31010 − 10 = 00 − (-5) = 5
42525 − 10 = 1515 − (0) = 15
52020 − 25 = -5-5 − (15) = -20
62020 − 20 = 00 − (-5) = 5
72020 − 20 = 00 − (0) = 0

Reversing the finite-difference mask reverses the sign of the response.

For an image whose rows are [10, 10, 20, 20, 20], the horizontal derivative is nonzero around the transition from 10 to 20. The vertical derivative vanishes because all rows are identical.

Image grid and horizontal/vertical finite-difference responses, redrawn without highlights

The diagram uses zero values on the output border and evaluates the interior 3×3 neighborhoods. A horizontal change produces a horizontal derivative; identical rows produce no vertical derivative.

Laplacian and Sobel operators

The Laplacian is a second-order operator. Sobel approximates first derivatives while smoothing in the perpendicular direction.

Sign convention: the notebook uses the opposite signs for some Laplacian and Sobel masks. Multiplying a derivative mask by −1 reverses response signs. Gradient magnitude is unchanged if both derivative components are reversed, but the directed angle changes by π.