Fred's ImageMagick Scripts



    Licensing:

    Copyright © Fred Weinhaus

    My scripts are available free of charge for non-commercial use, ONLY.

    For use of my scripts in commercial (for-profit) environments or non-free applications, please contact me (Fred Weinhaus) for licensing arrangements. My email address is fmw at alink dot net.

    If you: 1) redistribute, 2) incorporate any of these scripts into other free applications or 3) reprogram them in another scripting language, then you must contact me for permission, especially if the result might be used in a commercial or for-profit environment.

    Usage, whether stated or not in the script, is restricted to the above licensing arrangements. It is also subject, in a subordinate manner, to the ImageMagick license, which can be found at: http://www.imagemagick.org/script/license.php

CAMERADEBLUR


Deblurs an image in the frequency domain using an ideal deblurring filter for either motion blur or lens defocus.

Download Script

last modified: September 26, 2017



USAGE: cameradeblur [-t type] [-a amount] [-r rotation] [-n noise] infile outfile
USAGE: cameradeblur [-h or -help]

-t ... type ........ type of blur; choices are: motion (or m) or
.................... defocus (or d); default=defocus
-a ... amount ...... amount of blur; either length of motion blur or
.................... diameter of defocus; float>0; default=10
-r ... rotation .... rotation angle clockwise from horizontal for the
.................... motion blur; floats; -180<=rotation<=180; default=0
-n ... noise ....... estimate of the noise to signal power ratio; float>=0;
.................... default=0

PURPOSE: To deblur an image in the frequency domain using an ideal deblurring filter for either motion blur or lens defocus.

DESCRIPTION: CAMERADEBLUR deblurs an image in the frequency domain using an ideal frequency domain deblurring filter for either motion blur or lens defocus. The motion blur filter in the frequency domain is just an optionally rotated 1D sinc function. The lens defocus filter in the frequency domain is just a jinc function. The user specifies the parameters needed to create those functions directly as images. An explicit filter image is not input to the script. The internally generated filter will then be divided into the Fourier transform of the image created with +fft and the results will then be returned to the spatial domain via the inverse Fourier transform using +ift. Any alpha channel on the filter will be removed automatically before processing. If the image has an alpha channel it will not be processed, but simply copied from the input to the output.

ARGUMENTS:

-t type ... TYPE of blur. The choices are: motion (or m) and defocus (or d). The default=defocus.

-a amount ... AMOUNT of blur. This is either the length of the motion blur or the diameter of the lens defocus. Values are float>0. The default=10.

-r rotation ... ROTATION angle in degrees specified clockwise from horizontal for the motion blur. Values are floats with -180<=rotation<=180. The default=0.

-n noise ... NOISE is the estimate of the small constant added to the denominator in the division process and represents the noise to signal power ratio. Values are floats>=0. Usually, one simply uses trial an error with an arbitrary small value for the noise, typically, in the range of about 0.001 to 0.0001. However, it can be estimated from the variance of a nearly constant section of the image (to get the noise variance) divided by an estimate of the variance of the whole image (to get the signal variance). Values are floats>=0. The default=0

REQUIREMENTS: IM version 6.5.4-7 or higher, but compiled with HDRI enabled in any quantum level of Q8, Q16 or Q32. Also requires the FFTW delegate library.

LIMITATIONS: This script works well only with even, square images. Otherwise, the FFT will pad them with black to conform. However, there will be excessive ringing due to the color discontinuity associated with the padding. This even, square limitation is a ramification of the current IM implementation that needs addressing at some future time. It is not a limitation of FFTW.

See Fourier Transform with ImageMagick, for more details.

CAVEAT: No guarantee that this script will work on all platforms, nor that trapping of inconsistent parameters is complete and foolproof. Use At Your Own Risk.


EXAMPLES


Removing Horizontal Motion Blur

Input
(see script, camerablur)

Log Enhanced
Internally
Generated
Sinc Filter

Arguments:
-t motion -a 7 -r 0 -n 0



Removing Horizontal Motion Blur With Added Noise

Input
(see script, camerablur)

