Introduction

Abstract

Curvature flow equations smoothen images by smoothing their level lines. In contrast to linear smoothing filters, they do not only conserve sharp edges, but also have some remarkable properties like contrast invariance, or affine invariance. This filter resembles some recently discovered approximation schemes that (more or less) inherit many of those properties.

Common smoothing algorithms are often approximations of the heat equation applied to an image — in most cases realized by convolution with some smartly choosen kernel. This kind of smoothing removes (or "smears out") noise, but originally sharp edges become unlocatable. Moreover there are kinds of noise like impulse noise (also called Salt'n'Pepper noise) where the heat equation fails. Nonlinear filters like the well-known Median filter usually give better results. Sharp edges are untouched, but even nasty impuls noise can be removed satisfyingly. The Median filter is an approximation of the mean curvature flow — a mathematical object which smoothes the level lines of an image.

This filter collection provides several approximations of curvature flow filters, including the Median. These filters are realized by combining discrete erosions and dilations (operators that replace pixels with neighbor pixels that are darker or brighter, resp.) with several (nonlinear convolution) kernels, and resemble not only the mean curvature flow, but also the affine curvature flow, which gives usually even better results. The user may choose between a pure morphological approach, where no new color values are created, and an "almost-morphological" approach where the average value of erosions and dilations will be returned.

This plugin is designed to work with AviSynth 2.5 and is able to process all color spaces (RGB, RGBA, YV12, YUY2), although YUV color spaces are recommended.

Terms and Conditions for Execution, Copying, Distribution and Modification

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2, as published by the Free Software Foundation in June 1991 (see 'GNUGPLv2.txt').

There is no guaranty that the rights of third parties (including software patents) are not infringed upon by the execution or distribution of this program. Referring to section 8 of the General Public License, any use of this program (including all activities mentioned in section 0 and including execution) is restricted to countries where any of these activities do not infringe upon any such rights of third parties. It lies with the user to verify the compliance of his or her use (especially concerning but not limited to distribution) of this program with the rights of any third parties in the respective countries or regions.

This program is distributed in the hope that it will be useful, but without any warranty; without even the implied warranty of merchantability or fitness for a particular purpose. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License 2 along with this program; if not, write to the

Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

The mathematical formulars in this document require a HTML 4.0 compatible browser to be displayed correctly.

Usage

Loading the plugin

You need the files AviSynth_C.dll and AVSCurveFlow.dll in your working directory. To load the plugin, your script should start with the following lines:

LoadPlugin("AviSynth_C.dll")
LoadCPlugin("AVSCurveFlow.dll")

Curveature Flow filter functions

CurvatureFlow(clip Clip, Type, string Mode, int "Steps")
MeanCurvatureFlow(clip Clip, int "Steps")
AffineCurvatureFlow(clip Clip, int "Steps")

CurvatureFlow applies a curvature flow smoothing filter to the source clip.

MeanCurvatureFlow(clip Clip, int "Steps") is an abbreviation of CurvatureFlow(Clip,"MCF","MEAN",Steps).
AffineCurvatureFlow(clip Clip, int "Steps") is an abbreviation of CurvatureFlow(Clip,"ACF","MEAN",Steps).

Sample Scripts

This script opens a video file and applies the affine curvature flow. This is always a good first try when denoising a video:

LoadPlugin("AviSynth_C.dll")
LoadCPlugin("AVSCurveFlow.dll")

AVISource("MyMovie.avi")
ConvertToYUY2
AffineCurvatureFlow(2)

This script applies the classical closing operator, which should remove some black dirt (if there is any):

LoadPlugin("AviSynth_C.dll")
LoadCPlugin("AVSCurveFlow.dll")

AVISource("MyMovie.avi")
ConvertToYUY2
CurvatureFlow(8,"C",2)
If there is heavy black dirt, you can try to create a stronger closing operator with this script:
LoadPlugin("AviSynth_C.dll")
LoadCPlugin("AVSCurveFlow.dll")

AVISource("MyMovie.avi")
ConvertToYUY2
CurvatureFlow(8,"D",4)
CurvatureFlow(8,"E",4)

Mathematical Background

In the following Du denotes the gradient of a gray level image u:R2→R, and D2u its Hessian. When the task of smoothing an image comes up a common tool is the heat equation:

ut = Δu = tr(D2u)=D2u⋅(Du,Du)/||Du||2+D2u⋅(Du,Du)/||Du||2
As described in the introduction, this equation has the drawback that it smears out edges which should remain sharp. A way to avoid this is to blur only parallel to edges. Since Du is perpendicular to edges, the modified equation reads:
ut = D2u⋅(Du,Du)/||Du||2 = ||Du|| div(Du/||Du||)
This and some related equations will be studied in this section.

The following remarks aren't mathematically exact, but rather an outline of the details, that are needed to understand the implementation.

Morphological Scale Space Operators

A scale space is a family of image operators Tt which, when applied to an image u, generate a family of images (Ttu)t. In [AGLM], several demands on the operator Tt are described and it is shown that assuming some of these axioms yields scale space operators, which are propagators of partial differential equations.

