实现高斯模糊 - 如何计算卷积矩阵(内核) [英] Implementing Gaussian Blur - How to calculate convolution matrix (kernel)

查看:1275
本文介绍了实现高斯模糊 - 如何计算卷积矩阵(内核)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题非常接近这个问题:如何高斯模糊图像,而不使用任何内置的高斯函数?



这个问题的答案很复杂好,但它没有给出实际计算真实高斯滤波器内核的示例。答案给出一个任意内核,并显示如何应用使用该内核的过滤器,但不是如何计算真正的内核本身。我试图从头开始在C ++或Matlab中实现高斯模糊,所以我需要知道如何从头计算内核。



如果有人可以使用任何小的示例图像矩阵来计算真实的高斯滤波器内核。

解决方案

您可以从头创建一个高斯内核MATLAB文档 fspecial 。请阅读该页中算法部分中的高斯内核创建公式,并按照下面的代码。代码是创建一个m-by-n矩阵,其中sigma = 1。

  m = 5; n = 5; 
sigma = 1;
[h1,h2] = meshgrid( - (m-1)/ 2:(m-1)/ 2, - (n-1)/ 2:(n-1)/ 2)
hg = exp( - (h1。^ 2 + h2。^ 2)/(2 * sigma ^ 2));
h = hg ./ sum(hg(:));

h =

0.0030 0.0133 0.0219 0.0133 0.0030
0.0133 0.0596 0.0983 0.0596 0.0133
0.0219 0.0983 0.1621 0.0983 0.0219
0.0133 0.0596 0.0983 0.0596 0.0133
0.0030 0.0133 0.0219 0.0133 0.0030

注意这可以通过内置的 fspecial 如下:

  fspecial('gaussian',[mn],sigma )
ans =

0.0030 0.0133 0.0219 0.0133 0.0030
0.0133 0.0596 0.0983 0.0596 0.0133
0.0219 0.0983 0.1621 0.0983 0.0219
0.0133 0.0596 0.0983 0.0596 0.0133
0.0030 0.0133 0.0219 0.0133 0.0030

我认为直接用任何你喜欢的语言实现这一点。 / p>

编辑:让我也添加 h1 h2 针对特定情况,因为您可能不熟悉 meshgrid

  h1 = 

-2 - 1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2
-2 -1 0 1 2

h2 =

-2 -2 -2 -2 -2 -2
-1 -1 -1 -1 -1
0 0 0 0 0
1 1 1 1 1
2 2 2 2 2


My question is very close to this question: How do I gaussian blur an image without using any in-built gaussian functions?

The answer to this question is very good, but it doesn't give an example of actually calculating a real Gaussian filter kernel. The answer gives an arbitrary kernel and shows how to apply the filter using that kernel but not how to calculate a real kernel itself. I am trying to implement a Gaussian blur in C++ or Matlab from scratch, so I need to know how to calculate the kernel from scratch.

I'd appreciate it if someone could calculate a real Gaussian filter kernel using any small example image matrix.

解决方案

You can create a Gaussian kernel from scratch as noted in MATLAB documentation of fspecial. Please read the Gaussian kernel creation formula in the algorithms part in that page and follow the code below. The code is to create an m-by-n matrix with sigma = 1.

m = 5; n = 5;
sigma = 1;
[h1, h2] = meshgrid(-(m-1)/2:(m-1)/2, -(n-1)/2:(n-1)/2);
hg = exp(- (h1.^2+h2.^2) / (2*sigma^2));
h = hg ./ sum(hg(:));

h =

    0.0030    0.0133    0.0219    0.0133    0.0030
    0.0133    0.0596    0.0983    0.0596    0.0133
    0.0219    0.0983    0.1621    0.0983    0.0219
    0.0133    0.0596    0.0983    0.0596    0.0133
    0.0030    0.0133    0.0219    0.0133    0.0030

Observe that this can be done by the built-in fspecial as follows:

fspecial('gaussian', [m n], sigma)
ans =

    0.0030    0.0133    0.0219    0.0133    0.0030
    0.0133    0.0596    0.0983    0.0596    0.0133
    0.0219    0.0983    0.1621    0.0983    0.0219
    0.0133    0.0596    0.0983    0.0596    0.0133
    0.0030    0.0133    0.0219    0.0133    0.0030

I think it is straightforward to implement this in any language you like.

EDIT: Let me also add the values of h1 and h2 for the given case, since you may be unfamiliar with meshgrid if you code in C++.

h1 =

    -2    -1     0     1     2
    -2    -1     0     1     2
    -2    -1     0     1     2
    -2    -1     0     1     2
    -2    -1     0     1     2

h2 =

    -2    -2    -2    -2    -2
    -1    -1    -1    -1    -1
     0     0     0     0     0
     1     1     1     1     1
     2     2     2     2     2

这篇关于实现高斯模糊 - 如何计算卷积矩阵(内核)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