ImageMagick:寻找一种快速模糊图像的方法

ImageMagick: Looking for a fast way to blur an image

我正在寻找一种比使用 GaussianBlur.
更快地模糊 图像的方法 我正在寻找的解决方案可以是命令行解决方案,但我更喜欢使用 perl 表示法的代码。

实际上,我们使用 Perl 图像魔术 API 来模糊图像:

# $image is our Perl object holding a imagemagick perl image
# level is a natural number between 1 and 10
$image->GaussianBlur('x' . $level);

这很好用,但是随着关卡高度的增加,它消耗的时间似乎呈指数级增长。

问题:如何提高模糊操作的时间? 还有另一种更快的模糊图像的方法吗?

documentation speaks of the difference between Blur and GaussianBlur.

There has been some confusion as to which operator, "-blur" or the "-gaussian-blur" is better for blurring images. First of all "-blur" is faster, but it does this using two stage technique. First in one axis, then in the other. The "-gaussian-blur" operator on the other hand is more mathematically correct as it blurs in all directions simultaneously. The speed cost between the two can be enormous, by a factor of 10 or more, depending on the amount of bluring involved.

[...]

In summary, the two operators are slightly different, but only minimally. As "-blur" is much faster, use it. I do in just about all the examples involving blurring. Large

那就是:

$image->Blur( 'x' . $level );

但是 Perl ImageMagick documentationBlurGaussianBlur 上都有相同的文本(强调我的)。我现在无法尝试,您必须自己进行基准测试。

Blur: reduce image noise and reduce detail levels with a Gaussian operator of the given radius and standard deviation (sigma).

GaussianBlur: reduce image noise and reduce detail levels with a Gaussian operator of the given radius and standard deviation (sigma).

一个 alternative that the documentation also lists 正在将图像调整到非常小,然后再次放大。

Using large sigma values for image bluring is very slow. But onw technique can be used to speed up this process. This however is only a rough method and could use some mathematicaly rigor to improve results. Essentually the reason large blurs are slow is because you need a large window or 'kernel' to merge lots of pixels together, for each and every pixel in the image. However resize (making image smaller) does the same thing but generates fewer pixels in the process. The technique is basically shrink the image, then enlarge it again to generate the heavilly blured result. The Gaussian Filter is especially useful for this as you can directly specify a Gaussian Sigma define.

示例命令行代码是这样的:

convert  rose: -blur 0x5   rose_blur_5.png
convert rose: -filter Gaussian -resize 50% \
      -define filter:sigma=2.5 -resize 200%  rose_resize_5.png

我发现 the suggested method 调整图像大小以进行模糊模仿会使输出看起来非常像素化,因为对于非常大的 sigma 值(如 25 或更大)。所以我终于想到了缩小-模糊-放大的想法,它产生了非常好的结果(与大西格玛的简单模糊几乎没有区别):

# plain slow blur
convert -blur 0x25 sample.jpg blurred_slow.jpg
# much faster
convert -scale 10% -blur 0x2.5 -resize 1000% sample.jpg blurred_fast.jpg

在我的 i5 2.7Ghz 上,它显示了高达 10 倍的加速。

不确定我是否还能帮助 OP 解决这个问题,但我最近尝试了同样的方法来处理一张模糊的锁屏图片。 我发现 省略 -blur 部分可以节省更多的计算时间,并且仍然可以为 4K 图片提供出色的结果:

convert in.png -scale 2.5% -resize 4000% out.png
# real: 0.174s user: 0.144s size: 1.2MiB

convert in.png -scale 10% -blur 0x2.5 -resize 1000% out.png
# real: 0.136s user: 2.117s size: 1.2MiB

convert in.png -blur 0x25 out.png
# real: 2.425s user: 21.408s size: 1KiB

但是,在 3840x2160 分辨率下,您不能低于 2.5%。它将调整图像大小。我想 eps 值对于其他尺寸的图片是不同的。

需要注意的是,生成的图像大小有明显差异!