Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImageMagick: Looking for a fast way to blur an image

I am searching for a faster way to blur an image than to use the GaussianBlur.
The solution I am looking for can be a command line solution, but I prefer code in perl notation.

Actually, we use the Perl image magick API to blur images:

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

This works fine, but with the level height the amount of time it consumes seems to grow exponentially.

Question: How can I improve the time used for the bluring operation? Is there another faster approach to blur images?

like image 844
Thariama Avatar asked Feb 26 '16 10:02

Thariama


3 Answers

I found that the suggested method of resizing image for blur imitation makes output look very pixelated for very large values of sigma like 25 or more. So I finally came to an idea of downscale-blur-enlarge, which makes very nice result (almost indistinguishable from simple blur with large sigma):

# 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

On my i5 2.7Ghz it shows up to 10x speed up.

like image 53
Alexander Svetkin Avatar answered Oct 20 '22 19:10

Alexander Svetkin


The 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

That would simply be:

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

But the Perl ImageMagick documentation has the same text on both Blur and GaussianBlur (emphasis mine). I can't try now, you would have to benchmark it yourself.

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).

An alternative that the documentation also lists is resizing the image to be very tiny, and then enlarging again.

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.

The example command line code is this:

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
like image 6
simbabque Avatar answered Oct 20 '22 18:10

simbabque


Not sure if I could still help OP with this, but I recently tried the same for a blurred screenlock picture. I found that omitting the -blur part saves even more calculation time and is still delivering great results for a 4K picture:

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

However, you couldn't go lower than 2.5% with 3840x2160. It will resize the image. I guess the eps value differs for pictures of other sizes.

It should be noted, that the resulting image sizes differ noticably!

like image 5
Scareactor Avatar answered Oct 20 '22 17:10

Scareactor