傅里葉變換 高通濾波 低通濾波

幅值譜:頻率和幅值的關係。中心爲頻率最小點。因此幅值譜中心部分代表的是低頻信息,即空間域的平緩的部分。越往外代表的是高頻信息,空間域的邊緣啊噪聲等

頻率:以正弦波爲例,就是週期的倒數

幅值:週期內最大值

幅值高(幅值譜越亮的部分)代表像素越多。一幅圖像經過傅里葉變換之後一定是中心較亮,四周較暗(低頻像素多,高頻像素少),因爲一般一幅圖像中高頻信息(邊緣等)是很少的,多的是背景。


高通濾波,低通濾波是按頻率劃分的。對於正弦曲線,振幅變化快就是高頻,變化慢就是低頻。那麼對於圖像,像素值變化快就是高頻(噪聲啦,邊緣啦、邊緣及噪聲在局部像素值變化較大),像素值變化慢就是低頻。所以一幅圖像低頻較多,高頻較少。那麼保留高頻就是高通濾波器(邊緣提取),保留低頻就是低通錄波器(圖像平滑)。
傅里葉變換將圖像轉換成幅值譜(magnitude_spectrum):按頻率從小到大由中心向四周擴散,幅值譜越亮說明像素越多,反之則越少。
上代碼:

import cv2
import numpy as np
import matplotlib.pyplot as plt
img=cv2.imread('../images/example_01.jpg',0)
# 高通濾波 numpy 用的多
# 正變換
f=np.fft.fft2(img)
fshift=np.fft.fftshift(f)
magnitude_spectrum=20*np.log(np.abs(fshift))


plt.subplot(121),plt.imshow(img,cmap='gray'),plt.title('Input Image'),plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(magnitude_spectrum,cmap='gray'),plt.title('magnitude_spectrum')
plt.show()


上效果:

那麼像提取圖像的邊緣就是去掉低頻保留高頻:先通過傅里葉正變換得到幅值譜,去掉低頻像素,然後再通過傅里葉逆變換得到圖像邊緣。上代碼:

import cv2
import numpy as np
import matplotlib.pyplot as plt
# img=cv2.imread('../images/can.jpg',0)
# img=cv2.imread('../images/qin.jpg',0)
img=cv2.imread('../images/example_01.jpg',0)
# 高通濾波 numpy 用的多
# 正變換
f=np.fft.fft2(img)
fshift=np.fft.fftshift(f)
magnitude_spectrum=20*np.log(np.abs(fshift))


rows,cols=img.shape
crow,ccol=int(rows/2),int(cols/2)
print('img.shape',img.shape)
# 低頻過濾
fshift[(crow-30):(crow+30),(ccol-30):(ccol+30)]=0
#逆變換
f_ishift=np.fft.ifftshift(fshift)
img_back=np.abs(np.fft.ifft2(f_ishift))


plt.subplot(121),plt.imshow(img,cmap='gray'),plt.title('Input Image'),plt.xticks([]),plt.yticks([])
plt.subplot(122),plt.imshow(img_back,cmap='gray'),plt.title('img_back'),plt.xticks([]),plt.yticks([])
plt.show()


上效果:


那麼均值濾波,高斯濾波,sobel,scharr,拉普拉斯濾波器是低通濾波器還高通濾波器呢?我們通過幅值譜來判
斷,上代碼:

import cv2
import numpy as np
from matplotlib import pyplot as plt


# simple averaging filter without scaling parameter
# 低通
mean_filter = np.ones((3,3))


# creating a guassian filter
# 低通
x = cv2.getGaussianKernel(5,10)
gaussian = x*x.T


# different edge detecting filters
# scharr in x-direction
# 高通
scharr = np.array([[-3, 0, 3],
                   [-10,0,10],
                   [-3, 0, 3]])
# sobel in x direction
# 高通
sobel_x= np.array([[-1, 0, 1],
                   [-2, 0, 2],
                   [-1, 0, 1]])
# sobel in y direction
# 高通
sobel_y= np.array([[-1,-2,-1],
                   [0, 0, 0],
                   [1, 2, 1]])
# laplacian
# 高通
laplacian=np.array([[0, 1, 0],
                    [1,-4, 1],
                    [0, 1, 0]])


filters = [mean_filter, gaussian, laplacian, sobel_x, sobel_y, scharr]
filter_name = ['mean_filter', 'gaussian','laplacian', 'sobel_x', \
                'sobel_y', 'scharr_x']
fft_filters = [np.fft.fft2(x) for x in filters]
fft_shift = [np.fft.fftshift(y) for y in fft_filters]
mag_spectrum = [np.log(np.abs(z)+1) for z in fft_shift]


for i in range(6):
    plt.subplot(2,3,i+1),plt.imshow(mag_spectrum[i],cmap = 'gray')
    plt.title(filter_name[i]), plt.xticks([]), plt.yticks([])
plt.show()


上效果:


前面討論過,幅值譜中間爲低頻,四周爲高頻且越亮像素越多,就是被濾波器保存下來了。不難發現,均值與高斯濾波器幅值譜中間亮,爲低通濾波器(平滑),拉普拉斯、sobel、scharr四周較亮爲高通濾波器(邊緣提取),邊緣提取又分垂直邊緣與水平邊緣~這個從圖上看就一目瞭然啦。
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章