# GaussianBlur 高斯模糊 / 高斯滤波

最近需要做图像增强,用于去模糊方法中,因为原本的图像数据集就带有模糊,而避免与别人重复并且增强自己的方法,需要在原有的数据集上进行二次加模糊处理。

# 原理

首先,高斯滤波是对滤波器进行加权平均(能量 = 1),其权重分配按照距离中心点的距离决定,离中心越近,权值越大,即高斯分布(正态分布),公式如下(一维),

f(x)=12πσexp((xμ)22σ2)f(x)=\frac{1}{\sqrt{2 \pi} \sigma} \exp \left(-\frac{(x - \mu)^{2}}{2 \sigma^{2}}\right)

其中,x-μ 看作是距离,因为是到中心像素的距离,因此,μ=0.

因此,可以改变的量值只有 sigma,二维情况对应 sigma_x 和 sigma_y,这是对于分布而言。对于滤波器而言,kernel_size 也是可以改变的量。

# 实现

这里可以自己去定义,并且实现高斯滤波,以及卷积处理的过程(以前我是这么干的)。

现在,可以通过一些专门的数据增强库进行直接调用实现,例如,opencv 里的 cv2.GaussianBlur,或者 Albumentations 库里的 albumentations.GaussianBlur 等。方法有很多,万变不离其宗。

这里我采用的是后一者库,

1
2
3
4
5
transform = albu.Compose([
albu.GaussianBlur(blur_limit=21, p=1)
])

trans_img = transform(image=img)['image']

包括其参数含义:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 高斯模糊

"""
Blur the input image using a Gaussian filter with a random kernel size.
Args:
blur_limit (int, (int, int)): maximum Gaussian kernel size for blurring the input image.
Must be zero or odd and in range [0, inf). If set to 0 it will be computed from sigma
as `round(sigma * (3 if img.dtype == np.uint8 else 4) * 2 + 1) + 1`.
If set single value `blur_limit` will be in range (0, blur_limit).
Default: (3, 7).
sigma_limit (float, (float, float)): Gaussian kernel standard deviation. Must be greater in range [0, inf).
If set single value `sigma_limit` will be in range (0, sigma_limit).
If set to 0 sigma will be computed as `sigma = 0.3*((ksize-1)*0.5 - 1) + 0.8`. Default: 0.
p (float): probability of applying the transform. Default: 0.5.
"""

"""
blur_limit: 选取kernel_size范围,如果设置为0,则会根据公式计算 `round(sigma * (3 if img.dtype == np.uint8 else 4) * 2 + 1) + 1`
默认是(3, 7)
sigma_limit:选取高斯核的均方差,如果设置为0,则会根据公式计算 `sigma = 0.3*((ksize-1)*0.5 - 1) + 0.8`
并且sigma跟kernel_size一样,都是范围性随机的

"""

# 结果

这里以训练集中的真实标签裁剪 256x256 尺度为例,变换采用上面代码部分,

image-20211209151219468

Edited on

Give me a cup of [coffee]~( ̄▽ ̄)~*

kwshh WeChat Pay

WeChat Pay

kwshh Alipay

Alipay

kwshh PayPal

PayPal