3x3 convolution kernel to IM ftt convolution

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
smajler
Posts: 28
Joined: 2013-05-17T09:26:53-07:00
Authentication code: 6789

3x3 convolution kernel to IM ftt convolution

Post by smajler »

Hi there i'm wondering is there any way to convert simple 3x3 convolution kernel like:
[1,2,1]
[2,4,2]
[1,2,1]
to ImageMagick FTT convolution/deconvolution. In IM ftt i saw that there authors for kernel use image with the same size as input image with circle filled of solid white color or white tone. In my app i'm using 3x3 kernel where user sets kernel values>0 and i'd like to transform it to IM white circle filled of toned white depending on my kernel values.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: 3x3 convolution kernel to IM ftt convolution

Post by fmw42 »

NOTE: for such a small filter size, it is really a waste of effort to do it in the Fourier domain.


Nevertheless, if you really want to do this, you will need to use IM in HDRI mode

input image:
Image

# create (binomial low pass) filter -- note the center must be shifted to the top left corner
convert \
\( xc:"gray(1)" xc:"gray(2)" xc:"gray(1)" +append \) \
\( xc:"gray(2)" xc:"gray(4)" xc:"gray(2)" +append \) \
\( xc:"gray(1)" xc:"gray(2)" xc:"gray(1)" +append \) \
-append \
-background black -extent 128x128 \
-auto-level \
-roll -1-1 \
filter.png
Image

# Do +FFT on both images, combine real and imaginary components appropriately for complex multiplication (see http://www.imagemagick.org/Usage/fourie ... ultiply_ri) and then +IFT and scale by inverse of mean of filter.


fact=`convert filter.png -format "%[fx:1/mean]" info:`
convert \
\( filter.png +fft \) \
\( lena2.jpg +fft \) \
\( -clone 0 -clone 2 -compose multiply -composite \) \
\( -clone 1 -clone 3 -compose multiply -composite \) \
\( -clone 4 -clone 5 +swap -compose minus -composite \) \
\( -clone 0 -clone 3 -compose multiply -composite \) \
\( -clone 1 -clone 2 -compose multiply -composite \) \
\( -clone 7 -clone 8 -compose plus -composite \) \
-delete 0-5,7,8 \
+ift -evaluate multiply $fact \
lena2_filtered.png
Image


As of IM 6.8.7.1 with the new -complex mathematic function, the previous commands can be simplified to

fact=`convert filter.png -format "%[fx:1/mean]" info:`
imh convert \
\( filter.png +fft \) \
\( lena2.png +fft \) \
-complex multiply \
+ift -evaluate multiply $fact \
lena2_filtered.png
smajler
Posts: 28
Joined: 2013-05-17T09:26:53-07:00
Authentication code: 6789

Re: 3x3 convolution kernel to IM ftt convolution

Post by smajler »

i've compiled IM to Quantum 8 bit ( i need 8 bit), set define to use HDRI 1, and add #define MAGICKCORE_FFTW_DELEGATE.
This configuration should be ok i think. I need so small kernel, becouse i'm developing application where user sets a kernel 3x3 or 5x5 manually and then with this kernel i'd like to do a deconvolution so thats why i'm going to use IM. I didn't find any way to do deconvolution on my own.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: 3x3 convolution kernel to IM ftt convolution

Post by fmw42 »

A simple convolution can be done using http://www.imagemagick.org/Usage/convolve/

If you need to do deconvolution, then that would need to be done in the Fourier Domain.

See
http://www.fmwconcepts.com/imagemagick/ ... urier.html
http://www.imagemagick.org/Usage/fourier/
http://www.imagemagick.org/Usage/fourier/fft_math/

Also see the new -complex operator at http://www.imagemagick.org/script/comma ... hp#complex

and the -define fourier:normalize at http://www.imagemagick.org/script/comma ... ns.php#fft
Post Reply