How to Combine / Cascade two 3 x 3 Filters into One 5 x 5 Filter

If you have a uniform 3x3 box filter T which is:

1 1 1

1 1 1

1 1 1

And an 3x3 laplacian filter W which is:

1 -2 1

-2 4 -2

1 -2 1

Can these 2 filters be combined into one 5x5 filter, the order being first T and then W ? (The combination should be possible) And what is the math behind this combination. I can make a 5x5 filter of all one's and after that stride the W filter over that 5x5 and add everything up, is it that simple?

• To cascade two filters, you convolve their coefficients. – Dan Boschen May 12 at 13:52

Let's say you have a convolution kernel $$f$$ and another convolution kernel $$g$$.
We also have an input signal (1D or 2D) $$x$$ and we are after the result of the cascaded convolution:

$$y = g \ast \left( f \ast x \right)$$

The nice thing about convolution is its associativity property.
Which means:

$$y = g \ast \left( f \ast x \right) = \left( g \ast f \right) \ast x = h \ast x$$

So the equivalent is $$h = g \ast f$$ which with the commutative property of convolution means:

$$h = g \ast f = f \ast g$$

Pay attention that for the discrete case only the Full Convolution (In MATLAB formulation) is commutative.

So in the above:

mF = [1, 1, 1; 1, 1, 1; 1, 1, 1];
mG = [1, -2, 1; -2, 4, -2; 1, -2, 1];

mH = conv2(mF, mG, 'full')
mH = conv2(mG, mF, 'full')

mH =

1    -1     0    -1     1
-1     1     0     1    -1
0     0     0     0     0
-1     1     0     1    -1
1    -1     0    -1     1

mH =

1    -1     0    -1     1
-1     1     0     1    -1
0     0     0     0     0
-1     1     0     1    -1
1    -1     0    -1     1