Clarification on linear vs circular convolution
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a simple program to satisfy myself that I understand convolution. This program takes an input image, calculates the 2D convolution using fft2, calculates the 2D convolution using conv2, then compares the results.
The code is below:
%%Image input
source_image_path = 'C:\...\cameraman.tif';
myFilter = fspecial('gaussian', [256 256], 4);
% load source image
myImage = im2double(imread(source_image_path));
if ( size(size(myImage)) == [1 3])
myImage = rgb2gray(myImage);
end
subplot(2,2,1); % 2 rows, 3 columns, grid position 1
imshow(myImage, []);
axis on;
title('Source', 'FontSize', 15);
conv = fftshift(ifft2((fft2(myImage) .* fft2(myFilter)))) ;
subplot(2,2,2);
imshow(conv, []);
title('fft conv result', 'FontSize', 15);
axis on;
second_conv = conv2(myImage, myFilter, 'same') ;
subplot(2,2,3);
imshow(second_conv, []);
title('conv2 result', 'FontSize', 15);
axis on;
subplot(2,2,4)
difference = imshowpair(conv, second_conv, 'diff');
title('difference', 'FontSize', 15);
axis on;
Testing this on cameraman.tif gives the following result:

So as expected, barring some fft errors near the edges. However, if I test on a different image, of tubuls, I get this:

Here the convolutions do not match. I went away and did some reading about linear vs circular convolution, and how to get these results to match using padding. I padded my code, and now my result is this:

So padding the code has successfully matched the linear and circular convolution methods.
My questions are therefore:
What is different about cameraman.tif and tubuls.tif that causes them to have different results in my unpadded code? I think this is something to do with Matlab assuming a periodicity in the tupuls image; why are the two images not treated the same?
Finally, which is the 'right' way to do this? I notice that retesting cameraman.tif on the padded code removes the edge defects. Should I always include padding when performing convolution using fft2? If so, what is the point of the linear convolution, and when might people use it?
1 Kommentar
Antworten (0)
Siehe auch
Kategorien
Mehr zu Fourier Analysis and Filtering finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!