possible bug in -gaussian vs -blur IM 6.5.8-6

Post any defects you find in the released or beta versions of the ImageMagick software here. Include the ImageMagick version, OS, and any command-line required to reproduce the problem. Got a patch for a bug? Post it here.
Post Reply
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

possible bug in -gaussian vs -blur IM 6.5.8-6

Post by fmw42 »

IM 6.5.8-6 Q16 Mac OSX Tiger.

With regard to post viewtopic.php?f=3&t=15217&start=0, I believe that there may be a problem with -gaussian 0xsigma. It seems that for the same 0xsigma, I get a different result relative to -blur 0xsigma and MOREOVER, the larger the sigma the smaller the blur amount with -gaussian. I would not expect that. Here are my tests that come from the Magick text example at http://www.imagemagick.org/script/convert.php.

# same as in example using -gaussian 0x6, but does not reproduce example shadow
convert -size 320x85 xc:transparent -font Bookman-DemiItalic -pointsize 72 \
-draw "text 25,60 'Magick'" -channel RGBA -gaussian 0x6 -fill darkred -stroke magenta \
-draw "text 20,55 'Magick'" magick_text1.png
Image

# using -blur with same 0x6 radiusxsigma makes the shadow much closer to the example
convert -size 320x85 xc:transparent -font Bookman-DemiItalic -pointsize 72 \
-draw "text 25,60 'Magick'" -channel RGBA -blur 0x6 -fill darkred -stroke magenta \
-draw "text 20,55 'Magick'" tmp3.png
Image

# using -gaussian 0x10 produces less blurring in the shadow (opposite of what I would expect)!
convert -size 320x85 xc:transparent -font Bookman-DemiItalic -pointsize 72 \
-draw "text 25,60 'Magick'" -channel RGBA -gaussian 0x10 -fill darkred -stroke magenta \
-draw "text 20,55 'Magick'" tmp4.png
Image

Fred
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: possible bug in -gaussian vs -blur IM 6.5.8-6

Post by magick »

Grab ftp://magick.imagemagick.org/pub/ImageM ... 8-7.tar.gz. Build and add -debug transform to your command line. With -gaussian-blur 4x0.84089642 you get nearly the same results as described here: http://en.wikipedia.org/wiki/Gaussian_blur suggesting the algorithm is behaving properly. With a radius of 0, we expect different results between 1D and 2D blurs because the kernel radius differs between 1D and 2D gaussian kernels. See GetOptimalKernelWidth1D() and GetOptimalKernelWidth2D() in magick/gem.c. You should get nearly identical results if you have a fixed radius, for example 4x0.5.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: possible bug in -gaussian vs -blur IM 6.5.8-6

Post by fmw42 »

Not to be too argumentative, but

can you explain why this does not reproduce the example on http://www.imagemagick.org/script/convert.php with the very same IM code? Is that example in error in some way in either the result or the -gaussian arguments stated? Furthermore, why should a larger 0xsigma make the blur less??? This just does not make sense to me. Perhaps the radiusxsigma works fine, but maybe there is some error in the 2D 0xsigma case.


# same as in example using -gaussian 0x6, but does not reproduce example shadow
convert -size 320x85 xc:transparent -font Bookman-DemiItalic -pointsize 72 \
-draw "text 25,60 'Magick'" -channel RGBA -gaussian 0x6 -fill darkred -stroke magenta \
-draw "text 20,55 'Magick'" magick_text1.png
Image

Also I am surprised that the 2D and 1D code for generating the automatic radius from the sigma would be so different! Is there some planned reason for them being so different? Sorry to be so curious.

Looking at the code you referenced, I don't really see there is much difference in the mathematics formulae.

In 2D you have:
alpha=exp(-((double) u*u+v*v)/(2.0*sigma*sigma));
normalize+=alpha/(2.0*MagickPI*sigma*sigma);

In 1D you have the produce of two terms:
normalize+=exp(-((double) u*u)/(2.0*sigma*sigma))/(MagickSQ2PI*sigma);
normalize+=exp(-((double) v*v)/(2.0*sigma*sigma))/(MagickSQ2PI*sigma);

which is the same as the 2D case when multiplied because the produce of two exponentials is the same as one exponential with the sum of exponents. see http://en.wikipedia.org/wiki/Gaussian_filter (But my code reading is not that great, so perhaps I am not looking at the right part of the code or interpreting it correctly). Perhaps mathematically they are the same, but in practice the separable 1D gaussian (-blur) may not be equivalent to the 2D gaussian (-gaussian). Perhaps I should contact Rick Mabry and get a true mathematician's explanation.

