一个高斯函数的应用

最近测试一个项目场景,需要模糊视频,由于高斯模糊已经很常见了,所以我们就不再自己实现了。网上也有各种实现。

一 ffmpeg里面的高斯模糊实现

ffmpeg里面的锐化滤镜使用的就是一种快速的高斯模糊实现。

有兴趣的同学可以去看看论文。

二 视频编码的时候具体使用

 -vf "unsharp=luma_msize_x=7:luma_msize_y=7:luma_amount=-0.7"
ffmpeg编码选项加速这个滤镜参数就可以了。亮度锐化值为负的时候,表示模糊。
三 模糊效果
左边是模糊的效果

四 实现源码
 static void apply_unsharp(      uint8_t *dst, int dst_stride,
                          const uint8_t *src, int src_stride,
                          int width, int height, UnsharpFilterParam *fp)
{
    uint32_t **sc = fp->sc;
    uint32_t sr[MAX_MATRIX_SIZE - 1], tmp1, tmp2;

    int32_t res;
    int x, y, z;
    const uint8_t *src2 = NULL;  //silence a warning
    const int amount = fp->amount;
    const int steps_x = fp->steps_x;
    const int steps_y = fp->steps_y;
    const int scalebits = fp->scalebits;
    const int32_t halfscale = fp->halfscale;

    if (!amount) {
        av_image_copy_plane(dst, dst_stride, src, src_stride, width, height);
        return;
    }

    for (y = 0; y < 2 * steps_y; y++)
        memset(sc[y], 0, sizeof(sc[y][0]) * (width + 2 * steps_x));

    for (y = -steps_y; y < height + steps_y; y++) {
        if (y < height)
            src2 = src;

        memset(sr, 0, sizeof(sr[0]) * (2 * steps_x - 1));
        for (x = -steps_x; x < width + steps_x; x++) {
            tmp1 = x <= 0 ? src2[0] : x >= width ? src2[width-1] : src2[x];
            for (z = 0; z < steps_x * 2; z += 2) {
                tmp2 = sr[z + 0] + tmp1; sr[z + 0] = tmp1;
                tmp1 = sr[z + 1] + tmp2; sr[z + 1] = tmp2;
            }
            for (z = 0; z < steps_y * 2; z += 2) {
                tmp2 = sc[z + 0][x + steps_x] + tmp1; sc[z + 0][x + steps_x] = tmp1;
                tmp1 = sc[z + 1][x + steps_x] + tmp2; sc[z + 1][x + steps_x] = tmp2;
            }
            if (x >= steps_x && y >= steps_y) {
                const uint8_t *srx = src - steps_y * src_stride + x - steps_x;
                uint8_t *dsx       = dst - steps_y * dst_stride + x - steps_x;

                res = (int32_t)*srx + ((((int32_t) * srx - (int32_t)((tmp1 + halfscale) >> scalebits)) * amount) >> 16);
                *dsx = av_clip_uint8(res);
            }
        }
        if (y >= 0) {
            dst += dst_stride;
            src += src_stride;
        }
    }
}

声明:上面代码我也没怎么读懂,有兴趣的可以先去研究一下论文,再看这个代码。

### 一维双高斯函数的定义 一维双高斯函数通常是由两个高斯函数组合而成。一个标准的一维高斯函数形式为 \(G(x)=\frac{1}{\sqrt{2\pi}\sigma}e^{-\frac{(x - \mu)^2}{2\sigma^2}}\),其中 \(\mu\) 是均值,\(\sigma\) 是标准差。一维双高斯函数可以表示为两个高斯函数的线性组合:\(DG(x)=A_1\frac{1}{\sqrt{2\pi}\sigma_1}e^{-\frac{(x - \mu_1)^2}{2\sigma_1^2}}+A_2\frac{1}{\sqrt{2\pi}\sigma_2}e^{-\frac{(x - \mu_2)^2}{2\sigma_2^2}}\),其中 \(A_1\) 和 \(A_2\) 分别是两个高斯函数的幅值,\(\mu_1\)、\(\mu_2\) 是均值,\(\sigma_1\)、\(\sigma_2\) 是标准差。 ### 一维双高斯函数的性质 - **对称性**:如果 \(\mu_1 = \mu_2\),那么函数关于 \(x = \mu_1\)(也就是 \(x = \mu_2\))对称。一般情况下,函数可能不具有简单的单一对称轴,但它是由两个具有对称性的高斯函数组成。 - **可积性**:由于高斯函数在整个实数域上是可积的,一维双高斯函数同样在整个实数域上可积,\(\int_{-\infty}^{\infty}DG(x)dx=A_1 + A_2\)。 - **平滑性**:高斯函数具有无限阶导数,一维双高斯函数作为高斯函数的线性组合,也具有良好的平滑性,这意味着它在图像等领域应用时可以起到平滑的作用。 ### 一维双高斯函数应用 - **图像处理**:在图像的边缘检测中,一维双高斯函数可以用于增强图像的边缘信息。通过调整两个高斯函数的参数,可以突出不同尺度的边缘特征。例如,一个高斯函数用于平滑图像,另一个用于增强边缘,二者结合可以更好地提取图像的边缘。 - **信号处理**:在信号滤波中,一维双高斯函数可以设计成滤波器,去除信号中的噪声,同时保留信号的重要特征。不同的参数设置可以适应不同类型的信号和噪声。 - **数据拟合**:当数据呈现出两个高斯分布叠加的特征时,一维双高斯函数可以用于拟合这些数据,从而分析数据的分布特征,例如在分析一些混合信号或者生物医学数据时会用到。 ```python import numpy as np import matplotlib.pyplot as plt def one_dimensional_double_gaussian(x, A1, mu1, sigma1, A2, mu2, sigma2): gaussian1 = A1 * (1 / (np.sqrt(2 * np.pi) * sigma1)) * np.exp(-((x - mu1) ** 2) / (2 * sigma1 ** 2)) gaussian2 = A2 * (1 / (np.sqrt(2 * np.pi) * sigma2)) * np.exp(-((x - mu2) ** 2) / (2 * sigma2 ** 2)) return gaussian1 + gaussian2 x = np.linspace(-10, 10, 1000) y = one_dimensional_double_gaussian(x, 1, -2, 1, 2, 2, 0.5) plt.plot(x, y) plt.title('One - Dimensional Double Gaussian Function') plt.xlabel('x') plt.ylabel('y') plt.show() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值