高斯核与高斯核的卷积的结果还是一个高斯核的推导

高斯核卷积高斯核

之前看sift源码中有个概念,就是对一张图像 I 0 I_0 I0,先进行标准差为 σ 1 \sigma_1 σ1的高斯核卷积,得到 I 1 I_1 I1,再基于 I 1 I_1 I1进行标准差为 σ 2 \sigma_2 σ2的高斯核卷积得到图像 I 2 I_2 I2,和 I 0 I_0 I0直接进行一次标准差为 σ \sigma σ的高斯核的卷积得到 I 2 I_2 I2之间的关系。写成表达式就是 I 0 ∗ g 1 ∗ g 2 I_0*g1*g2 I0g1g2是否等于 I 0 ∗ ( g 1 ∗ g 2 ) I_0*(g1*g2) I0(g1g2),如果成立 g = g 1 ∗ g 2 g=g1*g2 g=g1g2是个什么东东。结论是等号成立的,并且 g g g也是一个标准差为 σ \sigma σ的高斯分布的概率密度函数,且 σ 2 = σ 1 2 + σ 2 2 \sigma^2=\sigma_1^2 + \sigma_2^2 σ2=σ12+σ22。结合律留给有兴趣的读者,下面证明方差之间的关系。

证明

g 1 , g 2 g_1,g_2 g1,g2是两个1维正态分布的概率密度函数:
g 1 ( x ) = 1 2 π σ 1 e − ( x − μ 1 ) 2 2 σ 1 2 g_1(x)=\frac{1}{\sqrt{2\pi}\sigma_1}{e}^{-\frac{(x-\mu_1)^2}{2\sigma_1^2}} g1(x)=2π σ11e2σ12(xμ1)2
g 2 ( x ) = 1 2 π σ 2 e − ( x − μ 2 ) 2 2 σ 2 2 g_2(x)=\frac{1}{\sqrt{2\pi}\sigma_2}e^{-\frac{(x-\mu_2)^2}{2\sigma_2^2}} g2(x)=2π σ21e2σ22(xμ2)2

在无穷区间上的积分,被积函数的自变量发生平移并不影响积分结果,为了简化计算,不妨设 μ 1 = μ 2 = 0 \mu_1=\mu_2=0 μ1=μ2=0.
从而,

f ( t ) = g 1 ( x ) ∗ g 2 ( x ) f(t)=g_1(x)*g_2(x) f(t)=g1(x)g2(x)
= ∫ − ∞ ∞ g 1 ( x ) g 2 ( t − x ) d x =\displaystyle\int_{-\infty}^{\infty}g_1(x)g_2(t-x)dx =g1(x)g2(tx)dx
= 1 2 π σ 1 σ 2 ∫ − ∞ ∞ e − x 2 2 σ 1 2 − − ( t − x ) 2 2 σ 2 2 d x =\displaystyle{\frac{1}{2\pi \sigma_1\sigma_2}\int_{-\infty}^{\infty}e^{\frac{-x^2}{2\sigma_1^2}-\frac{-(t-x)^2}{2\sigma_2^2}}}dx =2πσ1σ21e2σ12x22σ22(tx)2dx

下面对积分部分进行计算
∫ − ∞ ∞ e − x 2 2 σ 1 2 − − ( t − x ) 2 2 σ 2 2 d x \displaystyle\int_{-\infty}^{\infty}e^{\frac{-x^2}{2\sigma_1^2}-\frac{-(t-x)^2}{2\sigma_2^2}}dx e2σ12x22σ22(tx)2dx
= ∫ − ∞ ∞ e − x 2 σ 2 2 − σ 1 2 ( t − x ) 2 2 σ 1 2 σ 2 2 d x =\displaystyle\int_{-\infty}^{\infty}e^{\frac{-x^2\sigma_2^2-\sigma_1^2(t-x)^2}{2\sigma_1^2\sigma_2^2}}dx =e2σ12σ22x2σ22σ12(tx)2dx

