title: Correct usage of fft2 and fftshift for shape from shading
body: <p>I am attempting to recreate a classical shape from shading algorithm seen in the Trucco/Verri text "Introductory Techniques for 3d Computer Vision", but I am having a hard time understanding the fft function in matlab. Essentially, I need to use the integrability constraint to get the depth (Z) of an image. I am not sure when to use fftshift or not in this scenario. Here is the code I have so far. Based on <a href="http://www.mathworks.com/matlabcentral/newsreader/view_thread/285244" rel="nofollow">http://www.mathworks.com/matlabcentral/newsreader/view_thread/285244</a>
  I basically wrapped all my fft2s in fftshifts, but I don't think this is the correct usage. Could someone please explain to me the usage and what I am doing wrong? Thank you. Basically, I'm trying to take my p and q (values which are the updates based on pixel intensities) transform them into the Fourier domain so as to use them in the equation C. Then I want to transform the Equation C back to the time domain because that will give me Z the depth. I also want to update P and Q based on C in the Fourier Domain. </p>
  
  <pre><code>    wx = (2.* pi .* x) ./ m; 
      wy = (2.* pi .* y) ./ n; 
      wx = ifftshift(wx); wy=ifftshift(wy);
  
      Cp = fftshift(fft2(fftshift(p))); 
      Cq = fftshift(fft2(fftshift(q)));
      C = -1i.*(wx .* Cp + wy .* Cq)./(wx.^2 + wy.^2); 
      Z = abs((ifft2(ifftshift(C)))); 
      p = ifftshift(ifft2(ifftshift(1i * wx .* C))); 
      q = ifftshift(ifft2(ifftshift(1i * wy .* C)));
  </code></pre>