简体   繁体   English

如何从原始 TEM tiff 获取 FFT 图像?

[英]How to get the FFT image from a raw TEM tiff?

I'm trying to write code that takes TEM (Transmission Electron Microscope) TITFF images, and computes the FFT.我正在尝试编写获取 TEM(透射 Electron 显微镜)TITFF 图像并计算 FFT 的代码。 But I always get plain Red, Green or Blue images.但我总是得到普通的红色、绿色或蓝色图像。

Here's what the RAW TEM images look like:这是 RAW TEM 图像的样子:

原始 TEM Tiff

Here's what the FFT image should look like: FFT 图像应如下所示:

GMS软件制作的FFT

But instead I get:但相反,我得到:

代码中的 FFT

Here's my code:这是我的代码:

import numpy as np
import diplib as dip
import matplotlib.pyplot as plt
from PIL import Image
from ncempy.io import dm


img1 = dip.ImageReadTIFF('RAW_FFT.tif')
f = np.fft.fft2(img1)
f = np.fft.fftshift(f)

plt.imshow(abs(f))
plt.show()

Do you have any idea what could be the problem?你知道可能是什么问题吗? I even tried to convert the image to np.array and do FFT step by step but I get the same result.我什至尝试将图像转换为np.array并逐步进行 FFT,但我得到了相同的结果。

FFT is complex and without a logarithm, Fourier transform would be so much brighter than all the other points that everything else will appear black. FFT 是复杂的并且没有对数,傅立叶变换将比所有其他点亮得多,以至于其他所有点都将显示为黑色。 see for details: https://homepages.inf.ed.ac.uk/rbf/HIPR2/fourier.htm详见: https://homepages.inf.ed.ac.uk/rbf/HIPR2/fourier.htm

import cv2
import numpy as np

img=cv2.imread('inputfolder/yourimage.jpg',0)
def fft_image_inv(image):
    f = np.fft.fft2(image)
    fshift = np.fft.fftshift(f)
    magnitude_spectrum = 15*np.log(np.abs(fshift))
    return magnitude_spectrum

fft= fft_image_inv(img)
cv2.imwrite('outputfolder/yourimage.jpg',fft)

output: output: 在此处输入图像描述

There are multiple issues here.这里有多个问题。 First, sometimes grayscale images are written to file as if they were RGB images (in a TIFF file, this could be as simple as storing a grayscale color map, the pixel values will be interpreted as indices into the map, and the loaded image will be an RGB image instead of a grayscale image, even through it has only grayscale colors).首先,有时灰度图像会像 RGB 图像一样写入文件(在 TIFF 文件中,这可能就像存储灰度颜色 map 一样简单,像素值将被解释为 map 的索引,加载的图像将是 RGB 图像而不是灰度图像,即使它只有灰度颜色)。

This is the case here.这里就是这种情况。 All three channels have exactly the same information, but there are three channels stored, and your FFT will compute the same thing three times!所有三个通道具有完全相同的信息,但存储了三个通道,您的 FFT 将计算相同的东西 3 次!

After loading the image with dip.ImageReadTIFF() , you can use parentheses to index one of the channels:使用dip.ImageReadTIFF()加载图像后,您可以使用括号来索引其中一个通道:

img1 = dip.ImageReadTIFF('RAW_FFT.tif')
img1 = img1(0)

We now have an actual gray-scale image.我们现在有一个实际的灰度图像。 This should get rid of the red color in the output.这应该可以消除 output 中的红色。

After computing the FFT, we have a floating-point image with a very high dynamic range (the largest magnitude, at the middle pixel, is 437536704).在计算 FFT 之后,我们得到了一个动态范围非常高的浮点图像(中间像素处的最大幅度为 437536704)。 pyplot, by default, will show floating-point images with 0 and all negative values as black, and 1 and all larger values as white (actual colors depend of course on the color map it uses).默认情况下,pyplot 将显示浮点图像,其中 0 和所有负值显示为黑色,1 和所有更大的值显示为白色(实际 colors 当然取决于它使用的颜色 map)。 So your display will be all white.所以你的显示器将全是白色的。 Use the vmax parameter to imshow to determine the value shown as white.使用vmax参数来imshow显示为白色的值。 Setting this to 1e6 should give you a similar display as in the GMS software.将此设置为 1e6 应该会给您与 GMS 软件中类似的显示。

Instead of pyplot you can use DIPlib for display.您可以使用 DIPlib 代替 pyplot 进行显示。 Its interactive viewer will let you use a slider to manually set the grayscale limits, and you can manually select to display the magnitude, as well as choose a logarithmic mapping (which tend to be most useful for displaying the frequency domain).它的交互式查看器将让您使用 slider 手动设置灰度限制,您可以手动 select 显示幅度,以及选择对数映射(这往往对显示频域最有用)。

f = dip.FourierTransform(img)
dip.viewer.ShowModal(f)

Alternatively, you can use a static display, which uses pyplot under the hood:或者,您可以使用 static 显示器,它在引擎盖下使用 pyplot:

f.Show((0, 1e6))

or或者

f.Show('log')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM