Why are imfilter & conv2 giving me different results?
7 views (last 30 days)
Show older comments
I'm writing a code that convolves a filter with a random matrix, convolves that with an image, then deconvolves the random matrix out of the first result to give the wanted image+filter
%clear previous run
clc
clear
%import image and turn gray
inputIm = imread('zebra.jpg');
grayIm = rgb2gray(inputIm);
%prompt user to enter key size and number of loops
prompt = 'Key size? ';
randSize = input(prompt);
prompt = 'Loops? ';
loops = input(prompt);
%loop to produce multiple results
for i = 1:loops
%generate randomized key and edge filter, then conv together
edgeFil = [-1 0 1; 0 0 0; 1 0 -1];
key = randn(randSize);
obFil = conv2(edgeFil, key);
%%%%%%%%%%Line 25 %%%%%%%%%%
%conv image with filter
filteredIm = imfilter(grayIm, obFil, 'replicate', 'same', 'conv');
%use Fourier to deconv the key from the image
[n, m] = size(filteredIm);
fIm = fft2(filteredIm);
fKey = fft2(key, n, m);
fRes = fIm ./ fKey;
result = ifft2(fRes);
%generate file names each loop
filImName = strcat('fil', num2str(i), '.jpg');
resultName = strcat('result', num2str(i), '.jpg');
%write images
imwrite(filteredIm, filImName);
imwrite(result, resultName);
%show and compare images
figure(i)
[X1,map1]=imread(filImName);
[X2,map2]=imread(resultName);
subplot(1,2,1), imshow(X1,map1)
subplot(1,2,2), imshow(X2,map2)
end
The final result here should be equal to
imfilter(grayIm, edgeFil, 'replicate', 'same', 'conv');
Instead,I'm getting some odd results that are different with each loop, which means it's deconvolving incorrectly.
However, using conv2 in line 25 gives me consistent results that are equal to
conv2(grayIm, edgeFil);
Which means it's deconvolving correctly. Unfortunately the final result is incorrect.
So my question is: why are these 2 functions behaving differently?
As far as I know they should be the same. I know the math is correct and I tested it using conv2 w/ non-image matrices. I'd like to note that I tried all the boundary options in imfilter and I'm getting the same odd results. I'd greatly appreciate your help. I can share the result images if that would help.
2 Comments
Walter Roberson
on 8 Oct 2018
I seem to recall that the convention for filters is that the mask is flipped left to right compared to the standard for convolution. This only matters for asymmetrical masks.
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!