# How to Do Convolution a 2D Signal (Image) and Result of Convolution of Two 1D Filters?

Assume a 2-D signal (i.e., some image). Load image and assume it to be signal x. Next assume that instead of having a 2-D filter you have two one D filters

$$f_1[n] =\begin{bmatrix}0.25 && 0.5 && 0.25\end{bmatrix}$$ and $$f_2[n]=\begin{bmatrix}0.25 \\ 0.5 \\ 0.25\end{bmatrix}$$ Assume that the convolution

$$f_1[n]* f_2[n] = f_3[n]= \begin{bmatrix}0.0625 && 0.125 && 0.0625\\ 0.125 && 0.25 && 0.125\\0.0625 && 0.125 && 0.0625\end{bmatrix}$$

Using this information and output at each stage verify that Associative property holds.

My code:

f3 = [0.0625 0.125 0.0625; 0.125 0.25 0.125; 0.0625 0.125 0.0625]
conv2('x, f3)


But this gives error saying x is not a vector. How do I fix this?

• It would be nice if you could edit your post with a more readable typesetting of the matrices, etc. Apr 1 '17 at 18:45
• Did what Laurent asked for, Maya, because I agree with him! You just have to use single or double \$ around your TeX formulas instead of **! Apr 2 '17 at 8:06
• @MarcusMüller I didn't know that!
– Maya
Apr 2 '17 at 10:34
• @Maya that's why I'm telling you ;) Have a great day! Apr 2 '17 at 10:41

You should show that conv2(f3,x) can be implemented in a separable way. In other words, computing x1 = conv2(f1,x), then applying f2 on the results, x12 = conv2(f2,x1) gives the same result. Or f12 = conv2(f2,f1) then x12 = conv2(f12,x).

You should be aware of the correct "dimensions" for f1 and f2: transpose of each other, so that one works on "rows" and the other on columns.

This is shown in the following Matlab code:

dataImage = zeros(25,25);
% Create a delta-like image
dataImage(13,13)=1;
% Create a random-kernel image
dataImage(11:13,11:13)=rand(3,3);
imagesc(dataImage);colormap gray

f1 = [0.25 0.5 0.25]'; f2 = [0.25 0.5 0.25];
disp(f1*f2);

f3 = [0.0625 0.125 0.0625; 0.125 0.25 0.125; 0.0625 0.125 0.0625];
disp(f3);

dataImagef1f2 = conv2(f2,conv2(f1,dataImage));

dataImagef1f2 = conv2(conv2(f2,f1),dataImage);

dataImagef3 = conv2(f3,dataImage);

subplot(2,2,1)
imagesc(dataImage);
xlabel('data')

subplot(2,2,2)
imagesc(dataImagef1f2);
xlabel('(data*f1)*f2')

subplot(2,2,3)
imagesc(dataImagef1f2);
xlabel('(f1*f2)*data')

subplot(2,2,4)
imagesc(dataImagef3);
xlabel('f3')


Suggestions for tutorials:

• Can you tell me whay did you create kernel and delta like images? And why does it have to be grayscale image? I am new to image processing/
– Maya
Apr 1 '17 at 18:35
• For the demo mostly, and to avoid side effects on the borders of the images. Having enough zeros around avoid some practical issues. Plus, the linearity of convolution entails that if you prove the associativity for the dirac image, then the result extends to other images. And it would work for color images, for instance by working on different color places. This is more a Matlab-type question Apr 1 '17 at 18:42
• Can you suggest me a good resource to study image processing in matlab?
– Maya
Apr 1 '17 at 22:07
• I am studying this course 'signals and systems" in university and have just begun using matlab but I cannot find detailed tutorial about image processing in thus course book : Attaway's Introduction to Matlab
– Maya
Apr 5 '17 at 18:40
• I don't have enough rep to upvote, sorry :/
– Maya
Apr 5 '17 at 19:08