0
$\begingroup$

I have searched Google for circular cross correlation using Matlab, and I have found it only for one dimensional signals.

Could you please help me implementing circular cross correlation between two images using MATLAB?

$\endgroup$
2
$\begingroup$

Cross correlation can be implemented in the frequency domain using FFT by multiplying signals.
For a cyclic cross correlation you can use:

cc = ifft(fft(x).*conj(fft(y)));

You can also calculate the linear cross correlation using FFT by zero-padding the signals before the FFT.
Related: Cross correlation with FFT and fftshift

Edit:
In order to calculate the cross correlation of two images you have 2 options:

  1. x and y should be the grayscale representation of the images
  2. Repeat the process 3 times. Each time x and y are a single channel (r,g or b) from each image
$\endgroup$
  • $\begingroup$ using Matlab, x will be the matrix of the first image and y the matrix of the second image? $\endgroup$ – Noha Nov 14 '20 at 18:48
  • $\begingroup$ It depends. See the edit $\endgroup$ – ThP Nov 14 '20 at 19:00
  • $\begingroup$ It might be safe to 1) check that the imaginary part is negligible 2) use the real part of the result them. $\endgroup$ – Laurent Duval Nov 14 '20 at 19:46
  • $\begingroup$ function [ h ] = cxcorr_fft( a,b ) %CXCORR_FFT Calculates the circular crosscorrelation of the two input % vectors using the fft based method % calculate crosscorrelation e = fft2(a); f = fft2(b); g = f.*conj(e); h = fftshift(ifft2(g)); end $\endgroup$ – Noha Nov 20 '20 at 11:33
  • $\begingroup$ I have used the CXCORR_FFT function in matlab, which is used to find the circular cross correlation for two vectors. I have replaced the fft function by the fft2 function, as you told me. The code is in the previous comment. Unfortunately, the results are not acceptable. The value of correlation between an image and its self is approximately the same as the value of the correlation between an image and a different image !!! $\endgroup$ – Noha Nov 20 '20 at 11:38
1
$\begingroup$

For 1D signals MATLAB has the function cconv().
In order to apply correlation and not convolution just flip the signals:

c = cconv(a, b(end:-1:1));

For 2D, in case you the Image Processing toolbox you may use imfilter(). Its default mode is correlation:

c = imfilter(a2D, b2D);
c = imfilter(a2D, b2D, 'corr'); % Equals to the above
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.