How to get Information about sharpness of Image with Fourier Transformation?

徘徊边缘 提交于 2020-03-03 08:39:34

问题


i am rookie with Matplotlib, Python, FFT. My Task is to get information about sharpness of a Image with FFT, but how do i get this done? What i have done so far:

#getImage:

imgArray2 = Camera.GetImage()
imgArray2 = cv2.flip(imgArray2, 0)
grayImage = Image.fromarray(imgArray2).convert('L')

#Fast Fourier Transformation:
f = np.fft.fft2(grayImage)

#Shift zero frequency to Center
fshift = np.fft.fftshift(f)

#Shows Result of FFT:
#plt.imshow(np.abs(np.log10(fshift)), cmap='gray')

#Try to Plot the result (this code is an example which i tried to modify):
N = 600
T = 1.0 / 800.0

xf = np.linspace(0.0, 1.0 / (2.0 + T), N / 2)

plt.plot(xf, 2.0 / N * np.abs(fshift[:N // 2]))

plt.title('Fourier Transformation')
plt.show()


EDIT: Based on the answer of roadrunner66. My new Code:

imgArray2 = Camera.GetImage()
imgArray2 = cv2.flip(imgArray2, 0)
grayImage = Image.fromarray(imgArray2).convert('L')

f = np.fft.fft2(grayImage)
fshift = np.fft.fftshift(f)

magnitude_spectrum = 20 * np.log(np.abs(fshift))

x = np.linspace(0, 1, 1024)
y = np.linspace(0, 1, 768)
X, Y = np.meshgrid(x, y)

highpass = 1 - np.exp(- ((X - 0.5) ** 2 + (Y - 0.5) ** 2) * 5)
print(np.shape(highpass))
f2 = fshift * highpass
z3 = np.absolute(np.fft.ifft2(f2))

plt.subplot(337)
plt.imshow(z3)
plt.title('only high frequency content survived')
plt.colorbar()
plt.subplot(338)
plt.imshow(highpass)
plt.title('highpass, suppresses \n low frequencies')
plt.colorbar()
plt.subplot(339)
plt.imshow(np.log10(np.abs(fshift * highpass)), cmap='gray')
plt.title('FFT*highpass')
plt.colorbar()
plt.show()

Can someone verify if i correctly ported the Code. Must i multiply magnitude and hishpass OR fshift and highpass?

Now if i have two pictures which are same, but one is blurry and the other one is sharp. Here are the results (Link, because i can not upload directly pictures): https://share-your-photo.com/e69b1128bc https://share-your-photo.com/1ef71afa07

Also a new Question: How can i compare two pictures with each to say which one is sharper without looking at it. I mean how can i program something like that? Is it possible to compare two Array and say which one has overall bigger values (overall bigger Values means more sharper?) Currently i am doing something like that:

sharpest = 0
sharpestFocus = 0

# Cam has a Focus Range from 0 to 1000
while i < 1000:
i = i + 25

#Set Focus Value to Camera
...

a = np.sum(np.log10(np.abs(fshift * highpass)) / np.log10(np.abs(fshift * highpass)).size)

if sharpest < a:
    sharpest = a
    sharpestFocus = i

...

This seems to work but it is very slow, because i loop and make 40 FFTs. Is there a faster way to do that?

Sorry if this question is stupid, but i am a noob :-)


回答1:


As the comments pointed out, you are looking for high frequencies (frequencies away from the center of your 2D Fourier plot). I'm giving a synthetic example. I added some noise to make it more similar to a real image. In the 3rd line I'm showing a lowpass filter in the middle, multiply the FFT spectrum to the right with it and inverse transform to get the filtered image on the left. So I suppressed the low frequencies in the image and only the sharp portions stand out now. Try with your image.

import numpy as np
import matplotlib.pyplot as p
%matplotlib inline

n=200
x=np.linspace(0,1,n)
y=np.linspace(0,1,n)
X,Y=np.meshgrid(x,y)
z=np.zeros((n,n))
z1= np.sin(2*np.pi*X*5)* np.cos(2*np.pi*Y*20)  +1/20*np.random.random(np.shape(z))

z2=np.copy(z1)
for i in range(30):
    z2[ i*10: 3+i*10, 100+i*3:103+i*3]=2

#Fast Fourier Transformation:
def f(z):
    return np.fft.fftshift(np.fft.fft2(z))



highpass=1-np.exp(- ((X-0.5)**2+(Y-0.5)**2)*5)
print(np.shape(highpass))
f2=f(z2)*highpass
z3= np.absolute( np.fft.ifft2(f2)) 


#Shows Result of FFT:
p.figure(figsize=(15,12))
p.subplot(331)
p.imshow( z1)
p.colorbar()
p.title('soft features only')
p.subplot(333)
p.imshow(np.abs( np.log10(f(z1)) ), cmap='gray')
p.title('see the spatial frequencies +/-5 from center in x, +/-20 in y')
p.colorbar()

p.subplot(334)
p.imshow( z2)
p.colorbar()
p.title('add some sharp feature')
p.subplot(336)
p.imshow(np.abs(np.log10(f(z2))), cmap='gray')
p.title('higher frequencies appear ()')
p.colorbar()

p.subplot(337)
p.imshow(z3)
p.title('only high frequency content survived')
p.colorbar()
p.subplot(338)
p.imshow( highpass)
p.title('highpass, suppresses \n low frequencies')
p.colorbar()
p.subplot(339)
p.imshow( np.log10(np.abs(f(z2)*highpass)), cmap='gray')
p.title('FFT*highpass')
p.colorbar()



来源:https://stackoverflow.com/questions/58392838/how-to-get-information-about-sharpness-of-image-with-fourier-transformation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!