<chapter id="refocus">
<chapterinfo>

<title>Refocus a Photograph</title>

<authorgroup>
    <author>
        <firstname>Gilles</firstname><surname>Caulier</surname>
        <affiliation><address><email>caulier dot gilles at free.fr</email></address></affiliation>
    </author>
    <author>
        <firstname>Gerhard</firstname><surname>Kulzer</surname>
        <affiliation><address><email>gerhard at kulzer.net</email></address></affiliation>
    </author>        
</authorgroup>

<abstract>
<para>
The digiKam image plugin <emphasis>Refocus</emphasis> is a tool to refocus an image by enhancing the sharpness. It uses the <emphasis>Deconvolution Filter</emphasis> algorithm copyrighted by Ernst Lippe.
</para>
</abstract>

<keywordset>
<keyword>KDE</keyword>
<keyword>Digikam</keyword>
</keywordset>
</chapterinfo>

<title>Introduction</title>

<para>
Out-of-focus photographs, as well as most digitized images, need correction of sharpness. This is due to the digitizing process that must chop up a color continuum in points with slightly different colors: elements thinner than sampling frequency will be averaged into an uniform color. Thus, sharp borders are rendered a little blurred. The same phenomenon appears when printing color dots on paper. SLR cameras need even more sharpening on a regular basis than consumer cameras.
</para>

<para>
Some scanners apply a sharpen filter while scanning. It's worth to disable it so that you keep control over your image.
</para>

<para>
This tool attempts to "refocus" an image by undoing the defocussing. This is better than just trying to sharpen a photograph. It is employing a technique called <emphasis>FIR Wiener Filtering</emphasis>. The traditional technique for sharpening images is to use unsharp masking. Refocus generally produces better results than Unsharp masking. Start it from the <menuchoice><guimenu>Fix</guimenu><guimenuitem>Refocus</guimenuitem></menuchoice> Image Editor menu.
</para>

<para>
The Refocus technique works differently from <ulink url="help:/digikamimageplugins/unsharp.html">Unsharp Mask</ulink> and is also unlike the <ulink url="help:/digikam/using-kapp-imageeditor.html#using-kapp-adjustsharpness">Sharpen Filter</ulink>  which both increase the contrast of the feature the edges of an image. Refocus rather reverses the process by which the image got blurred by the circular aperture of the camera. This method gives you as much of the original "in focus" image as possible. Refocus uses a very powerful deconvolution algorithm that will reclaim the data that has been mixed up. In mathmatical terms, blurring is usually the result of a convolution, a deconvolution will reverse the process, this is exactly what Refocus is doing. Furthermore, the FIR filter technique allows to remove much of the noise and granularity that often gets accentuated in the sharpening process of all sharpening filters.
</para>

<sect1 id="using-plugin-refocus">
<title>Using the plugin</title>

<figure>
    <title>Refocus Dialog</title>
    <graphic srccredit="Refocus Dialog" fileref="refocusdialog.png"/>
</figure>

<para>
The image panel and the original preview help you to pan within the image. The preview window shows the filter output using the current settings. 
</para>

<para>
In most cases you don't know precisely what convolution caused the image degradation. There are two convolutions that are frequently used to model image degradation: 

<itemizedlist>

  <listitem><para>The Gaussian convolution: this one is mathematically similar to the normal distribution, with its bell-shaped curve. From a theoretical point of view the mathematical justification for using the Gaussian convolution is that when you a apply a large number of independent random convolutions the results will always approach a Gaussian convolution.</para></listitem>
  
  <listitem><para>The circular convolution: this one spreads each source point uniformly across a small disk with a fixed radius. Technically this describes the effects of using a (ideal) lens that is not correctly focused.</para></listitem>

</itemizedlist>

The refocus plug-in supports both the Gaussian and the Circular convolution plus mixtures of both. The actual convolution that is used by the plug-in is in fact the result of convolving both the Gaussian and the circular convolution with one another. Both of these convolutions are identical to the identity convolution when their parameter is equal to 0. Therefore, when the Gaussian radius equals 0, the result is a Circular convolution, and likewise when the radius equals 0 the result is a Gaussian convolution.
</para>

