Quickest way to do a large radius blur in grayscale

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
hwttdz
Posts: 22
Joined: 2010-08-29T05:29:08-07:00
Authentication code: 8675308

Quickest way to do a large radius blur in grayscale

Post by hwttdz »

I would like to use in a script a very strongly blurred grayscale copy of an image and I am wondering at the relative speeds and advantage to the various methods

I am currently using the gaussian-blur, but will compare the results of the gaussian-blur to those of blur. Additionally, it seems I can use a low pass filter in frequency space (as here: http://www.fmwconcepts.com/imagemagick/ ... /index.php or http://www.fmwconcepts.com/imagemagick/ ... /index.php).

Also since it's in grayscale space I can use only one channel correct? How can I ensure that I'm only performing the operation once instead of once per channel?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Quickest way to do a large radius blur in grayscale

Post by anthony »

gaussian-blur is a full 2-dimentional convolution.

You can see its internal working by looking at the 'gaussian kernel' with the -morphology convolution methdo
http://www.imagemagick.org/Usage/convolve/#gaussian

For large blurs a 2-pass 1-dimentional blur (directly available as -blur) is orders of magnitude faster. the larger the blur the greater the different between them.

Internal workings of a 1-dimensional gaussian convolution can be seen in
http://www.imagemagick.org/Usage/convolve/#blur

And the very next section as to how they are equivelent (except for edge effects, and low-level rounding quantum effects)
IM examples, Convolution, Gaussian vs Blur Kernels
http://www.imagemagick.org/Usage/convol ... an_vs_blur


That whole page goes very deep into other bluring techniques and even its use to unsharp (sparning images by subtracting its blur).


Even faster methods...
The other method of extremely large blurs is actually to resize the image smaller and larger. This is a blur that is created more by essentially compressing and losing information, than actually filtering information.

A third method of extremely large sigma but more more exact blurring is to use fourier transform to directly remove (low-pass filter) the frequencies in an image. This may be the best recommendation, speed wise.
Fourier transforsm, Bluring - Low Pass Filtering
http://www.imagemagick.org/Usage/fourier/#blurring

Actually this also allows you to implement a very simple elliptical blurring though I have not tried it or created and example.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Quickest way to do a large radius blur in grayscale

Post by fmw42 »

hwttdz wrote:I would like to use in a script a very strongly blurred grayscale copy of an image and I am wondering at the relative speeds and advantage to the various methods

I am currently using the gaussian-blur, but will compare the results of the gaussian-blur to those of blur. Additionally, it seems I can use a low pass filter in frequency space (as here: http://www.fmwconcepts.com/imagemagick/ ... /index.php or http://www.fmwconcepts.com/imagemagick/ ... /index.php).

Also since it's in grayscale space I can use only one channel correct? How can I ensure that I'm only performing the operation once instead of once per channel?

-blur will be much faster than -gaussian-blur.

but the fastest way is to use -scale followed by -resize to zoom it back to full size. However, it will be a little blocky in its results.

convert logo: -scale 10% -interpolate bicubic -resize 1000% logo_blur.png
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Quickest way to do a large radius blur in grayscale

Post by anthony »

fmw42 wrote:

Code: Select all

convert logo: -scale 10% -interpolate bicubic -resize 1000% logo_blur.png
This actually works better at even larger blurs!

Code: Select all

convert logo: -scale 2% -interpolate bicubic -resize 5000% logo_blur.png
However the size of the results may not be exactly the same due to interger rounding effect at the small scale.
You may need to extract and re-use the exact original size for the enlargement rather than depend on percentages.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Quickest way to do a large radius blur in grayscale

Post by fmw42 »

Anthony wrote:However the size of the results may not be exactly the same due to interger rounding effect at the small scale.
You may need to extract and re-use the exact original size for the enlargement rather than depend on percentages.
Good point.

Fred
hwttdz
Posts: 22
Joined: 2010-08-29T05:29:08-07:00
Authentication code: 8675308

Re: Quickest way to do a large radius blur in grayscale

Post by hwttdz »

I'll certainly try both (all) methods, hopefully some relative timings will be forthcoming in a bit.

I've started with the fft method and have successfully built imagemagick with fft support, and it seems to be working, but I get a
"convert: unbalanced parenthesis `)' @ error/convert.c/ConvertImageCommand/3011."
on:
"/usr/local/bin/convert temp.ppm -fft +depth \( -clone 0 -write temp_magnitude.ppm +delete \) \( -clone 1 -write temp_phase.png +delete \)"

Anything to worry about?
Last edited by hwttdz on 2011-06-08T21:53:30-07:00, edited 2 times in total.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Quickest way to do a large radius blur in grayscale

Post by fmw42 »

try

/usr/local/bin/convert temp.ppm -fft result.ppm

If ppm supports multiple layers, then

result.ppm[0] is magnitude
and
result.pmm[1]= is phase

otherwise

result-0.ppm is magnitude
and
result-1.pmm is phase


Note you will not see much but black with the magnitude, so you usually need to process it with -evaluate log XX or -gamma XX or -evaluate pow XX. I usually use -evaluate log XX where xx varies depending upon image and how much detail you want to see from about 10 to 10,000 (in factors of 10, e.g. 10, 100, 1000, 10000).


Fred
Last edited by fmw42 on 2011-06-08T21:59:23-07:00, edited 2 times in total.
hwttdz
Posts: 22
Joined: 2010-08-29T05:29:08-07:00
Authentication code: 8675308

Re: Quickest way to do a large radius blur in grayscale

Post by hwttdz »

Well that only gives one output file, but I can't open it in the gimp. I guess I can play with the image in imagemagick later. Using ppm's for the intermediates seems appealing as I won't get errors from compression.

The previous other than the warning seems to work, so I may as well stick with it for the moment.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Quickest way to do a large radius blur in grayscale

Post by fmw42 »

hwttdz wrote:Well that only gives one output file, but I can't open it in the gimp. I guess I can play with the image in imagemagick later. Using ppm's for the intermediates seems appealing as I won't get errors from compression.

The previous other than the warning seems to work, so I may as well stick with it for the moment.

The output file will have two layers.

Try using PNG to get two images. Or see my comments edited above.

Other formats are TIFF, PFM and MIFF all of which will have two layers. So PNG may be your best bet if you want two images. Again see my edited comments above in the previous post. PFM is best if you want to work in HDRI mode.
hwttdz
Posts: 22
Joined: 2010-08-29T05:29:08-07:00
Authentication code: 8675308

Re: Quickest way to do a large radius blur in grayscale

Post by hwttdz »

Both the magnitude and phase should have been the same format in my previous message.

Also, interestingly after transforming to phase space and back again the size of the image increased about 3 fold. But there seem to be little if any visually perceptible differences between the original and the recomposed file.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Quickest way to do a large radius blur in grayscale

Post by fmw42 »

hwttdz wrote:Both the magnitude and phase should have been the same format in my previous message.

Not sure what you mean by format. With FFT you specify only one output file, but it produces either a two-layer (two-frame) image or two images with -0 and -1 appended.

convert image -fft result.tiff

will produce result.tiff[0] as magnitude and result.tiff[1] for phase, but the output file you will see is just result.tiff. So you have to access each frame [0] and [1] as specified here.

convert image -fft result.png

will produce two images, result-0.png for magnitude and result-1.png

see details about this at:
http://www.fmwconcepts.com/imagemagick/ ... tml#im_fft
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Quickest way to do a large radius blur in grayscale

Post by fmw42 »

Your command might work, but you have to add null: at the end to delete the images produced by -fft as you only deleted the clones.

/usr/local/bin/convert temp.ppm -fft \( -clone 0 -write temp_magnitude.ppm +delete \) \( -clone 1 -write temp_phase.png +delete \) null:

I am not sure why you add +depth, as it is not needed and probably wont do anything. But see my notes above about the magnitude being mostly black.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Quickest way to do a large radius blur in grayscale

Post by anthony »

hwttdz wrote:I get a
"convert: unbalanced parenthesis `)' @ error/convert.c/ConvertImageCommand/3011."
on:
"/usr/local/bin/convert temp.ppm -fft +depth \( -clone 0 -write temp_magnitude.ppm +delete \) \( -clone 1 -write temp_phase.png +delete \)"
You just didn't give it an output image filename so the last parenthesis became a filename and the parenthesis became unbalanced.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply