我是Matplotlib,Python,FFT的新手。 我的任务是用FFT获得图像清晰度的信息,但我该怎么做呢?到目前为止我所做的:
#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的答案。我的新代码:
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和highpass?你知道吗
如果我有两张相同的照片,一张模糊,另一张锐利。以下是结果(链接,因为我不能直接上传图片): https://share-your-photo.com/e69b1128bchttps://share-your-photo.com/1ef71afa07
还有一个新问题: 我怎么能不看就把两张照片对比一下,说哪一张更清晰呢。我的意思是我怎么能编这样的程序? 有没有可能比较两个数组,并说明哪一个数组的值总体上更大(总体上更大的值意味着更清晰?) 目前我正在做这样的事情:
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。有没有更快的办法?你知道吗
抱歉,如果这个问题很愚蠢,但我是个笨蛋:-)
正如注释所指出的,您正在寻找高频(远离2D Fourier图中心的频率)。 我举一个综合的例子。我添加了一些噪音,使它更像一个真实的图像。 在第三行,我在中间展示了一个低通滤波器,将右侧的FFT频谱与之相乘,然后进行逆变换,得到左侧的滤波图像。所以我抑制了图像中的低频,现在只有尖锐的部分突出。 试试你的形象。你知道吗
相关问题 更多 >
编程相关推荐