1. Home
  2. Signal processing

Signal processing · 2-D filtering

conv2: Two-Dimensional Convolution

Eigenfield editorial team ·

The conv2 operation convolves a matrix with a smaller matrix, usually called a kernel or mask. Each output entry is a weighted sum of a neighborhood of the input, and the weights are the kernel entries. Blurring, sharpening, and edge detection on images are all conv2 calls with different kernels, and the same function handles any gridded data such as a temperature map or a surface height field. The topic belongs to the wider area of signal processing.

It extends the one-dimensional rule from convolution of vectors to two indices:

C(i, j) = ∑p ∑q A(p, q) · K(i − p, j − q)

Output size

A has M×N entries and K has P×Q. Three sizes are in common use, and the choice decides what happens at the border.

  • full: every position where the kernel touches the matrix at all, giving (M + P − 1) × (N + Q − 1) entries. Border values use implicit zeros outside the matrix.
  • same: the central part of the full result, with the size of A.
  • valid: only positions where the kernel lies completely inside A, giving (M − P + 1) × (N − Q + 1) entries. No zero padding is involved, so no edge artifacts appear.

A worked example, full output

Let A = [[1, 2], [3, 4]] and K = [[1, 0], [0, 1]], an identity along the diagonal. Each entry A(p, q) is copied to position (p, q) by the 1 in K(0, 0) and to (p + 1, q + 1) by the 1 in K(1, 1). The two copies are added.

The full result is 3×3. Reading across the rows, the first row is 1, 2, 0, the second is 3, 5, 2, and the third is 0, 3, 4. The center value 5 is the only overlap: A(1,1) = 4 from the first copy and A(0,0) = 1 from the shifted one. Entries total 20, and the product of the sums, 10 × 2, is also 20, so the result passes the same checksum used in one dimension.

A worked example, valid output

Take A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] and K = [[1, 1], [1, 1]]. The valid output is (3 − 2 + 1) × (3 − 2 + 1) = 2×2, and each entry is the sum of one 2×2 block of A:

  • top left 1 + 2 + 4 + 5 = 12
  • top right 2 + 3 + 5 + 6 = 16
  • bottom left 4 + 5 + 7 + 8 = 24
  • bottom right 5 + 6 + 8 + 9 = 28

Dividing by 4 turns this into a moving average. The output grows by 4 from left to right and by 12 from top to bottom, matching the slope of the input.

Separable kernels

A kernel is separable when it is the outer product of a column vector and a row vector. The all-ones 2×2 kernel above is [1; 1] times [1, 1]. When that holds, one 2-D convolution can be replaced by a convolution of every column with the first vector and then every row with the second. For a P×Q kernel this reduces the work per output entry from P·Q multiplications to P + Q, a large saving for the wide smoothing kernels used on images. Gaussian blur kernels are the standard case of this property.

Flipping, correlation and edges

Convolution flips the kernel in both directions before sliding it. Correlation does not flip. For kernels that are symmetric the two agree, and for asymmetric ones, such as a gradient mask, the sign of the output changes. When a documented mask is quoted for one convention and applied under the other, the edges simply come out inverted.

Border behavior is the other trap. The full and same modes treat the outside as zero, which darkens edges after a blur. If that matters, pad the matrix by replicating its border rows and columns, then use valid. A discussion of how images are handled as matrices is in image processing with matrices, and circular shift of a matrix describes the wraparound alternative that is natural when the data is periodic.

Back to top