= ∫ − ∞ ∞ e x 2 σ 2 2 + σ 1 2 t 2 + σ 1 2 x 2 − 2 σ 1 2 t x − 2 σ 1 2 σ 2 2 d x =\displaystyle\int_{-\infty}^{\infty}e^{\frac{x^2\sigma_2^2+\sigma_1^2t^2+\sigma_1^2x^2-2\sigma_1^2tx}{-2\sigma_1^2\sigma_2^2}}dx =e2σ12σ22x2σ22+σ12t2+σ12x22σ12txdx

= ∫ − ∞ ∞ e ( σ 1 2 + σ 2 2 ) ( x − σ 1 2 t σ 1 2 + σ 2 2 ) 2 − σ 1 4 t 2 ( σ 1 2 + σ 2 2 ) 2 ⋅ ( σ 1 2 + σ 2 2 ) + σ 1 2 t 2 − 2 σ 1 2 σ 2 2 d x =\displaystyle\int_{-\infty}^{\infty}e^{\frac{(\sigma_1^2+\sigma_2^2)(x-\frac{\sigma_1^2t}{\sigma_1^2+\sigma_2^2})^2-\frac{\sigma_1^4t^2}{(\sigma_1^2+\sigma_2^2)^2}\cdot(\sigma_1^2+\sigma_2^2)+\sigma_1^2t^2}{-2\sigma_1^2\sigma_2^2}}dx =e2σ12σ22(σ12+σ22)(xσ12+σ22σ12t)2(σ12+σ22)2σ14t2(σ12+σ22)+σ12t2dx

= ∫ − ∞ ∞ e ( σ 1 2 + σ 2 2 ) ( x − σ 1 2 t σ 1 2 + σ 2 2 ) 2 + σ 1 2 σ 2 2 t 2 σ 1 2 + σ 2 2 − 2 σ 1 2 σ 2 2 d x =\displaystyle\int_{-\infty}^{\infty}e^{\frac{(\sigma_1^2+\sigma_2^2)(x-\frac{\sigma_1^2t}{\sigma_1^2+\sigma_2^2})^2+\frac{\sigma_1^2\sigma_2^2t^2}{\sigma_1^2+\sigma_2^2}}{-2\sigma_1^2\sigma_2^2}}dx =e2σ12σ22(σ12+σ22)(xσ12+σ22σ12t)2+σ12+σ22σ12σ22t2dx

= ∫ − ∞ ∞ e ( σ 1 2 + σ 2 2 ) ( x − σ 1 2 t σ 1 2 + σ 2 2 ) 2 − 2 σ 1 2 σ 2 2 − t 2 σ 1 2 + σ 2 2 d x =\displaystyle\int_{-\infty}^{\infty}e^{\frac{(\sigma_1^2+\sigma_2^2)(x-\frac{\sigma_1^2t}{\sigma_1^2+\sigma_2^2})^2}{-2\sigma_1^2\sigma_2^2}-\frac{t^2}{\sigma_1^2+\sigma_2^2}}dx =e2σ12σ22(σ12+σ22)(xσ12+σ22σ12t)2σ12+σ22t2dx

= e − t 2 σ 1 2 + σ 2 2 ∫ − ∞ ∞ e ( σ 1 2 + σ 2 2 ) ( x − σ 1 2 t σ 1 2 + σ 2 2 ) 2 − 2 σ 1 2 σ 2 2 d x =e^{\frac{-t^2}{\sigma_1^2+\sigma_2^2}}\displaystyle\int_{-\infty}^{\infty}e^{\frac{(\sigma_1^2+\sigma_2^2)(x-\frac{\sigma_1^2t}{\sigma_1^2+\sigma_2^2})^2}{-2\sigma_1^2\sigma_2^2}}dx =eσ12+σ22t2e2σ12σ22(σ12+σ22)(xσ12+σ22σ12t)2dx

前面说过,无穷积分上被积函数的自变量平移操作不影响积分值(可以用换元法证明,令 y = x − σ 1 2 t σ 1 2 + σ 2 2 y=x-\frac{\sigma_1^2t}{\sigma_1^2+\sigma_2^2} y=xσ12+σ22σ12t, d x = d y dx=dy dx=dy,换元必换限,这里上下限依旧是正负无穷,不变)
上式
= e − t 2 σ 1 2 + σ 2 2 ∫ − ∞ ∞ e ( σ 1 2 + σ 2 2 ) x 2 − 2 σ 1 2 σ 2 2 d x =e^{\frac{-t^2}{\sigma_1^2+\sigma_2^2}}\displaystyle\int_{-\infty}^{\infty}e^{\frac{(\sigma_1^2+\sigma_2^2)x^2}{-2\sigma_1^2\sigma_2^2}}dx =eσ12+σ22t2e2σ12σ22(σ12+σ22)x2dx

