如何在不使用任何内置高斯函数的情况下对图像进行高斯模糊? [英] How do I gaussian blur an image without using any in-built gaussian functions?

查看:26
本文介绍了如何在不使用任何内置高斯函数的情况下对图像进行高斯模糊?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用原生高斯模糊公式来模糊我的图像.我阅读了维基百科文章,但我不确定如何实现这一点.

I want to blur my image using the native Gaussian blur formula. I read the Wikipedia article, but I am not sure how to implement this.

如何使用公式来决定权重?

How do I use the formula to decide weights?

我不想使用像 MATLAB 那样的任何内置函数

I do not want to use any built in functions like what MATLAB has

推荐答案

编写一个简单的高斯模糊实际上很容易.它以与任何其他卷积滤波器完全相同的方式完成.盒子和高斯滤波器之间的唯一区别是您使用的矩阵.

Writing a naive gaussian blur is actually pretty easy. It is done in exactly the same way as any other convolution filter. The only difference between a box and a gaussian filter is the matrix you use.

假设您有一个如下定义的图像:

Imagine you have an image defined as follows:

 0  1  2  3  4  5  6  7  8  9
10 11 12 13 14 15 16 17 18 19
20 21 22 23 24 25 26 27 28 29
30 31 32 33 34 35 36 37 38 39
40 41 42 43 44 45 46 47 48 49
50 51 52 53 54 55 56 57 58 59
60 61 62 63 64 65 66 67 68 69
70 71 72 73 74 75 76 77 78 79
80 81 82 83 84 85 86 87 88 89
90 91 92 93 94 95 96 97 98 99

一个 3x3 的盒式滤波器矩阵定义如下:

A 3x3 box filter matrix is defined as follows:

0.111 0.111 0.111
0.111 0.111 0.111
0.111 0.111 0.111

要应用高斯模糊,您需要执行以下操作:

To apply the gaussian blur you would do the following:

对于像素 11,您需要加载像素 0、1、2、10、11、12、20、21、22.

For pixel 11 you would need to load pixels 0, 1, 2, 10, 11, 12, 20, 21, 22.

然后将像素 0 乘以 3x3 模糊滤镜的左上部分.顶部中间的像素 1,右上角的像素 2,像素 3,中间的左侧的像素 10,依此类推.

you would then multiply pixel 0 by the upper left portion of the 3x3 blur filter. Pixel 1 by the top middle, pixel 2, pixel 3 by top right, pixel 10 by middle left and so on.

然后将它们加在一起并将结果写入像素 11.如您所见,像素 11 现在是其自身和周围像素的平均值.

Then add them altogether and write the result to pixel 11. As you can see Pixel 11 is now the average of itself and the surrounding pixels.

边缘情况确实变得有点复杂.您对纹理边缘的值使用什么值?一种方法是绕到另一边.这对于稍后平铺的图像来说看起来不错.另一种方法是将像素推到周围的地方.

Edge cases do get a bit more complex. What values do you use for the values of the edge of the texture? One way can be to wrap round to the other side. This looks good for an image that is later tiled. Another way is to push the pixel into the surrounding places.

因此对于左上角,您可以按如下方式放置样本:

So for upper left you might place the samples as follows:

 0  0  1
 0  0  1
10 10 11

我希望您能看到如何轻松地将其扩展到大型过滤器内核(即 5x5 或 9x9 等).

I hope you can see how this can easily be extended to large filter kernels (ie 5x5 or 9x9 etc).

高斯滤波器和箱式滤波器之间的区别在于矩阵中的数字.高斯滤波器使用跨行和列的高斯分布.

The difference between a gaussian filter and a box filter is the numbers that go in the matrix. A gaussian filter uses a gaussian distribution across a row and column.

例如对于任意定义为的过滤器(即这不是高斯,但可能不远)

e.g for a filter defined arbitrarily as (ie this isn't a gaussian, but probably not far off)

0.1 0.8 0.1

第一列将相同,但乘以上面一行的第一项.

the first column would be the same but multiplied into the first item of the row above.

0.01 0.8 0.1
0.08 
0.01 

第二列将相同,但值将乘以上面一行中的 0.8(依此类推).

The second column would be the same but the values would be multiplied by the 0.8 in the row above (and so on).

0.01 0.08 0.01
0.08 0.64 0.08
0.01 0.08 0.01

将以上所有加在一起的结果应该等于 1.上述过滤器和原始盒式过滤器之间的区别在于写入的最终像素对中心像素的权重要大得多(即已经在那个位置).发生模糊是因为周围的像素确实模糊到该像素中,尽管没有那么多.使用这种过滤器,您会得到一种模糊效果,但不会破坏太多的高频(即从像素到像素的颜色快速变化)信息.

The result of adding all of the above together should equal 1. The difference between the above filter and the original box filter would be that the end pixel written would have a much heavier weighting towards the central pixel (ie the one that is in that position already). The blur occurs because the surrounding pixels do blur into that pixel, though not as much. Using this sort of filter you get a blur but one that doesn't destroy as much of the high frequency (ie rapid changing of colour from pixel to pixel) information.

这类过滤器可以做很多有趣的事情.您可以使用这种过滤器通过从当前像素中减去周围像素来进行边缘检测.这将只留下真正巨大的颜色变化(高频).

These sort of filters can do lots of interesting things. You can do an edge detect using this sort of filter by subtracting the surrounding pixels from the current pixel. This will leave only the really big changes in colour (high frequencies) behind.

5x5 过滤器内核的定义与上述完全相同.

A 5x5 filter kernel is define exactly as above.

例如,如果您的行是 0.1 0.2 0.4 0.2 0.1 那么如果您将其中的每个值乘以第一项以形成一列,然后将每个值乘以第二项以形成第二列,依此类推使用过滤器

e.g if your row is 0.1 0.2 0.4 0.2 0.1 then if you multiply each value in their by the first item to form a column and then multiply each by the second item to form the second column and so on you'll end up with a filter of

0.01 0.02 0.04 0.02 0.01
0.02 0.04 0.08 0.04 0.02
0.04 0.08 0.16 0.08 0.04
0.02 0.04 0.08 0.04 0.02
0.01 0.02 0.04 0.02 0.01

取一些任意位置你可以看到位置 0, 0 是简单的 0.1 * 0.1.位置 0、2 为 0.1 * 0.4,位置 2、2 为 0.4 * 0.4,位置 1、2 为 0.2 * 0.4.

taking some arbitrary positions you can see that position 0, 0 is simple 0.1 * 0.1. Position 0, 2 is 0.1 * 0.4, position 2, 2 is 0.4 * 0.4 and position 1, 2 is 0.2 * 0.4.

我希望这能给你一个足够好的解释.

I hope that gives you a good enough explanation.

这篇关于如何在不使用任何内置高斯函数的情况下对图像进行高斯模糊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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