Image filtering in frequency domain python
Hi everybody,
I am new in programming and I would like to apply a filter on an image in frequency domain. actually, its from a paper and i want to re implement it. its the formula: im_out= (1+ 5*((1-e^-f)/f)) * im_in and here are my codes:
im = cv2.imread('lena.jpg',0)
x=im.shape[0]
y=im.shape[1]
fft= np.fft.fft2(im)
fshift= np.fft.fftshift(fft)
freqx = np.fft.fftfreq(fft.shape[0])
freqy = np.fft.fftfreq(fft.shape[1])
zr = freqx==0
freqx[zr]=0.000000001
for i in xrange(x):
for j in xrange(y):
filter = 1+(5*(1-np.e**(-1*(np.sqrt((freqx)**2 + (freqy)**2))))/(np.sqrt((freqx)**2 + (freqy)**2)))
fim= fshift* filter
fishift= np.fft.ifftshift(fim)
imback=np.fft.ifft2(fishift)
imback=np.uint8(np.real(imback))
plt.subplot(221)
plt.imshow(im,cmap='gray')
plt.subplot(222)
plt.imshow(imback,cmap='gray')
plt.show()
but i get the image without any visible changes, it should be kind of low pass filter. now I am wondering if its correct to use np.fft.fftfreq to find "spatial frequency in the image plane". or I should use distance from center as "f"?!!! in the paper they said "f" is spatial frequency of the image plane!!!!
could anybody help me plz !!!
I don't understand python code but you don't need fftshift. Normalize frequency fc for an image of nbCols and nbRows is given by fcx=i/nbCols and fcy = j/nbRows your f value is something like f=sqrt(fcx^2+fcy^2) Becarefull for real signal you have a hermtian symetry for your filter
LBerger, thx for your answer. u are telling that i don't need to shift the image to have DC in the center. and just divide the pixel number to the number of rows and col ? what do u mean by hermtian symetry? how it affects?
It was Hermitianif i(x,y) is your image with real number (not complex)and I=F(i(x,y))is fourier transform of i then I(x,y)=conjugate(I(nbCols-x,nbrows-y)) You can find this in signal processing book. Your filter must have this symetry Fftshift is used for graphic representation : we like to have O(0,0) at the center of the figure
Ah got it, its symmetric. but its not working like this i won't get a blur image it just make the image a little darker just it!!!!
Yes Frequency response of your filter
you mean that the filter is not for blurring?????? it should blur the image, but just make it a little darker!!!