I'm currently visual effects supervisor on the new Fast and Furious movie. Every now and then, I come up with some ideas on graphics programming that I'd like to share, so I started this blog.

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 http://en.wikipedia.org/wiki/Phase_correlation 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.

&nbsp FA * FB*
R = ----------
&nbsp |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 http://read.pudn.com/downloads163/sourcecode/windows/741399/Phase%20Correlation.doc ]

// do the hadamard product
float size;
for(i = 0; i < ras1->chan_size; i++) {
&nbsp img[i][0] = out1[i][0] * out2[i][0]
&nbsp &nbsp + out1[i][1] * out2[i][1];
&nbsp img[i][1] = out1[i][1] * out2[i][0]
&nbsp &nbsp - out1[i][0] * out2[i][1];
&nbsp size = sqrt(img[i][0]*img[i][0] + img[i][1]*img[i][1]);
&nbsp img[i][0] /= size;
&nbsp 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.

3 Comments:

Anonymous said...

You divide by the sqrt of the sum of the squares of each element, because that´s the magnitude of a complex number, as indicated by the |...| symbol. Simply basic school math, which you can refresh here .

You divide by the sqrt of the sum of the squares of each element, because that´s the magnitude of a complex number, as indicated by the |...| symbol. Simply basic school math, which you can refresh here https://en.wikipedia.org/wiki/Complex_number.