matlab - Seperable convolution using 1D FFT vs 2D FFT -


i trying use matlab convolve image gaussian filter using 2 methods: separable convolution using 1d fft , non-separable convolution using 2d fft. i'm expecting separable convolution faster. however, not small images, larger ones 2d faster. i'm not sure if it's problem implementation or if it's because don't have concept quite right.

here code:

img1 = randi([1,256],128,128);      % create gaassian filter rf1 = fspecial('gaussian', [1 128], 1.0); cf1 = transpose(rf1); gf1 = cf1 * rf1;      rc1 = round(conv2(img1, gf1, 'same')); rc1 = fft2dconv(img1, gf1); rc2 = fft1dconv(img1, rf1, cf1);   function o = fft1dconv(img, rowf, colf)  % zero-pad imgsize = size(img); rsize = size(rowf); csize = size(colf);  img = padarray(img, [imgsize(1)/2, imgsize(2)/2]); rowf = padarray(rowf, [2*imgsize(1)-rsize(1), 2*imgsize(2)-rsize(2)], 'post'); colf = padarray(colf, [2*imgsize(1)-csize(1), 2*imgsize(2)-csize(2)], 'post');   % seperable convolution using 1d fft tic; result = fft(transpose(fft(img))) .* fft(transpose(fft(colf))); result = result .* fft(transpose(fft(rowf))); o = transpose(round(real(ifft2(result)))); toc;   % remove pad o = o(imgsize(1)+1:2*imgsize(1),imgsize(2)+1:2*imgsize(2));  end   function o = fft2dconv(img, filter)  %zero-pad imgsize = size(img); fsize = size(filter);  img = padarray(img, [imgsize(1)/2, imgsize(2)/2]); filter = padarray(filter, [2*imgsize(1)-fsize(1), 2*imgsize(2)-fsize(2)], 'post');  % non-seperable convolution using 2d fft tic; o = round(real(ifft2(fft2(img) .* fft2(filter)))); toc;  % remove pad o = o(imgsize(1)+1:2*imgsize(1),imgsize(2)+1:2*imgsize(2));  end 

and timing results are:

elapsed time 0.003315 seconds. elapsed time 0.004369 seconds. 

for 4 x 4 image, separable method faster, larger images. not case , i'm not sure why. appreciated.

i suggest profile code see going on. other operations you're doing instead of core fft calculations themselves.

go matlab , type in profile viewer. once that, run command in window , let finish. once finishes, identify intensive parts of code , can figure out how optimize there. right in saying separable filters faster.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

c# - Unity IoC Lifetime per HttpRequest for UserStore -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -