Note that there are some explanatory texts on larger screens.

plurals
  1. POImage Convolution in Frequency Domain (MATLAB)
    text
    copied!<p>I'm writing a c++ program for a class to do convolution in the frequency domain, and I noticed the final result had error along the corner. So I tried it out in MATLAB and got the exact same results. e.g.</p> <p>Using cameraman from <a href="http://engronline.ee.memphis.edu/eece7214/images/Downlodable.htm" rel="nofollow">http://engronline.ee.memphis.edu/eece7214/images/Downlodable.htm</a></p> <p>I did </p> <pre><code>a = imread('cameraman.pgm'); h = ones(25,25)/25/25; a(512,512) = 0; h(512,512) = 0; c = ifft2(fft2(a).*fft2(h))/256; c = c(1:256, 1:256); c = real(c); imwrite(c,'test2.png') </code></pre> <p>I took a peek at c before extracting the top-left corner, and I found it was the same answer as imfilter(a, h) except it was translated a tad from the corner. Digital Image Processing by Gonzalez says nothing about this, and Google has left my eyes bleeding from searching with no help at all (everyone repeats the same instructions Gonzalez gives to extract the top left corner).</p> <p>Unrelated to the main question, I'd also like to know why I had to divide by 256 in this MATLAB code. In my C++ code, I didn't have to scale the result, and I got the same answer as this MATLAB code.</p> <p>edit: I did a little playing around with 1-dimensional vectors (doing conv and ifft(fft*fft)), and I think the 'error' is from the output showing the 'full' convolution in the top left corner instead of the 'same' convolution. But even if that were the case, I'm not sure how to deterministically code "extract this portion only to get the 'same' instead of the top left 256x256 portion of the 'full'"</p> <p>edit: More googling has resulted in a possible solution via <a href="http://jeremy.fix.free.fr/IMG/pdf/fftconvolution.pdf" rel="nofollow">http://jeremy.fix.free.fr/IMG/pdf/fftconvolution.pdf</a>. It has a lot of math symbols I've never seen before, but from what I can gather, if you are convolving an nxn and a mxm, extract m:(m+n-1) to get the 'same' convolution from the fft approximation. I'd still like to hear from someone who is more expert than I am, so don't choose not to comment based on this update!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload