You are on page 1of 48

Spatial convolution and

correlation
Convolution
Convolution Formula for 2D
Digital Signal
• Convolution is applied similarly to correlation.
• Note how similar the formulas for correlation and convolution.
• The only difference is that in the signs of s and t variables.
• Correlation formula has positive signs for s and t while
convolution has negative signs for them.
• Correlation :

• Convolution
Convolution
Convolution kernel, ω Input Image, f

1 -1 -1 2 2 2 3
1 2 -1 2 1 3 3
1 1 1 2 2 1 2
Rotate 180o 1 3 2 2
1 1 1
-1 2 1
-1 -1 1
1 1 1 Convolution 2 2 2 3
-1 2 1 2 1 3 3
-1 -1 1 2 2 1 2
1 3 2 2
1 1 1
2
-1 4 2 2 3 5
Output
2 1
-1 -2 3 3 Image, g
2 2 1 2
1 3 2 2
Input Image, f
1 1 1 Convolution 2 2 2 3
-1 2 1 2 1 3 3
-1 -1 1 2 2 1 2
1 3 2 2
1 1 1
2 4
-2 2 2 3 5 4
Output
2 -1
-2 1 3 3 Image, g
2 2 1 2
1 3 2 2
Input Image, f
1 1 1 Convolution 2 2 2 3
-1 2 1 2 1 3 3
-1 -1 1 2 2 1 2
1 3 2 2
1 1 1
2 -2
2 4
2 3 5 4 4
Output
2 -1
1 -3
3 3 Image, g
2 2 1 2
1 3 2 2
Input Image, f
1 1 1 Convolution 2 2 2 3
-1 2 1 2 1 3 3
-1 -1 1 2 2 1 2
1 3 2 2
1 1 1
2 2 2 3
-2 6 1 5 4 4 -2
Output
2 1 3 -3
-3 3 1 Image, g
2 2 1 2
1 3 2 2
Input Image, f
1 1 1 Convolution 2 2 2 3
-1 2 1 2 1 3 3
-1 -1 1 2 2 1 2
1 3 2 2

1 2 2 2 3 5 4 4 -2
Output
-1 4
2 1 3 3 9 Image, g
-1 -2
2 2 1 2
1 3 2 2
Input Image, f
1 1 1 Convolution 2 2 2 3
-1 2 1 2 1 3 3
-1 -1 1 2 2 1 2
1 3 2 2

2 2 2 3 5 4 4 -2
Output
-2
2 2
1 3 3 9 6 Image, g

2 -2
-2 2 1 2
1 3 2 2
Input Image, f
Convolution
5 4 4 -2
9 6 14 5
11 7 6 5
9 12 8 5
Final output Image, g
Correlation

 Correlation of a filter w(x, y) of size m x n with an


image f(x, y), denoted as

 This equation is evaluated for all values of the displacement


variables x and y so that all elements of w visit every pixel in f,
where we assume that f has been padded appropriately.
 a = (m-1)/2, b = (n-1)/2,
 and we assume for notational convenience that m and n are odd
integers.
• The size of mask(template) must be odd (i.e. 3x3, 5x5, etc.)
to ensure it has a center. The smallest meaningful size is
3x3.
How much Padding Required?

• It depends on both the template size.


• For 3x3 template, pad two columns and two rows. One
column to the left and another to the right. One row at the
top and one row at the bottom.
• Generally, for N*N template pad (N-1)/2 columns and
rows at each side.
Matching using Correlation
• Matching depends on just multiplication.
• The highest results of multiplication is the one
selected as best match.
• But the highest results of multiplication not always
refers to best match.
Correlation
correlation kernel, ω

1 -1 -1
Input Image f
1 2 -1
2 2 2 3
1 1 1
2 1 3 3
Don’t rotate use it directly
2 2 1 2
1 3 2 2
1 -1 -1 Correlation 2 2 2 3
1 2 -1 2 1 3 3
1 1 1 2 2 1 2

1 -1 -1 1 3 2 2

1 2 -2
4 2 2 3 5
1 2 1 3 3 output
Image, g
2 2 1 2
1 3 2 2
Input Image, f
1 -1 -1 Correlation 2 2 2 3
1 2 -1 2 1 3 3
1 1 1 2 2 1 2
1 3 2 2
1 -1 -1
2 2 -2
4 2 3 5 10
2 1 3 3 output
Image, g
2 2 1 2
1 3 2 2
Input Image, f
1 -1 -1 Correlation 2 2 2 3
1 2 -1 2 1 3 3
1 1 1 2 2 1 2
1 3 2 2
1 -1 -1
2 2 2 -3
4 3 5 10 10
2 1 3 3 output
Image, g
2 2 1 2
1 3 2 2
Input Image, f
1 -1 -1 Correlation 2 2 2 3
1 2 -1 2 1 3 3
1 1 1 2 2 1 2
1 3 2 2
1 -1 -1
2 2 2 3
6 -1 5 10 10 15
2 1 3 3 1 output
Image, g
2 2 1 2
1 3 2 2
Input Image, f
1 -1 -1 Correlation 2 2 2 3
1 2 -1 2 1 3 3
1 1 1 2 2 1 2
1 3 2 2

1 -2
2 -2
2 2 3 5 10 10 15
1 4
2 -1
1 3 3 3 output
Image, g
1 2 2 1 2
1 3 2 2
Input Image, f
1 -1 -1 Correlation 2 2 2 3
1 2 -1 2 1 3 3
1 1 1 2 2 1 2
1 3 2 2

2 -2
2 -2
2 3 5 10 10 15
2 1 -3
2 3 3 3 4 output
Image, g
2 2 1 2
1 3 2 2

Input Image, f
Correlation

5 10 10 15
3 4 6 11
7 11 4 9
-5 4 4 5
Final output Image, g

You might also like