How to Combine / Cascade two `3 x 3` Filters into One `5 x 5` Filter
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