Illustrate the discrete convolution theorem.
In this demonstration, the convolution theorem is illustrated by comparing the result of the convolution with its equivalent filtering in frequency domain. The convolution used is the periodic discrete convolution with kernel origin at the array origin.
Reading the image
The image is read and displayed
1. import ia636 as ia 2. 3. fin = adreadgray('lenina.pgm') 4. adshow(fin)
Convolution with the Laplacian kernel
The image is convolved (periodicaly) with the 3x3 diagonal gradient:
1. fd = fin.astype(float) 2. h = array([[-2,-1,0], 3. [-1, 0,1], 4. [ 0, 1,2]]) 5. g = ia.iapconv2(fd,h) 6. adshow(ia.ianormalize(g,[0,255]))
Equivalent filter in frequency domain
The 3x3 kernel is zero padded to the size of the input image. Its spectrum is visualized.
1. hx = zeros(fd.shape) 2. hx[:h.shape,:h.shape] = h 3. H = ia.iadft(hx); 4. adshow(ia.iadftview(H))
Filtering in the frequency domain
The image is filtered by multiplying its DFT by the frequency mask computed in the previous step.
1. F = ia.iadft(fd) 2. G = F * H 3. print "Is symmetrical:", ia.iaisdftsym(G) 4. adshow(ia.iadftview(G)) 5. g_aux = ia.iaidft(G).real 6. adshow(ia.ianormalize(g_aux).astype(uint8))
Is symmetrical: True
Comparing the results
Both images, filtered by the convolution and filtered in the frequency domain are compared to see that they are the same. The small differencies are due to numerical precision errors.
1. e = abs(g - g_aux) 2. print "Max error:", e.max()
Max error: 6.03213447903e-06