Sunday, August 17, 2014

Phase Correlation

This is more of a grumble than an insight :)

I wanted to use "phase correlation" to do some optical flow, trying to smooth out some wretched noise in a film we had made. I had read about phase correlation a few times, and decided to give it a go.

The Wikipedia page is great (as Wikipedia usually is.) In particular, it has good example pictures, and describes the math simply. Unfortunately, I believe that the math is unreadable. This is what it says:

Given two input images A and B

Apply a window function (e.g., a Hamming window) on both images to reduce edge effects. Then, calculate the discrete 2D Fourier transform of both images; FA and FB.

Calculate the cross-power spectrum by taking the complex conjugate of the second result, multiplying the Fourier transforms together elementwise, and normalizing this product elementwise.

     FA * FB*
R = ----------
     |FA FB*|

where FB* is the complex conjugate of FB and * is the Hadamard product of FA and FB*
What this means in code is this: [thank god for ]

// do the hadamard product
float size;
for(i = 0; i < ras1->chan_size; i++) {
     img[i][0] = out1[i][0] * out2[i][0]
         + out1[i][1] * out2[i][1];
     img[i][1] = out1[i][1] * out2[i][0]
         - out1[i][0] * out2[i][1];
     size = sqrt(img[i][0]*img[i][0] + img[i][1]*img[i][1]);
     img[i][0] /= size;
     img[i][1] /= size;
Now, how are those related? The Hadamard product part I get, but How could I have known that I should divide each element of the product by the sqrt of the sum of the squares of each element?

My problem is that this is typical math shorthand, it really only makes sense if you already know the answer. A typical example is if you are trying to write sin(x) * sin(x) it is typically written sin^2(x) -- this is shorthand, and it makes for easier reading if you already know what it means, but it's just crazy. It's like the word ain't; it ain't a word.

I suppose code is the new math, and I should be happy that it exists.