本章主要讲解频域域滤波的技术,主要技术用到是大家熟悉的傅里叶变换与傅里叶反变换。这里有比较多的篇幅讲解的傅里叶的推导进程,用到Numpy傅里叶变换。本章理论基础比较多,需要更多的耐心来阅读,有发现有错误,可以与我联系。谢谢!
目录
- 背景
- 傅里叶级数和变换简史
import sys
import numpy as np
import cv2
import matplotlib
import matplotlib.pyplot as plt
import PIL
from PIL import Imageprint(f"Python version: {sys.version}")
print(f"Numpy version: {np.__version__}")
print(f"Opencv version: {cv2.__version__}")
print(f"Matplotlib version: {matplotlib.__version__}")
print(f"Pillow version: {PIL.__version__}")
Python version: 3.6.12 |Anaconda, Inc.| (default, Sep 9 2020, 00:29:25) [MSC v.1916 64 bit (AMD64)]
Numpy version: 1.16.6
Opencv version: 3.4.1
Matplotlib version: 3.3.2
Pillow version: 8.0.1
def normalize(mask):return (mask - mask.min()) / (mask.max() - mask.min() + 1e-8)
背景
傅里叶级数和变换简史
内容比较多,请自行看书,我就实现一维的傅里叶变换先。
卷积用大小为m×nm\times nm×n元素的核对大小为M×NM\times NM×N的图像进行滤波时,需要运算次数为MNmnMNmnMNmn。如果核是可分享的,那么运算次数为MN(m+N)MN(m + N)MN(m+N),而在频率域执行等交的滤波所需要的运算次数为2MNlog2MN2MN\text{log}_2MN2MNlog2MN,2表示计算一次正FFT和一次反FFT。
Cn(m)=M2m22M2log2M2=m24log2M(4.1)C_n(m) = \frac{M^2 m^2}{2M^2 \text{log}_2}M^2 = \frac{m^2}{4 \text{log}_2 M}\tag{4.1}Cn(m)=2M2log2M2m2M2=4log2Mm2(4.1)
如果是可分离核,则变为
Cs(m)=M2m22M2log2M2=m2log2M(4.2)C_s(m) = \frac{M^2 m^2}{2M^2 \text{log}_2 M^2} = \frac{m}{2 \text{log}_2 M} \tag{4.2}Cs(m)=2M2log2M2M2m2=2log2Mm(4.2)
当C(m)>1C(m) > 1C(m)>1时,FFT的方法计算优势更大;而C(m)≤1C(m) \leq 1C(m)≤1时,空间滤波的优势更大
# FFT 计算的优势
M = 2048
m = np.arange(0, 1024, 1)
c_n = m**2 / (4 * np.log2(M))
c_s = m / (2 * np.log2(M))
fig = plt.figure(figsize=(10, 5))
ax_1 = fig.add_subplot(1, 2, 1)
ax_1.plot(c_n)
ax_1.set_xlim([0, 1024])
ax_1.set_xticks([3, 255, 511, 767, 1023])
ax_1.set_ylim([0, 25*1e3])
ax_1.set_yticks([0, 5*1e3, 10*1e3, 15*1e3, 20*1e3, 25*1e3])
ax_2 = fig.add_subplot(1, 2, 2)
ax_2.plot(c_s)
ax_2.set_xlim([0, 1024])
ax_2.set_xticks([3, 255, 511, 767, 1023])
ax_2.set_ylim([0, 5])
ax_2.set_yticks([0, 10, 20, 30, 40, 50])
plt.show()
def set_spines_invisible(ax):ax.spines['left'].set_color('none')ax.spines['right'].set_color('none')ax.spines['top'].set_color('none')ax.spines['bottom'].set_color('none')
# 不同频率的叠加
x = np.linspace(0, 1, 500)t = 50
A = 1
y_1 = A * np.sin(t * 2 * np.pi * x)t = 20
A = 2.5
y_2 = A * np.sin(t * 2 * np.pi * x)t = 5
A = 3
y_3 = A * np.sin(t * 2 * np.pi * x)t = 2
A = 20
y_4 = A * np.sin(t * 2 * np.pi * x)y_5 = y_1 + y_2 + y_3 + y_4fig = plt.figure(figsize=(8, 8))ax_1 = fig.add_subplot(5, 1, 1)
plt.plot(x, y_1), plt.xticks([]), plt.yticks([])
set_spines_invisible(ax_1)ax_2 = fig.add_subplot(5, 1, 2)
plt.plot(x, y_2), plt.xticks([]), plt.yticks([])
set_spines_invisible(ax_2)ax_3 = fig.add_subplot(5, 1, 3)
plt.plot(x, y_3), plt.xticks([]), plt.yticks([])
set_spines_invisible(ax_3)ax_4 = fig.add_subplot(5, 1, 4)
plt.plot(x, y_4), plt.xticks([]), plt.yticks([])
set_spines_invisible(ax_4)ax_5 = fig.add_subplot(5, 1, 5)
plt.plot(x, y_5), plt.xticks([]), plt.yticks([])
set_spines_invisible(ax_5)plt.tight_layout()
plt.show()