SMOOTHING (LOWPASS) SPATIAL FILTERS

Gonzalez R. C. and Woods R. E. Digital Image Processing (Forth Edition).

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

FILTERS

filters实际上就是通过一些特殊的kernel w ww 对图片进行如下操作:
g ( x , y ) = ∑ s = − a a ∑ t = − b b w ( s , t ) f ( x + s , y + t ) , x = 1 , 2 , ⋯ , M , y = 1 , 2 , ⋯ N . g(x, y) = \sum_{s=-a}^a \sum_{t=-b}^b w(s, t) f(x+s, y+t), \: x = 1,2,\cdots, M, \: y = 1, 2,\cdots N.g(x,y)=s=aat=bbw(s,t)f(x+s,y+t),x=1,2,,M,y=1,2,N.
其中w ( s , t ) ∈ R m × n , m = 2 a + 1 , n = 2 b + 1 w(s, t) \in \mathbb{R}^{m \times n}, m=2a+1, n = 2b+1w(s,t)Rm×n,m=2a+1,n=2b+1.

注: 一般来说kernel的边是奇数, 这样可以确定唯一的中心, 但是偶数其实也是可以的.

实际上, 上面可以转换成卷积的形式:
( w ∗ f ) ( x , y ) = ∑ s = − a a ∑ t = − b b w ′ ( s , t ) f ( x − s , y − t ) , x = 1 , 2 , ⋯ , M , y = 1 , 2 , ⋯ N . (w * f) (x, y) = \sum_{s=-a}^a \sum_{t=-b}^b w'(s, t) f(x-s, y-t), \: x = 1,2,\cdots, M, \: y = 1, 2,\cdots N.(wf)(x,y)=s=aat=bbw(s,t)f(xs,yt),x=1,2,,M,y=1,2,N.
只是w ′ ( s , t ) = w ( − s , − t ) w'(s, t) = w(-s, -t)w(s,t)=w(s,t), 不过下面我们仅考虑卷积操作, 故直接定义为:
( w ∗ f ) ( x , y ) = ∑ s = − a a ∑ t = − b b w ( s , t ) f ( x − s , y − t ) , x = 1 , 2 , ⋯ , M , y = 1 , 2 , ⋯ N . (w * f) (x, y) = \sum_{s=-a}^a \sum_{t=-b}^b w(s, t) f(x-s, y-t), \: x = 1,2,\cdots, M, \: y = 1, 2,\cdots N.(wf)(x,y)=s=aat=bbw(s,t)f(xs,yt),x=1,2,,M,y=1,2,N.
即可.

注: 注意到上面会出现f ( − 1 , − 1 ) f(-1, -1)f(1,1)之类的未定义情况, 常见的处理方式是在图片周围加padding(分别为pad a, b), 比如补0或者镜像补.

用卷积的目的是其特别的性质:

  1. f ∗ g = g ∗ f f * g = g * ffg=gf;
  2. f ∗ ( g ∗ h ) = ( f ∗ g ) ∗ h f * (g * h) = (f * g) * hf(gh)=(fg)h;
  3. f ∗ ( g + h ) = ( f ∗ g ) + ( g ∗ h ) f * (g + h) = (f * g) + (g * h)f(g+h)=(fg)+(gh).

注: f , g , h f, g, hf,g,h应当形状一致.

特别的, 如果
w = u v T , w = uv^T,w=uvT,

w ∗ f = u ′ ∗ ( v T ∗ f ) , u ′ ( x ) = u ( − x ) . w * f = u' * (v^T * f), \quad u'(x) = u(-x).wf=u(vTf),u(x)=u(x).
可以显著降低计算量.

Box Filter Kernels


w i j = 1 m n , i = 1 , 2 , ⋯ , m , j = 1 , 2 , ⋯ , n . w_{ij} = \frac{1}{mn}, \quad i=1,2,\cdots, m, \: j=1,2,\cdots, n.wij=mn1,i=1,2,,m,j=1,2,,n.

img = cv2.imread("./pics/alphabeta.png")
img.shape
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 由于是截图, 先转成灰度图
plt.imshow(img, cmap='gray')

image-20210614143243637

# 或者等价地用 cv2.blur(img, (m, n))
kernels = [np.ones((i, i)) / (i * i) for i in [3, 11, 21]]
imgs_smoothed = [cv2.filter2D(img, -1, kernel) for kernel in kernels]
fig, axes = plt.subplots(2, 2)
axes[0, 0].imshow(img, cmap='gray')
axes[0, 0].set_title("raw")
axes[0, 1].imshow(imgs_smoothed[0], cmap="gray")
axes[0, 1].set_title("3x3")
axes[1, 0].imshow(imgs_smoothed[1], cmap="gray")
axes[1, 0].set_title("11x11")
axes[1, 1].imshow(imgs_smoothed[2], cmap="gray")
axes[1, 1].set_title("21x21")
plt.tight_layout()
plt.show()

image-20210614143312683

Lowpass Gaussian Filter Kernels


w ( s , t ) = G ( s , t ) = K e − s 2 + t 2 2 σ 2 , w(s, t) = G(s, t) = K e^{-\frac{s^2+t^2}{2\sigma^2}},w(s,t)=G(s,t)=Ke2σ2s2+t2,
高斯分布的特点是绝大部分集中于( − 3 σ , + 3 σ ) (-3\sigma, +3\sigma)(3σ,+3σ)之间, 故一般w ww的大小选择为( − 6 σ , + 6 σ ) (-6\sigma, +6\sigma)(6σ,+6σ), 需要注意的是, σ \sigmaσ的选择和图片的大小息息相关.

imgs_smoothed = [cv2.GaussianBlur(img, ksize=ksize, sigmaX=sigma) for (ksize, sigma) in [((5, 5), 1), ((21, 21), 3.5), ((43, 43), 7)]]
fig, axes = plt.subplots(1, 4)
axes[0].imshow(img, cmap='gray')
axes[0].set_title("raw")
axes[1].imshow(imgs_smoothed[0], cmap="gray")
axes[1].set_title("5x5, 1")
axes[2].imshow(imgs_smoothed[1], cmap="gray")
axes[2].set_title("21x21, 3.5")
axes[3].imshow(imgs_smoothed[2], cmap="gray")
axes[3].set_title("43x43, 7")
plt.tight_layout()
plt.show()

image-20210614145105794

Order-Statistic (Nonlinear) Filters

g ( x , y ) g(x, y)g(x,y)( x , y ) (x, y)(x,y)周围的点的一个某个顺序的值代替, 比如median.

imgs_smoothed = [cv2.medianBlur(img, ksize=ksize) for ksize in [3, 7, 15]]
fig, axes = plt.subplots(1, 4)
axes[0].imshow(img, cmap='gray')
axes[0].set_title("raw")
axes[1].imshow(imgs_smoothed[0], cmap="gray")
axes[1].set_title("3x3")
axes[2].imshow(imgs_smoothed[1], cmap="gray")
axes[2].set_title("7x7")
axes[3].imshow(imgs_smoothed[2], cmap="gray")
axes[3].set_title("15x15")
plt.tight_layout()
plt.show()

image-20210614145546864


版权声明:本文为MTandHJ原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。