3
$\begingroup$

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);
$\endgroup$
7
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ No, in fact what I meant was conv(conv(conv2(OriginalImage, mask), mask), mask). $\endgroup$ – BRabbit27 Nov 11 '12 at 15:57
  • 1
    $\begingroup$ Yes, that is what I thought you meant, and this is then equivalent to using the 7x7 kernel above. $\endgroup$ – Paul R Nov 11 '12 at 16:41
  • 2
    $\begingroup$ Good answer. I think you should add a sentence about the fact that it is possible due to the fact that convolution is associative. $\endgroup$ – Andrey Rubshtein Nov 11 '12 at 17:02
  • 1
    $\begingroup$ Should this resulting kernel be normalized? $\endgroup$ – heltonbiker Nov 11 '12 at 17:24
  • 2
    $\begingroup$ 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. $\endgroup$ – Paul R Nov 11 '12 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.