One of these assumptions is morphology: The operator Tt is translation invariant and commutes with contrast transformations. A contrast transformation g is defined by a nondecreasing function g:R→R and applied to an image u through:

g(u)(x) = g(u(x))

A core result of [AGLM] shows that satisfying some special assumptions, including morphology, Tt is the propagator of this general partial differential equation (which will be referred as (PDE) in the sequel):

ut = ||Du||b(κ)
with κ=div(Du/||Du||) being the curvature of the level lines of u, and b a continuous odd function. One well-known equation of this type is the mean curvature flow:
ut = ||Du||div(Du/||Du||) = ||Du||κ
If moreover Tt is assumed to be affine invariant (i.e., it commutes with affine transformations of images), it necessarily is the evolution operator of the affine morphological scale space:
ut = ||Du||κ1/3

Geometrical Curvature Flow Equations

If an image evolves accordingly to (PDE), the level lines of the image follow the geometric curvature flow equation:

ct = b(κ) N
where κ is the curvature of the level line c and N is the normal vector. This has been shown for the affine morphological scale space by Sapiro & Tannenbaum in [ST, Appendix B].

Approximation Schemes

One way to apply these equations to pixel images is to approximate the right hand side of (PDE) with finite differences. Unfortunatelly it has turned out that there is no way to create a (reasonable) monotone finite difference scheme. In the last years, several approximation schemes for the mentioned equations have been developed following a morphological approach. These filters are compositions of a dilation-type and an erosion-type operator which on their part act on 3×3 pixel blocks. Erosions and dialtions are usually written as

Eu(x) = supB∈F infy∈B u(x+y)    Du(x) = infB∈F supy∈B u(x+y)
with a family of sets F, which precisely defines the behavious of the operators.

In [CDK], such a family F is presented, and it is shown, that a proper combination of the resulting operators is consistent with the mean curvature flow. There is also a natural way presented how this family can be discretizised on the 3×3 pixel block.

In [BB], another family of sets is presented, which is then proven to create an operator consistent with the affine curvature flow. There is also a discretization on the 3×3 pixel block, which results in eight different approximations, depending on a special area parameter that may vary between 0 and 4.5. If this area is below 0.5, the operators become the identity operator. If this area is above 3.5, the corresponding discrete family F only consists of the full 3×3 pixel block, hence the paricular erosion and dilation operator represent the full (classical) morphological erosion and dilation, respectively. As numerical experiments in [BB] show, choosing an area of 1.5 yields best results when denoising impulse noise.

Another morphological approximation scheme for the mean curvature flow is the median operator. The discretization is obvious: Replace each pixel by the median value of its 3×3 neightborhood. This operator can be interpreted as dilation and erosion as well.

Fast Implementation

All discrete (3×3 pixel) erosions and dilations choose one of nine pixels and replace the center pixel with this new value. This choice only depends on the order of the values. To exploit this fact, the implementation prepares a look-up table for each morphological operator where all possible orders are listed together with their respective choice. When a pixel is processed, the order of its neighborhood values is determined and the table is used to determine which value is the new one of the pixel. In the actual implementation as AviSynth plugin, these tables are built upon invokation of the filter function in the script. A table once built will not be build again until the script is closed (and the script environment is destroied). If another filter in the same environment needs a table which has already been built, this table will be reused.

To further reduce the computational effort of sorting pixels, two pixel blocks (twelve pixels) are always processed toghether (what doubles the size necessary for a look-up table).

Only grayscale images have been considered so far. When a YUV image is processed, the choice described above is made on the basis of the Y channel. Then all channels are updated based on this choice. Hence luma and chroma values are transported the same way. In contrast to this, each channel of an RGB image will be treated separately, so in general different color spaces of equal images can give completely different results.

References

[AGLM] Luis Alvarez, Frédéric Guichard, Pierre-Louis Lions, Jean-Michel Morel: Axioms and Fundamental Equations of Image Processing, Archive for Rational Mechanics and Analysis 123(3), pp.199-257, 1993.
[BB] Wolfgang Boiger, Folkmar Bornemann: Local Morphological Schemes for the Affine Morphological Scale Space, Preprint, 17pp, Technische Universität München (March 2006).
[CDK] Francine Catté, Françoise Dibos, Georges Koepfler: A morphological scheme for mean curvature motion and applications to anisotropic diffusion and motion of level sets, SIAM Journal on Numerical Analysis 32(6), pp.1895-1909, 1995
[ST] Guillermo Sapiro, Allen Tannenbaum: Affine Invariant Scale-Space, International Journal of Computer Vision 11(1), pp.25-44, 1993.

Version History

2006.01.25 Initial Version.
2007.03.06
  • GetFrame function rewritten. Size of binary reduced by 0.5 kB.
  • Typos in Demo.avs corrected.
  • Typos in AVSShock.htm corrected.
  • Added filter name to error messages.
  • Changed error handling in filter create function to avoid multiple memory allocation.
First public version.
Wolfgang Boiger