Search code examples
perlimage-processingimagemagickgaussiangaussianblur

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?


Solution

  • 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