0
$\begingroup$

I compared the difference between seperable and direct filtering for MATLAB's cameraman image. The difference is huge. I would like to understand the reason for this. Code below.

I = imread('cameraman.tif');

% Do seperable 1-D filtering
H = 11;
sigma = 2;
g1 = fspecial('gaussian',[1 H],sigma);

I2 = double(I);
I_seperable = double(I);
for i = 1:size(I,1)
   I2(i,:) = imfilter(double(I(i,:)),g1,'circular','same','conv');
end
for j = 1:size(I,2)
  I_seperable(:,j) = imfilter(double(I2(:,j)),g1,'circular','same','conv');
end

% Do direct 2-D filtering 
g2 = fspecial('gaussian',[H H],sigma);

% How much do the results differ ?
I_direct = imfilter(double(I),g2,'circular','same','conv');
delta_I = double(I_direct) - double(I_seperable);
norm(delta_I,'fro')

The result of last computation

ans =

    2.1879e+03
$\endgroup$
1
$\begingroup$

Turns out that when doing filtering by columns, i.e. for the following section

for j = 1:size(I,2)
   I_seperable(:,j) = imfilter(double(I2(:,j)),g1,'circular','same','conv');
end

we need to make sure the filter is aligned along columns. Changing g1 to g1' makes the difference almost zero as seen below.

ans =

   1.9782e-11
| improve this answer | |
$\endgroup$
  • $\begingroup$ Faster way to do so would be: mOutputImage = conv2(vFilterCoef, vFilterCoef', mInputImage). Anyhow, if the filter is separable, imfilter decompose it automatically. $\endgroup$ – Royi Oct 2 '14 at 5:44

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.