# Cross-correlation and auto-correlation of an image using $\tt fft$

I'm working on a project where I have to find the auto-correlation and cross- correlation of types of pixels in an image:

I am using MATLAB and my code for the autocorrelation is:

fftimage = fft2(image);
congfft = conj(fftimage);%conjugate
ans1 = fftimage*congfft;
ans2 = ifft2(ans1);
ans3 = abs(ans2);
ans3 = fftshift(ans3);
image(ans3,'CDataMapping','scaled')


for the cross-correlation

 ans1 = fftimage*congfft2; %congfft2: conjugate the other type of pixel.


I don't understand clearly the outputs of the code. Here is the autocorrelation output of the yellow pixels:

For 1D signals, the autocorrelation of a rectangular pulse will be a triangular pulse.

For your image, the autocorrelation will be a triangular pulse in both directions.

The image below shows:

• The original image
• The autocorrelation without zero padding
• The autocorrelation with zero padding
• The autocorrelation with zero padding, viewed as a mesh.

For your example image, the zero padding is not required because the support of the non-zero pixels is way less than the image size. For non-zero pixels covering the whole image, you'll need to zero pad when using the FFT.

Your image does not appear to show the gradual change. I suspect you will need to normalize the pixel values manually. It's not clear to me that 'CDataMapping','scaled' does the right thing (it may, I'm just not familiar with it).

what does the output value in each pixel mean for the auto and the cross-correlation?

As calculated, not much. It would mean a little more if the mean was subtracted from the image before calculation. As it is, the most you are seeing is the auto-correlation of the mean pixel value (128?) over a limited range.

If the mean were subtracted, then it would tell you about how each pixel influences the pixels around it: if the autocorrelation values are close to zero, then that means "not much". If the autocorrelation values are high, then it means "greatly".

R Code Below

# 32079

par(mfrow=c(2,2))
image <- array(0,c(20,20))
image[8:13,8:13] <- 1
image(image, col= grey(seq(0, 1, length = 256)))
title("Original")

fftimage <- fft(image)
congfft <- Conj(fftimage)
ans1 <- fftimage * congfft
ans2 <- Re(fft(ans1, inverse=TRUE))/20/20
image(ans2, col= grey(seq(0, 1, length = 256)))