Nevertheless I am surprised that they are not closer in their results and that a larger sigma for the 2D case results in a smaller blur?

I will download the 6.5.8-7 release and add -debug transform to that, but I am not sure what that is going to tell us that you have not already done?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: possible bug in -gaussian vs -blur IM 6.5.8-6

Post by magick »

When the radius is 0, we calculate the discrete kernel width by checking when the Gaussian function goes to 0. The 1 and 2-D Gaussian functions apparently go to 0 at different rates. We'll double check the Gaussian function for 1 and 2-D in ImageMagick but so far it appears they are implemented correctly. Perhaps Rick will provide some insight.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: possible bug in -gaussian vs -blur IM 6.5.8-6

Post by fmw42 »

Not to beat this to death, but at a minimum, something is wrong/inconsistent with the example on http://www.imagemagick.org/script/convert.php. Perhaps it just needs to be changed to -blur and a new example image displayed.

I have sent email to Rick Mabry and hope he can illuminate me further on any possible differences that one should expect. I have a feeling that the separable 1D (-blur) filter might produce more squarish results and 2D (-gaussian) filter might produce more circular results. But he can set me straight on that, I hope.

I will report back if and when I hear further from him or get any other clearer insight.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: possible bug in -gaussian vs -blur IM 6.5.8-6

Post by fmw42 »

I have corresponded with Rick Mabry on this. He believes that (mathematically) -blur and -gaussian should in principle produce equivalent (or nearly so) blurring. He thinks they are working correctly to do the blur, but something else is different when transparency is involved

He proposed 4 tests. The 3rd one below, which is the case of -gaussian on a transparent background produces for me a totally transparent image, whereas the 4th one below, which is the equivalent -blur case, shows a blurred square and circle on a transparent background. The 1st and 2nd, -blur and -gaussian on a white background, show equivalent results.

convert -size 400x200 xc:white -strokewidth 3 -stroke black -fill none -draw \
"polyline 50,50 150,50 150,150 50,150 50,50 circle 300,100 350,100" -channel \
RGBA -blur 0x6 tmp_blur-white-0x6.png

convert -size 400x200 xc:white -strokewidth 3 -stroke black -fill none -draw \
"polyline 50,50 150,50 150,150 50,150 50,50 circle 300,100 350,100" -channel \
RGBA -gaussian-blur 0x6 tmp_gaussian-white-0x6.png

convert -size 400x200 xc:transparent -strokewidth 3 -stroke black -fill none \
-draw "polyline 50,50 150,50 150,150 50,150 50,50 circle 300,100 350,100" \
-channel RGBA -alpha on -gaussian-blur 0x6 tmp_gaussian-trans-0x6.png

convert -size 400x200 xc:transparent -strokewidth 3 -stroke black -fill none \
-draw "polyline 50,50 150,50 150,150 50,150 50,50 circle 300,100 350,100" \
-channel RGBA -alpha on -blur 0x6 tmp_blur-trans-0x6.png

Is there some built-in difference with respect to how -blur and -gaussian work with transparency/alpha channel. I have a slight recollection that Anthony told me that -blur had been enhanced perhaps for transparency. Is that the issue?

I see that the example at http://www.imagemagick.org/script/convert.php has been changed to use -blur. So that takes care of that inconsistency. Thanks
Last edited by fmw42 on 2009-12-18T18:29:02-07:00, edited 1 time in total.
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: possible bug in -gaussian vs -blur IM 6.5.8-6

Post by magick »

We can reproduce the problem you posted and have a patch. Thanks for your persistence.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: possible bug in -gaussian vs -blur IM 6.5.8-6

Post by fmw42 »

Credit goes to Rick for his analysis and test cases.

Sorry I accidentally only posted 2 of the 4 cases. So you may want to look at them all. I have edited my last post to include all 4 of Rick's test cases.

Thanks.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: possible bug in -gaussian vs -blur IM 6.5.8-6

Post by fmw42 »

OK, in IM 6.5.8-7 Q16 Mac OSX Tiger, Rick's 3rd test with -gaussian and transparent background now works and matches the same thing with -blur. Thanks for the quick fix.
Post Reply