(CV) – Convolution and Cross Correlation

Convolution & Cross-correlation

The primary implement of convolution and cross-correlation.


You can refer to the complete notes from 04 Notes

Linear Systems(Filter)

Denotes the input function:


Filter is used to convert input function to an output(or response) function:


$\mathcal{S}$ is refered to as system operator which maps a member of the set of possible

outputs $g[m,n]$ to a member of the set of possible inputs $f[m,n]$. When using notation involving $\mathcal{S}$, we can write that

\mathcal{S}[g]&=f \\
\mathcal{S}\left\{f[m,n]\right\}&=g[m,n] \\

Examples of Filters

Moving Average

If we want to smooth or blur the image, we can set the value of a pixel to be the average of its neighboring pixels:


Image Segmentation

The value of a pixel can be set:

255 & f[m,n]\geq{t}\\

Properties of Systems

Not all systems will have the following(or any) of these properties in general. You can refer to the notes or slide about other properties.

Amplitude Properties

  1. Superposition(叠加原理)


Properties of Systems

  1. Shift Invariance(平移不变性)


Linear Systems

A linear system is a system that satisfies the property of superposition.

Linear systems are also known as impulse response(脉冲响应) of a system $\mathcal{S}$. Consider a function $\delta_2[m,n]$:

1&m=0\text{ and }n=0\\

The response $r$:


A simple linear shift-invariant system is a system that shifts the pixels of an image, based on the shifting property of the delta function.


Then we can use the superposition property to write any linear shift-invariant system as a weighted sum of such shifting system:


We can define the filter $h$ of a linear shift-invariant system as


Linear shift invariant systems(LSI):

  • Systems that satisfy the superposition property
  • Have an impulse response: $\mathcal{S}[\delta_2[n,m]]=\delta_2[n,m]$
  • Discrete convolution: $f[n,m]*h[n,m]$(multiplication of shifted-version of impulse response by original function)

Convolution and Correlation


The impulse function $\delta[n]$ is defined to be 1 at $n=1$ and 0 elsewhere. For an arbitrary input signal $x$ can be written as $x[n]=\sum_{k=-\infty}^\infty{x[k]\delta[n-k]}$. If we pass the equation into a linear, shift-invariant system, the output is $y[n]=\sum_{k=-\infty}^\infty{x[k]h[n-k]}$, i.e the convolution of the signal $x$ with the impulse response $h$.

However, an image is written in form of matrix. For a linear, shift-invariant system, the output is $y[n,m]=\sum_{i=-\infty}^\infty\sum_{j=-\infty}^\infty{x[i,j]h[n-i,m-j]}$

,i.e the convolution of the signal $x$ with the impulse response $h$ in 2 dimensions.


Cross Correlation is often used to measure the similarity of two images. cross correlation is the same as convolution, except that the filter kernel is not flipped. Two-dimensional cross correlation is:



The steps for discrete convolution are:

  • Fold $h[k,l]$(折叠) about origin to form $h[-k]$
  • Shift th folded results by $n$ to form $h[n-k]$
  • Multiply $h[n-k]$ by $f[k]$
  • Sum over all $k$
  • Repeat for every n

However, cross correlation doesn't have to fold the filter. The convolution amis to calculate the value of weighted $f[m,n]$ by folded and shifted filter $f[k,l]$

In this picture, we assume function $f$ and its height is 1.0. The value of the result at 5 different points is indicated by the shaded area below each point. Also, the symmetry of f is the reason f*g and $f\star{g}$ are identical in this example.

Cross correlation doesn't satisfy commutative property(交换律):


However, convolution satisfy commutative property:


How to run

  1. Download files from my Github repo
  2. hw1.ipynb is interface for user to debug/visualize the code in filter.py


  1. Stanford CS131 lecture #4 note & slide
  2. Convolution(Wikipedia)
  3. Cross Correlation
  4. Answers of Homework of CS 131