Page 1 of 1

Quickest way to do a large radius blur in grayscale

Posted: 2011-06-08T20:49:31-07:00
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?

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

Posted: 2011-06-08T21:09:42-07:00
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.

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

Posted: 2011-06-08T21:11:28-07:00
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

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

Posted: 2011-06-08T21:16:34-07:00
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.

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

Posted: 2011-06-08T21:39:17-07:00
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

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

Posted: 2011-06-08T21:43:07-07:00
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?

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

Posted: 2011-06-08T21:46:25-07:00
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

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

Posted: 2011-06-08T21:50:49-07:00
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.

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

Posted: 2011-06-08T21:51:57-07:00
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.

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

Posted: 2011-06-08T21:54:13-07:00
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.

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

Posted: 2011-06-08T22:02:52-07:00
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

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

Posted: 2011-06-08T22:06:26-07:00
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.

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

Posted: 2011-06-08T22:29:00-07:00
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.