Re: How to speed up conv2 ?



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))
.



Relevant Pages

  • Re: How to speed up conv2 ?
    ... I got the following method to speed up the two-dimension ... A two-dimensional correlation calculation over a large image is ... bear in mind that FFT domain ... kernel must be properly padded. ...
    (comp.soft-sys.matlab)
  • Re: How to speed up conv2 ?
    ... I got the following method to speed up the two-dimension ... A two-dimensional correlation calculation over a large image is ... check out the Image Processing Toolbox function imfilter. ...
    (comp.soft-sys.matlab)
  • Generalized Cross-correlation
    ... I'm trying to generalize the cross-correlation to an arbitrary set of parameters rather than just translation. ... With the standard cross-correlation we see that shifting does not the extrema of the "kernel". ... Here, because of the p2 depenency, the correlation interpretation of the p-spectrum will generally not make much sense. ... For the standard cross-correlation we get a consistent view because the shifting parameter does not effect the kernel negatively. ...
    (sci.math)