<para>
In practice, in most cases the Circular convolution works much better than the Gaussian convolution. The Gaussian convolution has a very long tail, so mathematically the result of the convolution also depends on source pixels at a large distance from the original source pixel. The FIR Wiener inverse of a Gaussian convolution in most cases is heavily influenced by source pixels at a large distances, and in most cases this produces undesirable results.
</para>

<para>
The Circular convolution generally produces much better results. One reason is that the FIR Wiener inverse of the Circular convolution in general influenced by source pixels in the immediate neighborhood of the original source pixel. Another reason is that a Circular convolution is theoretically a good mathematical model for images that are slightly unfocused.
</para>

<para>
To set correctly the deconvolution filter, the plug-in has the following parameters:

<itemizedlist>

  <listitem><para><guilabel>Circular Sharpness</guilabel>: This is the radius of the Circular convolution filter. It is the most important parameter for using the plug-in. With most images the default value of 1 should give good results. Select a higher value when your image is very blurred, but beware of producing halos.</para></listitem>
  
  <listitem><para><guilabel>Correlation</guilabel>: Increasing the <guilabel>Correlation</guilabel> may help reducing artifacts. The correlation can range from 0-1. Useful values are 0.5 and values close to 1, e.g. 0.95 and 0.99. Using a high value for the correlation will reduce the sharpening effect of the plug-in.</para></listitem> 
  
  <listitem><para><guilabel>Noise filter</guilabel>: Increasing the <guilabel>Noise filter</guilabel> parameter helps reducing artifacts. The Noise can range from 0-1 but values higher than 0.1 are rarely helpful. When the Noise value is too low, e.g. 0 the image quality will be horrible. A useful value is 0.03. Using a high value for the Noise will even blur the image further.</para></listitem>
  
<listitem><para><guilabel>Gaussian Sharpness</guilabel>: This is the radius for the Gaussian convolution filter. Use this parameter when your blurring is Gaussian (mostly due to previous blur filtering). In most cases you should leave this parameter to 0, because it causes nasty artifacts. When you use non-zero values you will probably have to increase the <guilabel>Correlation</guilabel> and/or <guilabel>Noise filter</guilabel> parameters, too.</para></listitem>

  <listitem><para><guilabel>Matrix size</guilabel>: This parameter determines the size of the transformation matrix. Increasing the <guilabel>Matrix Size</guilabel> may give better results, especially when you have chosen large values for <guilabel>Circular Sharpness</guilabel> or <guilabel>Gaussian Sharpness</guilabel>. Note that the plug-in will become very slow when you select large values for this parameter. In most cases you should select a value in the range 3-10.</para></listitem>
  
  <listitem><para><guilabel>Save As</guilabel> and <guilabel>Load</guilabel>: these buttons are used to do just that. Any Refocus parameters that you have set can be saved to the filesystem and loaded later.</para></listitem>

  <listitem><para><guilabel>Reset All</guilabel>: this button resets all settings to default values.</para></listitem>

</itemizedlist>

</para>

<para>

Below, you can see few hints to help you work with the refocus plug-in:

<itemizedlist>

  <listitem><para>Preferrably perform all croping, color and intensity curve corrections on the image before using this plug-in.</para></listitem>
  
  <listitem><para>Otherwise use this plug-in before performing any other operations on the image. The reason is that many operations on the image will leave boundaries that are not immediately visible but that will leave nasty artifacts.</para></listitem>
   
  <listitem><para>When you are scanning images and compress them, e.g. to JPEG, you should use the plug-in on the uncompressed image.</para></listitem> 

</itemizedlist>

</para>

</sect1>

<sect1 id="comparison-plugin-refocus">
<title>Comparison With Other Techniques</title>

<para>
Comparison to two other techniques frequently used to enhance images are:

