本文中利用Numpy包对CT图像进行傅里叶变换,生成幅度和相位图。最后再通过逆傅里叶变换重新得到CT图像。
1. 打开Jupyter notebook后,先导入以下科学包import numpy as npimport mathimport pydicomimport osimport matplotlib.pyplot as plt
2. 利用load_scan()和get_pixels_hu()函数读取CT文件。如果对这两个函数不清楚,可以参看篇推文《利用Python打开DICOM CT文件》
# open CT_lung datasetdir_path_ct = './Data/HNSCC_01/'patient_ct = load_scan(dir_path_ct)imgs_ct = get_pixels_hu(patient_ct)# For "NotImplementedError: this transfer syntax JPEG 2000 Image Compression, # can not be read because Pillow lacks the jpeg 2000 decoder plugin"# https://pydicom.github.io/pydicom/stable/tutorials/installation.html#installing-pillow
3. 利用np.fft.fft2()对图像进行傅里叶变换
# Fourier Transferfrom matplotlib.colors import LogNorm# CTct_ft = np.fft.fft2(imgs_ct[9])imgs_ct_ft = np.fft.fftshift(ct_ft) # Shift the zero-frequency component to the center of the spectrum.imgs_ct_magnitude = np.abs(imgs_ct_ft)imgs_ct_phase =np.arctan2(np.imag(imgs_ct_ft), np.real(imgs_ct_ft)) * 180 / np.pi
np.fft.fft2()的返回值为复数。根据复数的实部与虚部可以得到幅度(imgs_ct_magnitude )和相位(imgs_ct_phase )信息。
np.fft.fftshift()对频域谱进行移动使零频率分量位于频谱中心。这更符合通常所见的频域图像习惯。
4. 利用matplotlib.pyplot绘制幅度和相位图像。由于图像单个像素点的值范围很大,不利于直观表现,所以利用norm = LogNorm()进行对数处理。
plt.figure(figsize=(10, 10))plt.subplot(121)plt.title('CT Slice_10 (Magnitude)')plt.imshow(imgs_ct_magnitude, cmap='gray', norm = LogNorm()) # we apply a logarithmic transformation here to better display the dynamic range of the magnitudeplt.subplot(122)plt.title('CT Slice_10 (Phase)')plt.imshow(imgs_ct_phase, cmap='gray')plt.show()

5. 利用np.fft.ifft2()进行逆傅里叶变换。注意要利用np.fft.ifftshift()还原频域谱
# inverse Fourier transform# CTimgs_ct_ift = np.fft.ifftshift(imgs_ct_ft)ct_ift = np.real(np.fft.ifft2(imgs_ct_ift))ct_ift = ct_ift.astype(np.int16)
np.fft.ifft2()返回值同样是一个复数,但是虚部值基本为0,这里直接使用实部作为逆傅里叶变换后的图像。
6. 利用matplotlib.pyplot绘制图像
plt.figure(figsize=(10, 10))plt.subplot(131)plt.title('CT Slice_10 (Origin)')plt.imshow(imgs_ct[10], cmap='gray')plt.subplot(132)plt.title('CT Slice_10 (Inverse FT)')plt.imshow(ct_ift, cmap='gray') # we apply a logarithmic transformation here to better display the dynamic range of the magnitudeplt.subplot(133)plt.title('Orgin - Reconstructed')plt.imshow(np.round(imgs_ct[10] - ct_ift), cmap='gray')plt.show()

直观上可以看到,重新得到的图像与原图之间有一定差别,但通过np.max(imgs_ct[9] - ct_ift)得到结果为1,这表示像素点间的差别很小(最大仅为1),所以可以认为重新得到的图像基本等于原图。
看完本文有收获?请分享给更多人
推荐阅读





关注「质子重离子放疗」公众号
与中国物理师一同成长

本文介绍如何使用Python的Numpy库对CT图像进行傅里叶变换,生成幅度和相位图像,并通过matplotlib进行显示。还讨论了逆傅里叶变换的过程,以重新获取接近原始图像的结果。在傅里叶变换后,利用LogNorm进行对数处理以更好地显示图像,最终通过ifft2和ifftshift恢复图像,证实了变换后与原图的像素差异极小。

2万+

被折叠的 条评论
为什么被折叠?



