如何通过傅立叶变换获取有关图像清晰度的信息? [英] How to get Information about sharpness of Image with Fourier Transformation?

查看:109
本文介绍了如何通过傅立叶变换获取有关图像清晰度的信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Matplotlib,Python,FFT的新手. 我的任务是通过FFT获取有关图像清晰度的信息,但是我该如何完成呢?到目前为止,我所做的事情:

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()


基于roadrunner66的答案.我的新代码:


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()

有人可以验证我是否正确移植了代码.我必须乘以幅度和hishpass还是将fshift和高通相乘?

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

现在,如果我有两张相同的图片,但是一张模糊,另一张清晰.结果如下(链接,因为我无法直接上传图片): https://share-your-photo.com/e69b1128bc https://share-your-photo.com/1ef71afa07

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

还有一个新问题: 我如何比较两张图片而不说它,哪一张更清晰.我的意思是我该如何编程? 是否可以比较两个Array并说哪个总体上具有更大的值(总的来说,更大的值意味着更锐利?) 目前,我正在做类似的事情:

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

...

这似乎起作用,但是它非常慢,因为我循环并进行了40次FFT.有更快的方法吗?

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 :-)

推荐答案

正如评论所指出的,您正在寻找高频(远离2D傅里叶图中心的频率). 我举一个综合的例子.我添加了一些噪点,使其更类似于真实图像. 在第三行中,我在中间显示了一个低通滤波器,将FFT频谱与其右边相乘,然后进行逆变换以在左边获得滤波后的图像.因此,我抑制了图像中的低频,现在仅突出了锐利的部分. 尝试使用您的图片.

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()

这篇关于如何通过傅立叶变换获取有关图像清晰度的信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