= e − t 2 σ 1 2 + σ 2 2 ∫ − ∞ ∞ e − ( σ 1 2 + σ 2 2 2 σ 1 σ 2 x ) 2 d x =e^{\frac{-t^2}{\sigma_1^2+\sigma_2^2}}\displaystyle\int_{-\infty}^{\infty}e^{-(\frac{\sqrt{\sigma_1^2+\sigma_2^2}}{\sqrt{2}\sigma_1\sigma_2}x)^2}dx =eσ12+σ22t2e(2 σ1σ2σ12+σ22 x)2dx

y = σ 1 2 + σ 2 2 2 σ 1 σ 2 x y=\frac{\sqrt{\sigma_1^2+\sigma_2^2}}{\sqrt{2}\sigma_1\sigma_2}x y=2 σ1σ2σ12+σ22 x,则 d x = 2 σ 1 σ 2 σ 1 2 + σ 2 2 d y dx=\frac{\sqrt{2}\sigma_1\sigma_2}{\sqrt{\sigma_1^2+\sigma_2^2}}dy dx=σ12+σ22 2 σ1σ2dy,换元必换限,上下限依旧是正负无穷不变,从而有
原式
= e − t 2 σ 1 2 + σ 2 2 ∫ − ∞ ∞ e − y 2 ⋅ 2 σ 1 σ 2 σ 1 2 + σ 2 2 d y =e^{\frac{-t^2}{\sigma_1^2+\sigma_2^2}}\displaystyle\int_{-\infty}^{\infty}e^{-y^2}\cdot\frac{\sqrt2\sigma_1\sigma_2}{\sqrt{\sigma_1^2+\sigma_2^2}}dy =eσ12+σ22t2ey2σ12+σ22 2 σ1σ2dy

= e − t 2 σ 1 2 + σ 2 2 2 σ 1 σ 2 σ 1 2 + σ 2 2 ∫ − ∞ ∞ e − y 2 d y =e^{\frac{-t^2}{\sigma_1^2+\sigma_2^2}}\frac{\sqrt2\sigma_1\sigma_2}{\sqrt{\sigma_1^2+\sigma_2^2}}\displaystyle\int_{-\infty}^{\infty}e^{-y^2}dy =eσ12+σ22t2σ12+σ22 2 σ1σ2ey2dy

= e − t 2 σ 1 2 + σ 2 2 2 σ 1 σ 2 σ 1 2 + σ 2 2 π =e^{\frac{-t^2}{\sigma_1^2+\sigma_2^2}}\frac{\sqrt2\sigma_1\sigma_2}{\sqrt{\sigma_1^2+\sigma_2^2}}\sqrt\pi =eσ12+σ22t2σ12+σ22 2 σ1σ2π

其中 ∫ − ∞ ∞ e − y 2 d y = π \int_{-\infty}^{\infty}e^{-y^2}dy=\sqrt\pi ey2dy=π ,一般的高数书上都有,不等式放缩证明收敛性;平方后取极坐标,再两边夹取极限得到收敛的值。