# create noise
convert -size 128x128 xc:gray +noise random \
-channel green -separate noise_gray.png

# add noise to motion blurred image
convert cameraman2_idealmotion7r0.png noise_gray.png +swap \
-compose mathematics -set option:compose:args "0,1,0.01,-0.005" \
-composite cameraman2_idealmotion7r0_noise.png

Log Enhanced
Internally
Generated
Sinc Filter

 

 

Arguments:
-t motion -a 7 -r 0 -n 0.005

Arguments:
-t motion -a 7 -r 0 -n 0.0005

Arguments:
-t motion -a 7 -r 0 -n 0.00005



Removing Lens Defocus

Input
(see script, camerablur)

Log Enhanced
Internally
Generated
Jinc Filter

Arguments:
-t defocus -a 7 -n 0



Removing Lens Defocus With Added Noise

Input
(see script, camerablur)

# create noise
convert -size 128x128 xc:gray +noise random \
-channel green -separate noise_gray.png

# add noise to motion blurred image
convert cameraman2_idealdefocus7_fast.png noise_gray.png +swap \
-compose mathematics -set option:compose:args "0,1,0.01,-0.005" \
-composite cameraman2_idealdefocus7_fast_noise.png

Log Enhanced
Internally
Generated
Jinc Filter

 

 

Arguments:
-n 0.005

Arguments:
-n 0.0005

Arguments:
-n 0.00005



Removing Lens Defocus From A Real Image

(Subsection Of Photo Provided By Maxim Usatov)

Input

 

Deduce Degradation
From Cepstrum Script

cepstrum script with default arguments
followed by -contrasts-stretch 0x1%

cepstrum result with
red circle of radius=9

Deduce Noise To Signal Power
Ratio From "Flat" Subsection

convert image[150x50+10+10] image -format \
"%[fx:(u.standard_deviation/v.standard_deviation)^2]"
info: | head -n 1

0.00505017

red rectangle used
for subsection

Restore Image

Arguments:
-t defocus -a 9 -n 0.005



What the script does is as follows:

  • Computes the Fourier transform real and imaginary components
    of the image using +fft
  • Creates a Sinc function image for motion blur or
    a Jinc function image for defocus
  • Performs complex number division of the filter with the real and imaginary components
  • Converts the real and imaginary results of the division
    back to the spatial domain

This is equivalent to the following IM commands for horizontal motion blur.

  • width=`identify -ping -format "%w" $infile`
  • height=`identify -ping -format "%h" $infile`
  • w1=`convert xc: -format "%[fx:($width%2)==0?$width:($width+1)]" info:`
  • h1=`convert xc: -format "%[fx:($height%2)==0?$height:($height+1)]" info:`
  • cx=`convert xc: -format "%[fx:floor(($width+1)/2)]" info:`
  • cy=`convert xc: -format "%[fx:floor(($height+1)/2)]" info:`
  • d1=`convert xc: -format "%[fx:max($w1,$h1)]" info:`
  • fd1=`convert xc: -format "%[fx:$amount*pi/$d1]" info:`
  • sinang=`convert xc: -format "%[fx:sin($rotation*pi/180)]" info:`
  • cosang=`convert xc: -format "%[fx:cos($rotation*pi/180)]" info:`
  • qnoise=`convert xc: -format "%[fx:quantumrange*$noise]" info:`
  • convert -size ${w1}x1 xc: \
    -fx "zz=$fd1*(i-$cx)*$cosang; zz?sin(zz)/(zz):1" \
    -scale ${w1}x${h1}\! $tmpF
  • convert \( $infile -alpha off +fft \) \
    \( $tmpF $tmpF -compose multiply -composite -evaluate add $qnoise \) \
    \( -clone 0 $tmpF -compose multiply -composite \) \
    \( -clone 1 $tmpF -compose multiply -composite \) \
    \( -clone 3 -clone 2 +swap -compose divide -composite \) \
    \( -clone 4 -clone 2 +swap -compose divide -composite \) \
    -delete 0-4 +ift -crop ${width}x${height}+0+0 +repage \
    $outfile