Page 1 of 1

Multiply by Kernel

Posted: 2012-10-10T21:28:44-07:00
by hwttdz
Is it possibly to multiply by a kernel, this could be very convenient. I'm trying variations of:

Code: Select all

convert test.ppm -compose Multiply "Gaussian:0x2" test_by_gaussian.ppm

Re: Multiply by Kernel

Posted: 2012-10-10T21:53:14-07:00
by fmw42
convert test.ppm -compose Multiply "Gaussian:0x2" test_by_gaussian.ppm
You cannot do that per se in IM.

1) there is no -compose Multiply "Gaussian:0x2"

2) -compose is associated with -composite and requires two images, generally the same size. They then are combined point by point.

3) What I presume you want is to convolve a (small) gaussian filter with the image (not multiply) using -either -convolve or -morphology convolve or one of the built in -morphology kernels or to use -blur or -gaussian-blur. You cannot multiply every point in an image by a 2D pattern. You generally convolve it. That means for each pixel, multiply it and its neighbors by the values in the small filter, sum the products and divide by the sum of the filter values. (actually that is correlation, but it is the same as convolution if the filter is symmetric with respect to 180 degree rotation). Most likely what you want to do is either:

convert image -blur 0x2 result
or slower but the same result
convert image -gaussian-blur 0x2 result

4) If you convert your image and spatial gaussian filter via FFT, then you can multiply them point by point and then do an IFT

See
http://homepages.inf.ed.ac.uk/rbf/HIPR2/convolve.htm
http://www.fmwconcepts.com/imagemagick/ ... tering.pdf
http://www.imagemagick.org/script/comma ... p#convolve
http://www.imagemagick.org/script/comma ... s.php#blur
http://www.imagemagick.org/script/comma ... ssian-blur
http://www.imagemagick.org/Usage/convolve/#gaussian
http://www.imagemagick.org/Usage/compose/
http://www.fmwconcepts.com/imagemagick/ ... urier.html


If you could explain a bit more detail about what you are trying to do or want to do, it would be more helpful and perhaps we can clarify further.

Re: Multiply by Kernel

Posted: 2012-10-10T22:15:47-07:00
by hwttdz
You cannot do that.
1) there is no Gaussian:0x2 by itself
Right, I was wondering if I could automagically get one, in much the same way that I can automagically create a canvas using "-size 64x64 canvas:black".
2) -compose is associated with -composite and requires two images, generally the same size.
Right, I was hoping to do a multiply like in the rose example here http://www.imagemagick.org/Usage/compose/#multiply
3) What I presume you want is to convolve a (small) gaussian filter with the image (not multiply) using -either -convolve or -morphology convolve or one of the built in -morphology kernels or to use -blur or -gaussian-blur. You cannot multiply every point in an image by a 2D pattern. You generally convolve it. That means for each pixel, multiply it and its neighbors by the values in the small filter, sum the products and divide by the sum of the filter values. (actually that is correlation, but it is the same as convolution if the filter is symmetric with respect to 180 degree rotation).

See
http://homepages.inf.ed.ac.uk/rbf/HIPR2/convolve.htm
http://www.fmwconcepts.com/imagemagick/ ... tering.pdf
http://www.imagemagick.org/script/comma ... p#convolve
http://www.imagemagick.org/script/comma ... s.php#blur
http://www.imagemagick.org/script/comma ... ssian-blur
http://www.imagemagick.org/Usage/convolve/#gaussian
http://www.imagemagick.org/Usage/compose/

If you could explain a bit more detail about what you are trying to do or want to do, it would be more helpful and perhaps we can clarify further.
I'm trying to multiply the magnitude component of the fourier transform of an image by a gaussian in order to obtained a blurred image on doing the inverse fourier transform. I mostly understand the relation between convolution in real space <=> multiplication by kernel in phase space. But I still think I'd like to do it the totally backwards multiplicative way (if I would like to blur at numerous different radii it ends up being faster because I can "save my previous work").

Re: Multiply by Kernel

Posted: 2012-10-10T22:20:06-07:00
by hwttdz
More specifically, I'm looking at this:
http://imagemagick.org/Usage/fourier/#blurring
specifically the multiplication by circle/blurred circle.

Re: Multiply by Kernel

Posted: 2012-10-11T09:44:28-07:00
by fmw42
hwttdz wrote:More specifically, I'm looking at this:
http://imagemagick.org/Usage/fourier/#blurring
specifically the multiplication by circle/blurred circle.

see my unix shell script, fftfilter, at the link below.

You have to create a filter (supposedly gaussian as you mentioned above) whose size is the same size as the input image. The filter corresponds to what it should look like in the frequency domain. FFT is applied to the image and the magnitude is multiplied by your filter image and then the new magnitude and the phase images are combined by IFT.

If you want to create a spatial domain filter, and have that transformed, then use my script fftconvol.

Re: Multiply by Kernel

Posted: 2012-10-11T17:15:55-07:00
by anthony
hwttdz wrote:
You cannot do that.
1) there is no Gaussian:0x2 by itself
Right, I was wondering if I could automagically get one, in much the same way that I can automagically create a canvas using "-size 64x64 canvas:black".
You can get a image from a convolution kernel in a number of ways...

1/ convolve a single white pixel in the center of a black canvas.
Warning, you may need to include a scaling factor equal to the value of the orgin value of the kernel.
See kernel scaling
http://www.imagemagick.org/Usage/convol ... el_scaling
You can get an output of the kernel values using the special setting
http://www.imagemagick.org/Usage/morphology/#showkernel

You can use kernel2image script (unix shell script)
http://www.imagemagick.org/Usage/morpho ... rnel2image
It is what I use to generate IM Example kernel images. It can scale the kernel values, assign colors, or enlarge (image scale) the resulting image so you can see individual values (pixels) more clearly.

For example, from IM Examples...
http://www.imagemagick.org/Usage/convol ... ussian.gif

This is the example from the scripts built in documentation, no gamma adjustment.
kernel2image -n -g 1 Gaussian:0x30 gaussian_kernel.png

WARNING: The script was written before the colorspace adjustments, the resulting image may need checking to ensure colorspace changes are not doing automatic adjustments when saving the resulting image, and you are getting properly linear grayscale images. Similarly when extra colors are added if generating an array of enlarged pixels.
2) -compose is associated with -composite and requires two images, generally the same size.
Right, I was hoping to do a multiply like in the rose example here http://www.imagemagick.org/Usage/compose/#multiply
They do not have to be the same size, but for multiply, parts not overlayed will be preserved as is.
There is a proposal to provide a special setting to overlay images with virtual pixels (tiled or fixed color), whcih means the whole destination image will be covered including areas not directly overlayed.
I would like to blur at numerous different radii it ends up being faster because I can "save my previous work".
Take a look at variable blur mapping...
http://www.imagemagick.org/Usage/mapping/#blur