<itemizedlist>

  <listitem><para><ulink url="help:/digikam/using-kapp-imageeditor.html#using-kapp-adjustsharpness">Sharpen Filter</ulink></para></listitem>
  
  <listitem><para><ulink url="help:/digikamimageplugins/unsharp.html">Unsharp Mask</ulink></para></listitem>
  
</itemizedlist>
</para>

<para>
Sharpening applies a small convolution matrix that increases the difference between a source pixel and its immediate neighbors. FIR Wiener filtering is a more general technique because it allows a much larger neighborhood and better parameterizations. Sharpening only works when your images are very slightly blurred. Furthermore, for high values of the sharpening parameter the results frequently looks "noisy". With FIR Wiener filtering this noise can be greatly reduced by selecting higher values for the <guilabel>Correlation</guilabel> and <guilabel>Noise filter</guilabel> parameters. 
</para>

<para>
Unsharp masking is another very popular image enhancement technique. From a mathematical point of view its justification is a bit obscure but many people are very fond of it. The first step is to creat a blurred copy of the source image. Then the difference between the source image and the blurred image is subtracted from the source image, hence the name unsharp masking. If fact, unsharp masking is more of a contrast enhancement on the important image feature than a sharpening. It does not undo the aperture pattern interference of the camera diaphragm as refocus does.
</para>

<para>
In general, unsharp masking produces better results than sharpening. This is probably caused by the fact that unsharp masking uses a larger neighborhood than sharpening.
</para>

<para>
From a theoretical point of view unsharp masking must always introduce artifacts. Even under optimal circumstances it can never completely undo the effect of blurring. For Wiener filtering it is possible to prove that it is the optimal linear filter. In practice, in all cases the results of the FIR Wiener filter were at least as good as those of unsharp masking. The FIR Wiener filter is frequently better in restoring small details.
</para>

<para>
Below, you can see a comparison of different filter apply on a small unfocused image:

<informaltable><tgroup cols="2">
            
    <thead><row>
        <entry>Preview</entry>
        <entry>Type</entry>
    </row></thead>

    <tbody>
        <row>
            <entry>
            <graphic srccredit="Orignal Blured Image Preview" fileref="refocus-notsharpened.png"/>
            </entry>
        
            <entry>
            Original blured color image to fix. This image have been taken with an analog still camera. The unfocusing result of an insuffisant light for the auto-focus lens.
            </entry>
        </row>

        <row>
            <entry>
            <graphic srccredit="Sharpening Preview" fileref="refocus-sharped.png"/>
            </entry>
        
            <entry>
            Fixed image using simple sharpening filter. Sharpness setting is 80.
            </entry>
        </row>

        <row>
            <entry>
            <graphic srccredit="Unsharp Mask Preview" fileref="refocus-unsharpmask.png"/>
            </entry>
        
            <entry>
            Fixed image using unsharp mask filter. Settings are Radius=50, Amount = 5, and Threshold=0.
            </entry>
        </row>

        <row>
            <entry>
            <graphic srccredit="Refocus Preview" fileref="refocus-refocus.png"/>
            </entry>
        
            <entry>
            Fixed image using Refocus filter. Settings are Circular Sharpness=1.3, Correlation=0.5, Noise Filter=0.020, Gaussian Sharpness=0 and Matrix Size=5.
            </entry>
        </row>
        
    </tbody>
    
</tgroup></informaltable>
                  
</para>

<note><para>
For more information about correction of sharpness methods used in digital imagery, you can found a technical comparison at <ulink url="http://www.bialith.com/Research/BARclockblur.htm">this url</ulink>.
</para></note>

</sect1>

<sect1  id="inaction-plugin-refocus">
<title>The plugin in action</title>

<para>
This is an example of how the Refocus can change your life. The original image is (1) and the corrected image is (2). Refocus settings used to fix this unfocused image are defaults.
</para>

<figure>
    <title>Refocus Tool in Action</title>
    <graphic srccredit="Refocus Tool in Action" fileref="refocuspreview.png"/>
</figure>

</sect1>

</chapter>
<!--
Local Variables:
mode: sgml
sgml-omittag: nil
sgml-shorttag: t
End:
-->

