# Equivalent 2D mask of moving-average

I have the moving-average mask as

   mask = [1 1 1; 1 1 1; 1 1 1];


and then I compute the convolution 3 times

   imageF = conv2(conv2(conv2(originalImage, mask), mask), mask);


I want to know how can I get an equivalent mask to compute the filter with just one convolution

   imageF = conv2(originalImage, equivMask);

-

Convolution in linear time-invariant system is asociative. So to get the equivalent mask you just need to convolve the kernel with itself twice. This will then then give you a 7x7 kernel:

octave:1> a = [ 1 1 1 ; 1 1 1 ; 1 1 1 ]
a =

1   1   1
1   1   1
1   1   1

octave:2> conv2(a,conv2(a,a))
ans =

1    3    6    7    6    3    1
3    9   18   21   18    9    3
6   18   36   42   36   18    6
7   21   42   49   42   21    7
6   18   36   42   36   18    6
3    9   18   21   18    9    3
1    3    6    7    6    3    1

octave:3>


Note that the original mask is not normalised - it has a gain of 9 at DC - so with three convolutions you get an overall gain of 9^3.

Depending on what you're trying to achieve though you might just be better off with a 7x7 Gaussian.

-
No, in fact what I meant was conv(conv(conv2(OriginalImage, mask), mask), mask). –  BRabbit27 Nov 11 '12 at 15:57
Yes, that is what I thought you meant, and this is then equivalent to using the 7x7 kernel above. –  Paul R Nov 11 '12 at 16:41
Good answer. I think you should add a sentence about the fact that it is possible due to the fact that convolution is associative. –  Andrey Nov 11 '12 at 17:02
Should this resulting kernel be normalized? –  heltonbiker Nov 11 '12 at 17:24
You can just repeatedly convolve with the original mask. Obviously the kernel will grow by 2 in each dimension for each iteration so after 20 convolutions the kernel will be 41x41. –  Paul R Nov 11 '12 at 19:50