Re: How to speed up conv2 ?
- From: "Surapong Lertrattanapanich" <boheman@xxxxxxxxx>
- Date: Wed, 31 May 2006 15:02:34 -0400
Daniel Mark wrote:
Hello all:
I got the following method to speed up the two-dimension
correlation
calculation, but I need helps to implement it in matlab.
A two-dimensional correlation calculation over a large image is
very
time-consuming,
and to overcome this, we can do the correlation in the frequency
domain by multiplying the
two-dimensional FFTs the Hermitian template and the image, then
took
the inverse FFT
to obtain the output image.
Anyone can help?
THank you very much
-Daniel
You must be very careful to work on the implementation of 2-D
correlation thru FFT domain. First, bear in mind that FFT domain
assume periodic boundary condition (circular shift). Second, the
kernel must be properly padded. Third, to perform correlation in FFT
domain, the kernel must be rotated 180 degree (not required if kernel
is circularly symmetric which is true in general). Fourth, the output
of the FFT correlation will be the same size as the original.
Here is the simple implementation (in both spatial and FFT domains)
of the 2-D correlation between input image A and kernel h.
clear;
A = rand(256); % input image
h = rand(128); % kernel
% Spatial domain
tic;
sp_out = imfilter(A, h, 'circular', 'same', 'corr'); % 2D correlation
disp('Spatail domain takes (sec)');
toc;
% FFT domain
tic;
AA = fft2(A);
h_pad = zeros(size(A));
h_pad(1:size(h,1), 1:size(h,2)) = rot90(rot90(h));
hsize2 = floor(size(h)/2);
h_pad = circshift(h_pad, -hsize2);
HH = fft2(h_pad);
OUT = AA.*HH;
fft_out = real(ifft2(OUT));
disp('FFT domain takes (sec)');
toc;
disp('The sum of error');
err = sum(sum(sp_out - fft_out))
.
- References:
- How to speed up conv2 ?
- From: Daniel Mark
- How to speed up conv2 ?
- Prev by Date: Re: Histogram of non-zero cells
- Next by Date: Changing Default Page Setup w Commands
- Previous by thread: Re: How to speed up conv2 ?
- Next by thread: Doubt regarding "imfill" function from "DIP using Matlab" textbook
- Index(es):
Relevant Pages
|