# MATLAB: How to apply a sequence of filters on a single image with a single “imfilter” call

MATLAB

Is there a way to convolve sequencially one image with multiple filters using "imfilter" or another function?

• An alternative to calling "imfilter" multiple times is to first convolve all of the filters with eachother using the "conv2" function, and then using the resulting filter with "imfilter".
Here is a small example of what I explained:
A = magic(5); % matrix/image
h1 = [-1 0 1]; % first filter
h2 = [ 4 5 7]; % second filter
t = imfilter(A,h1,'full'); % result after first filter
B = imfilter(t,h2,'full'); % result after secind filter
H = conv2(h1,h2,'full'); % convolve the two filters
B2 = imfilter(A,H,'full'); % result after convolved filter
isequal(B,B2) % check that B is equal to B2
Please note that the convolution and filtering operations must be executed with the option 'full' in order to yield the same result. Without this option, the boundaries of the image will not be treated identically with the two approaches.
For more details about the mathematical operations you can refer to :
"Conv2"  <https://www.mathworks.com/help/matlab/ref/conv2.html>
In the end, this is mostly trading iterations on the "imfilter" function for iterations on the "conv2" functions when creating the convolved filter. However you may find that this approach allows you to factorize your code and to reuse the same sequence of filters on a different image.