Clarification on linear vs circular convolution

조회 수: 6 (최근 30일)
Thomas Gerard
Thomas Gerard 2016년 7월 7일
댓글: Rayyan Khan 2019년 8월 23일
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
Rayyan Khan
Rayyan Khan 2019년 8월 23일
Did you get the answer of your question? please let me know thanks.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Fourier Analysis and Filtering에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by