Python使用低通濾波器模糊圖像功能實現(xiàn)
使用低通濾波器模糊圖像
0. 前言
低通濾波器 (Low Pass Filter
, LPF
) 過濾了圖像中的高頻部分,并僅允許低頻部分通過。因此,在圖像上應用 LPF
會刪除圖像中的細節(jié)/邊緣和噪聲/離群值,此過程也稱為圖像模糊(或平滑),圖像平滑可以作為復雜圖像處理任務的預處理部分。在本節(jié)中,我們將學習如何使用不同類型的濾波器核執(zhí)行圖像卷積以模糊圖像。
1. 頻域中的不同類型的核與卷積
1.1 圖像模糊分類
圖像模糊通常包含以下類型:
- 邊緣模糊 (
Edge
) 這種類型的模糊通常通過卷積顯式地應用于圖像,例如線性濾波器核或高斯核等,使用這些濾波器核可以平滑/去除圖像中不必要的細節(jié)/噪聲。 - 運動模糊 (
Motion
) 通常是由于相機在拍攝圖像時抖動所產(chǎn)生的,也就是說,攝像機或被拍攝的對象處于移動狀態(tài)。我們可以使用點擴展函數(shù)來模擬這種模糊。 - 失焦模糊 (
de-focus
) 當相機拍攝的對象失焦時,會產(chǎn)生這種類型的模糊;我們可以使用模糊 (blur
) 核來模擬這種模糊。
接下來,我們創(chuàng)建以上三種不同類型的核,并將它們應用于圖像以觀察不同類型核處理圖像后的結果。
1.2 使用不同核執(zhí)行圖像模糊
(1) 我們首先定義函數(shù) get_gaussian_edge_blur_kernel()
以返回 2D
高斯模糊核用于邊緣模糊。該函數(shù)接受高斯標準差 ( σ σ σ) 以及創(chuàng)建 2D
核的大小(例如,sz = 15
將創(chuàng)建尺寸為 15x15
的核)作為函數(shù)的參數(shù)。如下所示,首先創(chuàng)建了一個 1D
高斯核,然后計算兩個 1D
高斯核的外積返回 2D
核:
import numpy as np import numpy.fft as fp from skimage.io import imread from skimage.color import rgb2gray import matplotlib.pyplot as plt import cv2 def get_gaussian_edge_blur_kernel(sigma, sz=15): # First create a 1-D Gaussian kernel x = np.linspace(-10, 10, sz) kernel_1d = np.exp(-x**2/sigma**2) kernel_1d /= np.trapz(kernel_1d) # normalize the sum to 1.0 # create a 2-D Gaussian kernel from the 1-D kernel kernel_2d = kernel_1d[:, np.newaxis] * kernel_1d[np.newaxis, :] return kernel_2d
(2) 接下來,定義函數(shù) get_motion_blur_kernel()
以生成運動模糊核,得到給定長度且特定方向(角度)的線作為卷積核,以模擬輸入圖像的運動模糊效果:
def get_motion_blur_kernel(ln, angle, sz=15): kern = np.ones((1, ln), np.float32) angle = -np.pi*angle/180 c, s = np.cos(angle), np.sin(angle) A = np.float32([[c, -s, 0], [s, c, 0]]) sz2 = sz // 2 A[:,2] = (sz2, sz2) - np.dot(A[:,:2], ((ln-1)*0.5, 0)) kern = cv2.warpAffine(kern, A, (sz, sz), flags=cv2.INTER_CUBIC) return kern
函數(shù) get_motion_blur_kernel()
將模糊的長度和角度以及模糊核的尺寸作為參數(shù),函數(shù)使用 OpenCV
的 warpaffine()
函數(shù)返回核矩陣(以矩陣中心為中點,使用給定長度和給定角度得到核)。
(3) 最后,定義函數(shù) get_out_of_focus_kernel()
以生成失焦核(模擬圖像失焦模糊),其根據(jù)給定半徑創(chuàng)建圓用作卷積核,該函數(shù)接受半徑 R
(Deocus Radius
) 和要生成的核大小作為輸入?yún)?shù):
def get_out_of_focus_kernel(r, sz=15): kern = np.zeros((sz, sz), np.uint8) cv2.circle(kern, (sz, sz), r, 255, -1, cv2.LINE_AA, shift=1) kern = np.float32(kern) / 255 return kern
(4) 接下來,實現(xiàn)函數(shù) dft_convolve()
,該函數(shù)使用圖像的逐元素乘法和頻域中的卷積核執(zhí)行頻域卷積(基于卷積定理)。該函數(shù)還繪制輸入圖像、核和卷積計算后得到的輸出圖像:
def dft_convolve(im, kernel): F_im = fp.fft2(im) #F_kernel = fp.fft2(kernel, s=im.shape) F_kernel = fp.fft2(fp.ifftshift(kernel), s=im.shape) F_filtered = F_im * F_kernel im_filtered = fp.ifft2(F_filtered) cmap = 'RdBu' plt.figure(figsize=(20,10)) plt.gray() plt.subplot(131), plt.imshow(im), plt.axis('off'), plt.title('input image', size=10) plt.subplot(132), plt.imshow(kernel, cmap=cmap), plt.title('kernel', size=10) plt.subplot(133), plt.imshow(im_filtered.real), plt.axis('off'), plt.title('output image', size=10) plt.tight_layout() plt.show()
(5) 將 get_gaussian_edge_blur_kernel()
核函數(shù)應用于圖像,并繪制輸入,核和輸出模糊圖像:
im = rgb2gray(imread('3.jpg')) kernel = get_gaussian_edge_blur_kernel(25, 25) dft_convolve(im, kernel)
(6) 接下來,將 get_motion_blur_kernel()
函數(shù)應用于圖像,并繪制輸入,核和輸出模糊圖像:
kernel = get_motion_blur_kernel(30, 60, 25) dft_convolve(im, kernel)
(7) 最后,將 get_out_of_focus_kernel()
函數(shù)應用于圖像,并繪制輸入,核和輸出模糊圖像:
kernel = get_out_of_focus_kernel(15, 20) dft_convolve(im, kernel)
2. 使用 scipy.ndimage 濾波器模糊圖像
scipy.ndimage
模塊提供了一系列可以在頻域中對圖像應用低通濾波器的函數(shù)。本節(jié)中,我們通過幾個示例學習其中一些濾波器的用法。
2.1 使用 fourier_gaussian() 函數(shù)
使用 scipy.ndimage
庫中的 fourier_gaussian()
函數(shù)在頻域中使用高斯核執(zhí)行卷積操作。
(1) 首先,讀取輸入圖像,并將其轉換為灰度圖像,并通過使用 FFT
獲取其頻域表示:
import numpy as np import numpy.fft as fp from skimage.io import imread import matplotlib.pyplot as plt from scipy import ndimage im = imread('1.png', as_gray=True) freq = fp.fft2(im)
(2) 接下來,使用 fourier_gaussian()
函數(shù)對圖像執(zhí)行模糊操作,使用兩個具有不同標準差的高斯核,繪制輸入、輸出圖像以及功率譜:
fig, axes = plt.subplots(2, 3, figsize=(20,15)) plt.subplots_adjust(0,0,1,0.95,0.05,0.05) plt.gray() # show the filtered result in grayscale axes[0, 0].imshow(im), axes[0, 0].set_title('Original Image', size=10) axes[1, 0].imshow((20*np.log10( 0.1 + fp.fftshift(freq))).real.astype(int)), axes[1, 0].set_title('Original Image Spectrum', size=10) i = 1 for sigma in [3,5]: convolved_freq = ndimage.fourier_gaussian(freq, sigma=sigma) convolved = fp.ifft2(convolved_freq).real # the imaginary part is an artifact axes[0, i].imshow(convolved) axes[0, i].set_title(r'Output with FFT Gaussian Blur, $\sigma$={}'.format(sigma), size=10) axes[1, i].imshow((20*np.log10( 0.1 + fp.fftshift(convolved_freq))).real.astype(int)) axes[1, i].set_title(r'Spectrum with FFT Gaussian Blur, $\sigma$={}'.format(sigma), size=10) i += 1 for a in axes.ravel(): a.axis('off') plt.show()
從上圖中可以看出,高斯核的標準差 σ σ σ 值越大,輸出圖像就會越模糊。
2.2 使用 fourier_uniform() 函數(shù)
scipy.ndimage
模塊的函數(shù) fourier_uniform()
實現(xiàn)了多維均勻傅立葉濾波器。頻率陣列與給定尺寸的方形核的傅立葉變換相乘。接下來,我們學習如何使用 LPF
(均值濾波器)模糊輸入灰度圖像。
(1) 首先,讀取輸入圖像并使用 DFT
獲取其頻域表示:
im = imread('1.png', as_gray=True) freq = fp.fft2(im)
(2) 然后,使用函數(shù) fourier_uniform()
應用 10x10
方形核(由功率譜上的參數(shù)指定),以獲取平滑輸出:
freq_uniform = ndimage.fourier_uniform(freq, size=10)
(3) 繪制原始輸入圖像和模糊后的圖像:
fig, (axes1, axes2) = plt.subplots(1, 2, figsize=(20,10)) plt.gray() # show the result in grayscale im1 = fp.ifft2(freq_uniform) axes1.imshow(im), axes1.axis('off') axes1.set_title('Original Image', size=10) axes2.imshow(im1.real) # the imaginary part is an artifact axes2.axis('off') axes2.set_title('Blurred Image with Fourier Uniform', size=10) plt.tight_layout() plt.show()
(4) 最后,繪制顯示方形核的功率譜:
plt.figure(figsize=(10,10)) plt.imshow( (20*np.log10( 0.1 + fp.fftshift(freq_uniform))).real.astype(int)) plt.title('Frequency Spectrum with fourier uniform', size=10) plt.show()
2.3 使用 fourier_ellipsoid() 函數(shù)
與上一小節(jié)類似,通過將方形核修改為橢圓形核,我們可以使用橢圓形核生成模糊的輸出圖像。
(1) 類似的,我們首先在圖像的功率譜上應用函數(shù) fourier_ellipsoid()
,并使用 IDFT
在空間域中獲得模糊后的輸出圖像:
freq_ellipsoid = ndimage.fourier_ellipsoid(freq, size=10) im1 = fp.ifft2(freq_ellipsoid)
(2) 接下來,繪制原始輸入圖像和模糊后的圖像:
fig, (axes1, axes2) = plt.subplots(1, 2, figsize=(20,10)) axes1.imshow(im), axes1.axis('off') axes1.set_title('Original Image', size=10) axes2.imshow(im1.real) # the imaginary part is an artifact axes2.axis('off') axes2.set_title('Blurred Image with Fourier Ellipsoid', size=10) plt.tight_layout() plt.show()
(3) 最后,顯示應用橢圓形核后圖像的頻譜:
plt.figure(figsize=(10,10)) plt.imshow( (20*np.log10( 0.1 + fp.fftshift(freq_ellipsoid))).real.astype(int)) plt.title('Frequency Spectrum with Fourier ellipsoid', size=10) plt.show()
3. 使用 scipy.fftpack 實現(xiàn)高斯模糊
我們已經(jīng)學習了如何在實際應用中使用 numpy.fft
模塊的 2D-FFT
。在本節(jié)中,我們將介紹 scipy.fftpack
模塊的 fft2()
函數(shù)用于實現(xiàn)高斯模糊。
(1) 使用灰度圖像作為輸入,并使用 FFT
從圖像中創(chuàng)建 2D
頻率響應數(shù)組:
import numpy as np import numpy.fft as fp from skimage.color import rgb2gray from skimage.io import imread import matplotlib.pyplot as plt from scipy import signal from matplotlib.ticker import LinearLocator, FormatStrFormatter im = rgb2gray(imread('1.png')) freq = fp.fft2(im)
(2) 通過計算兩個 1D
高斯核的外積,在空間域中創(chuàng)建高斯 2D
核用作 LPF
:
kernel = np.outer(signal.gaussian(im.shape[0], 1), signal.gaussian(im.shape[1], 1))
(3) 使用 DFT
獲得高斯核的頻率響應:
freq_kernel = fp.fft2(fp.ifftshift(kernel))
(4) 使用卷積定理通過逐元素乘法在頻域中將 LPF
與輸入圖像卷積:
convolved = freq*freq_kernel # by the Convolution theorem
(5) 使用 IFFT
獲得輸出圖像,需要注意的是,要正確顯示輸出圖像,需要縮放輸出圖像:
im_blur = fp.ifft2(convolved).real im_blur = 255 * im_blur / np.max(im_blur)
(6) 繪制圖像、高斯核和在頻域中卷積后獲得圖像的功率譜,可以使用 matplotlib.colormap
繪制色,以了解不同坐標下的頻率響應值:
plt.figure(figsize=(20,20)) plt.subplot(221), plt.imshow(kernel, cmap='coolwarm'), plt.colorbar() plt.title('Gaussian Blur Kernel', size=10) plt.subplot(222) plt.imshow( (20*np.log10( 0.01 + fp.fftshift(freq_kernel))).real.astype(int), cmap='inferno') plt.colorbar() plt.title('Gaussian Blur Kernel (Freq. Spec.)', size=10) plt.subplot(223), plt.imshow(im, cmap='gray'), plt.axis('off'), plt.title('Input Image', size=10) plt.subplot(224), plt.imshow(im_blur, cmap='gray'), plt.axis('off'), plt.title('Output Blurred Image', size=10) plt.tight_layout() plt.show()
(7) 要繪制輸入/輸出圖像和 3D
核的功率譜,我們定義函數(shù) plot_3d()
,使用 mpl_toolkits.mplot3d
模塊的 plot_surface()
函數(shù)獲取 3D
功率譜圖,給定相應的 Y
和Z值作為 2D
陣列傳遞:
def plot_3d(X, Y, Z, cmap=plt.cm.seismic): fig = plt.figure(figsize=(20,20)) ax = fig.gca(projection='3d') # Plot the surface. surf = ax.plot_surface(X, Y, Z, cmap=cmap, linewidth=5, antialiased=False) #ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) #ax.set_zscale("log", nonposx='clip') #ax.zaxis.set_scale('log') ax.zaxis.set_major_locator(LinearLocator(10)) ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f')) ax.set_xlabel('F1', size=15) ax.set_ylabel('F2', size=15) ax.set_zlabel('Freq Response', size=15) #ax.set_zlim((-40,10)) # Add a color bar which maps values to colors. fig.colorbar(surf) #, shrink=0.15, aspect=10) #plt.title('Frequency Response of the Gaussian Kernel') plt.show()
(8) 在 3D
空間中繪制高斯核的頻率響應,并使用 plot_3d()
函數(shù):
Y = np.arange(freq.shape[0]) #-freq.shape[0]//2,freq.shape[0]-freq.shape[0]//2) X = np.arange(freq.shape[1]) #-freq.shape[1]//2,freq.shape[1]-freq.shape[1]//2) X, Y = np.meshgrid(X, Y) Z = (20*np.log10( 0.01 + fp.fftshift(freq_kernel))).real plot_3d(X,Y,Z)
下圖顯示了 3D
空間中高斯 LPF
核的功率譜:
(9) 繪制 3D
空間中輸入圖像的功率譜:
Z = (20*np.log10( 0.01 + fp.fftshift(freq))).real plot_3d(X,Y,Z)
(10) 最后,繪制輸出圖像的功率譜(通過將高斯核與輸入圖像卷積獲得):
Z = (20*np.log10( 0.01 + fp.fftshift(convolved))).real plot_3d(X,Y,Z)
從輸出圖像的頻率響應中可以看出,高頻組件被衰減,從而導致細節(jié)的平滑/丟失,并導致輸出圖像模糊。
4. 彩色圖像頻域卷積
在本節(jié)中,我們將學習使用 scipy.signal
模塊的 fftconvolve()
函數(shù),用于與 RGB
彩色輸入圖像進行頻域卷積,從而生成 RGB
彩色模糊輸出圖像:
scipy.signal.fftconvolve(in1, in2, mode='full', axes=None)
函數(shù)使用 FFT
卷積兩個 n
維數(shù)組 in1
和 in2
,并由 mode
參數(shù)確定輸出大小。卷積模式 mode
具有以下類型:
- 輸出是輸入的完全離散線性卷積,默認情況下使用此種卷積模式
- 輸出僅由那些不依賴零填充的元素組成,
in1
或in2
的尺寸必須相同 - 輸出的大小與
in1
相同,并以輸出為中心
4.1 基于 scipy.signal 模塊的彩色圖像頻域卷積
接下來,我們實現(xiàn)高斯低通濾波器并使用 Laplacian
高通濾波器執(zhí)行相應操作。
(1) 首先,導入所需的包,并讀取輸入 RGB
圖像:
from skimage import img_as_float from scipy import signal import numpy as np import matplotlib.pyplot as plt im = img_as_float(plt.imread('1.png'))
(2) 實現(xiàn)函數(shù) get_gaussian_edge_kernel()
,并根據(jù)此函數(shù)創(chuàng)建一個尺寸為 15x15
的高斯核:
def get_gaussian_edge_blur_kernel(sigma, sz=15): # First create a 1-D Gaussian kernel x = np.linspace(-10, 10, sz) kernel_1d = np.exp(-x**2/sigma**2) kernel_1d /= np.trapz(kernel_1d) # normalize the sum to 1.0 # create a 2-D Gaussian kernel from the 1-D kernel kernel_2d = kernel_1d[:, np.newaxis] * kernel_1d[np.newaxis, :] return kernel_2d kernel = get_gaussian_edge_blur_kernel(sigma=10, sz=15)
(3) 然后,使用 np.newaxis
將核尺寸重塑為 15x15x1
,并使用 same
模式調用函數(shù) signal.fftconvolve()
:
im1 = signal.fftconvolve(im, kernel[:, :, np.newaxis], mode='same') im1 = im1 / np.max(im1)
在以上代碼中使用的 mode='same'
,可以強制輸出形狀與輸入陣列形狀相同,以避免邊框效應。
(4) 接下來,使用 laplacian HPF
內(nèi)核,并使用相同函數(shù)執(zhí)行頻域卷積。需要注意的是,我們可能需要縮放/裁剪輸出圖像以使輸出值保持像素的浮點值范圍 [0,1]
內(nèi):
kernel = np.array([[0,-1,0],[-1,4,-1],[0,-1,0]]) im2 = signal.fftconvolve(im, kernel[:, :, np.newaxis], mode='same') im2 = im2 / np.max(im2) im2 = np.clip(im2, 0, 1)
(5) 最后,繪制輸入圖像和使用卷積創(chuàng)建的輸出圖像。
plt.figure(figsize=(20,10)) plt.subplot(131), plt.imshow(im), plt.axis('off'), plt.title('original image', size=10) plt.subplot(132), plt.imshow(im1), plt.axis('off'), plt.title('output with Gaussian LPF', size=10) plt.subplot(133), plt.imshow(im2), plt.axis('off'), plt.title('output with Laplacian HPF', size=10) plt.tight_layout() plt.show()
如上圖所示,高斯 LPF
可以用于模糊圖像,而拉普拉斯 HPF
則可以用于提取圖像中的細節(jié)/邊緣(這些部分對應于圖像中的高頻分量)。
小結
在圖像上應用低通濾波器會刪除圖像中的細節(jié)/邊緣和噪聲/離群值,稱為圖像模糊(或平滑),圖像平滑可以作為復雜圖像處理任務的預處理部分。在本節(jié)中,我們介紹了多種不同類型的濾波器核與卷積操作,使用 scipy.ndimage
模塊中的濾波器模糊圖像,利用 scipy.fftpack
模塊的 fft2()
函數(shù)實現(xiàn)高斯模糊,還介紹了基于 scipy.signal 模塊的彩色圖像頻域卷積。
到此這篇關于Python使用低通濾波器模糊圖像的文章就介紹到這了,更多相關Python低通濾波器模糊圖像內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python使用 Beanstalkd 做異步任務處理的方法
這篇文章主要介紹了Python使用 Beanstalkd 做異步任務處理的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04python在CMD界面讀取excel所有數(shù)據(jù)的示例
這篇文章主要介紹了python在CMD界面讀取excel所有數(shù)據(jù),幫助大家更好的利用python辦公,感興趣的朋友可以了解下2020-09-09selenium與xpath之獲取指定位置的元素的實現(xiàn)
這篇文章主要介紹了selenium與xpath之獲取指定位置的元素的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-01-01Python使用Srapy框架爬蟲模擬登陸并抓取知乎內(nèi)容
這里我們來看如何通過Python使用Srapy框架爬蟲模擬登陸并抓取知乎內(nèi)容的實例,要實現(xiàn)持續(xù)的爬取需要利用到cookie的保存,我們首先還是來回顧一下cookie的相關知識點:2016-07-07pycharm 實現(xiàn)本地寫代碼,服務器運行的操作
這篇文章主要介紹了pycharm 實現(xiàn)本地寫代碼,服務器運行的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06