模糊高斯算法是什么样的?有实施例子吗?

发布于 2024-08-02 08:29:43 字数 211 浏览 6 评论 0原文

我有一个位图图像上下文,想让它看起来模糊。所以我能想到的最好的办法是高斯算法,但我对这种高斯模糊算法是什么样子不太了解?您知道这方面的好的教程或示例吗?如果全部都是手工完成而不使用太多特定于语言的 API,那么语言并不那么重要。即在可可中,幸运的人不需要考虑它,他们只需使用已经存在的 Imagefilter 即可。但我在cocoa-touch(objective-c,iPhone OS)中没有这样的东西。

I have a bitmap image context and want to let this appear blurry. So best thing I can think of is a gauss algorithm, but I have no big idea about how this kind of gauss blur algorithms look like? Do you know good tutorials or examples on this? The language does not matter so much, if it's done all by hand without using language-specific API too much. I.e. in cocoa the lucky guys don't need to think about it, they just use a Imagefilter that's already there. But I don't have something like this in cocoa-touch (objective-c, iPhone OS).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾`听者〃 2024-08-09 08:29:43

这其实很简单。您有一个过滤器模式(也称为过滤器内核) - 一个带有系数的(小)矩形数组 - 只需计算 图像和图案的卷积

for y = 1 to ImageHeight
  for x = 1 to ImageWidth
    newValue = 0
    for j = 1 to PatternHeight
      for i = 1 to PatternWidth
        newValue += OldImage[x-PatternWidth/2+i,y-PatternHeight/2+j] * Pattern[i,j]
    NewImage[x,y] = newValue

该图案只是二维高斯曲线或您喜欢的任何其他滤波器图案。您必须注意图像的边缘,因为滤镜图案将部分位于图像之外。您可以假设这些像素是黑色的,或者使用图像的镜像版本,或者看起来合理的东西。

最后一点,使用傅里叶变换计算卷积有更快的方法,但这个简单的版本应该足以满足第一次测试的需要。

This is actually quite simple. You have a filter pattern (also known as filter kernel) - a (small) rectangular array with coefficients - and just calculate the convolution of the image and the pattern.

for y = 1 to ImageHeight
  for x = 1 to ImageWidth
    newValue = 0
    for j = 1 to PatternHeight
      for i = 1 to PatternWidth
        newValue += OldImage[x-PatternWidth/2+i,y-PatternHeight/2+j] * Pattern[i,j]
    NewImage[x,y] = newValue

The pattern is just a Gauss curve in two dimensions or any other filter pattern you like. You have to take care at the edges of the image because the filter pattern will be partialy outside of the image. You can just assume that this pixels are balck, or use a mirrored version of the image, or what ever seems reasonable.

As a final note, there are faster ways to calculate a convolution using Fourier transforms but this simple version should be sufficent for a first test.

半透明的墙 2024-08-09 08:29:43

维基百科文章除了有关该主题的一些标准信息之外,还有一个示例矩阵。

The Wikipedia article has a sample matrix in addition to some standard information on the subject.

带上头具痛哭 2024-08-09 08:29:43

图像处理的最佳位置是这里。您可以在那里获取 matlab 代码。
这个 Wolfram 演示应该可以消除对手动操作的任何疑问。

如果你不想学习太多东西,可以学习 PIL(Python Imaging Library)

此处”正是您所需要的。

从上面的链接复制的代码:

import ImageFilter

def filterBlur(im):

    im1 = im.filter(ImageFilter.BLUR)

    im1.save("BLUR" + ext)

filterBlur(im1)

Best place for image processing is THIS. You can get matlab codes there.
And this Wolfram demo should clear any doubts about doing it by hand.

And if you don't want to learn too many things learn PIL(Python Imaging Library).

"Here" is exactly what you need.

Code copied from above link:

import ImageFilter

def filterBlur(im):

    im1 = im.filter(ImageFilter.BLUR)

    im1.save("BLUR" + ext)

filterBlur(im1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文