最后得到
f ( t ) = 1 2 π σ 1 σ 2 ⋅ e − t 2 σ 1 2 + σ 2 2 ⋅ 2 σ 1 σ 2 σ 1 2 + σ 2 2 ⋅ π f(t)=\frac{1}{2\pi\sigma_1\sigma_2}\cdot e^{\frac{-t^2}{\sigma_1^2+\sigma_2^2}}\cdot\frac{\sqrt2\sigma_1\sigma_2}{\sqrt{\sigma_1^2+\sigma_2^2}}\cdot\sqrt\pi f(t)=2πσ1σ21eσ12+σ22t2σ12+σ22 2 σ1σ2π
= 1 2 π σ 1 2 + σ 2 2 e − t 2 σ 1 2 + σ 2 2 =\frac{1}{\sqrt{2\pi}\sqrt{\sigma_1^2+\sigma_2^2}}e^{\frac{-t^2}{\sigma_1^2+\sigma_2^2}} =2π σ12+σ22 1eσ12+σ22t2
= 1 2 π σ e − t 2 σ 2 =\frac{1}{\sqrt{2\pi}\sigma}e^{\frac{-t^2}{\sigma^2}} =2π σ1eσ2t2
f ( t ) f(t) f(t)是标准差为 σ \sigma σ的高斯分布的概率密度函数,其中 σ 2 = σ 1 2 + σ 2 2 \sigma^2=\sigma_1^2+\sigma_2^2 σ2=σ12+σ22,证毕。

从理论上说,一张图先后经过 σ 1 , σ 2 \sigma_1,\sigma_2 σ1,σ2的高斯模糊和直接经过 σ \sigma σ的模糊后的状态是一样的,但是实际操作中,由于高斯核模板大小并不是无穷的,而是取了有限个元,所以其中的误差累计,具体会是什么结果也难以预料,只能多做做实验进行对比了。而在sift尺度空间的构造中,因为 σ \sigma σ σ 2 \sigma_2 σ2大,所以对应的高斯模板也会大一点,运算量就大了。也就是在 I 0 , I 1 I_0,I_1 I0,I1已知的情况下,由 I 1 I_1 I1得到 I 2 I_2 I2比由 I 0 I_0 I0得到 I 2 I_2 I2的实际运算量会小点。

  • 5
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,我可以为您提供一个简单的代码示例。首先,我们需要导入必要的库和图像文件: ```python import cv2 import numpy as np # 读取图像 img = cv2.imread('image.jpg') ``` 然后,我们可以使用以下代码生成一个随机高斯: ```python # 定义卷大小和标准差 ksize = (3, 3) sigma = 1.5 # 生成高斯 kernel = np.zeros(ksize, dtype=np.float32) mid = int(ksize[0] / 2) for i in range(-mid, mid + 1): for j in range(-mid, mid + 1): kernel[i + mid][j + mid] = np.exp(-(i ** 2 + j ** 2) / (2 * sigma ** 2)) sum_kernel = np.sum(kernel) kernel = kernel / sum_kernel print("生成的随机高斯为:\n", kernel) ``` 在上述代码中,我们首先定义了卷的大小和标准差,然后使用两个嵌套的循环生成高斯。最后,我们将卷的所有元素除以它们的总和,以确保它们的总和为1。 最后,我们可以使用以下代码将生成的卷应用于输入图像: ```python # 对图像进行卷 dst = cv2.filter2D(img, -1, kernel) # 显示原始图像和卷后的图像 cv2.imshow('Original Image', img) cv2.imshow('Gaussian Blurred Image', dst) cv2.waitKey(0) ``` 在上述代码中,我们使用OpenCV库中的filter2D函数将生成的卷应用于输入图像。最后,我们使用imshow函数显示原始图像和卷后的图像。 完整代码如下: ```python import cv2 import numpy as np # 读取图像 img = cv2.imread('image.jpg') # 定义卷大小和标准差 ksize = (3, 3) sigma = 1.5 # 生成高斯 kernel = np.zeros(ksize, dtype=np.float32) mid = int(ksize[0] / 2) for i in range(-mid, mid + 1): for j in range(-mid, mid + 1): kernel[i + mid][j + mid] = np.exp(-(i ** 2 + j ** 2) / (2 * sigma ** 2)) sum_kernel = np.sum(kernel) kernel = kernel / sum_kernel print("生成的随机高斯为:\n", kernel) # 对图像进行卷 dst = cv2.filter2D(img, -1, kernel) # 显示原始图像和卷后的图像 cv2.imshow('Original Image', img) cv2.imshow('Gaussian Blurred Image', dst) cv2.waitKey(0) ``` 请注意,此代码仅为示例,生成的卷可能不足以完全模拟真实的高斯分布。如果需要更准确的结果,请使用OpenCV库中的GaussianBlur函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值