DeSpot
- Conditional Temporal Spot Removing Filter
С-plugin for Avisynth 2.5
Version 1.0 December
30, 2003
- with temporal noise reduction.
Copyright (C)2003 Alexander G. Balakhnin aka Fizick.
http://bag.hotmail.ru
This filter is based on Conditional Temporal Median Filter for Avisynth 2.5
version 0.93, September 27, 2003
Copyright (C) 2003 Kevin Atkinson (kevin.med@atkinson.dhs.org) under
the GNU GPL version 2.
http://kevin.atkinson.dhs.org/temporal_median/
I rename the my new filter version to more appropriate name DeSpot,
and add some functions, of course
:-).
This filter is designed to remove temporal noise in the form of small
dots (spots) and streaks found in some videos. A common cause of this is
dirty VHS heads but we have also seen small black or white streaks
in broadcast material.
The filter is useful for cleaning of old telecined 8mm films
from spots and some stripes.
Example images
Used Avisynth example script with parameters as in base filter version:
Avisource("film8mm.avi")
loadplugin("avisynth_c.dll")
loadCplugin("despot.dll")
converttoYV12()
crop(0,0,720,288)
# Compare half-frames with and without noise reduction
StackVertical(last,last.DeSpot(p1=35,p2=14,pwidth=70,pheight=70,mthres=25,mwidth=20,mheight=15,mp=100,interlaced=false))
converttoYUY2()
One half frame from my old 8mm film (top - before, bottom -after the filter):
The same frame after new filter version (top half - with spot border blur, bottom half - with temporal smoothing in static areas):
Used script for new filter version:
Avisource("film8mm.avi")
loadplugin("avisynth_c.dll")
loadCplugin("despot.dll")
converttoYV12()
crop(0,0,720,288)
# Compare frames with blur, and with and without temporal noise reduction
StackVertical(last.despot(p1=35,p2=14,pwidth=70,pheight=70,mthres=25,mwidth=20,mheight=15,mp=100,interlaced=false,blur=4),
last.despot(p1=35,p2=14,pwidth=70,pheight=70,mthres=25,mwidth=20,mheight=15,mp=100,interlaced=false,blur=4,tsmooth=5))
converttoYUY2()
More info
USAGE
The filter requires AVISynth 2.5, and AviSynth C API Plugin version
0.14 or better by Kevin Atkinson (K.A.). The required DLL "avisynth_c.dll"
is included. The full version can be found at at
http://kevin.atkinson.dhs.org
After the AviSynth C Plugin is loaded (by putting it in the Plugin
directory or using LoadPlugin) the filter needs to be loaded using
LoadCPlugin (with path to filter):
LoadCPlugin("despot.dll").
With AVISynth version 2.53, the avisynth_c.dll is no more needed,
and any C-plugin may be loaded as ordinary plugin, simply by:
LoadPlugin("despot.dll")
Basic Usage:
DeSpot(clip, <parms>) OR
DeSpotMedian(clip, <parms> )
(without corner brackets, of course...)
The input MUST be YV12.
DeSpot will attempt to identify noise and eliminate it while.
DeSpotMedian will simply apply a simple temporal median filter to the
non-moving areas of the image.
Parms for DeSpot are any of the following
p1 (default 24)
p2 (default 12)
A pixel needs to be at different from its neighbors by at least 'P1'
in order for it to be considered noise. The surrounding pixels must
be different by at lease 'P2' in order for the pixel to be
considered part of the same spot.
pwidth (default 16)
pheight (default 5)
A spot can be no larger than PWIDTH x PHEIGHT
sign (default 0)
Set mode for removing of only black or white spots or both:
sign=0 - any spots and outliers (default)
sign=1 - only black (dark) spots and outliers
sign=2 - only white (light) spots and outliers
sign=3 - only black (dark) spots, any outliers
sign=4 - only white (light) spots, any outliers
fitluma (true or false, default=false)
Enables some frame luminosity correction in places of deleted spots
blur (from 0 to 4, default 0)
Value (length) of local spatial blur near borders of deleted spots
tsmooth (from 0 to 127, default 0)
Control temporal smoothing in static areas (except spots and motion).
Set approximate threshold of pixel luma variance in 3 frames,
The more variance exceed this threshold, the less temporal smoothing.
0 - no temporal smoothing.
Parms for both DeSpot and DeSpotMedian are:
mthres (default 16)
A pixel needs to be different from the previous frame by at least
'MTHRES' in order to be considered moving. This number should
be larger than 'P2' in order to prevent noise from being identified
by motion.
mwidth (default 7)
mheight (default 5)
mp (default mwidth*mheight/3)
These control the behavior of the the motion map denoising algorithm.
interlaced (boolean)
Whether to treat the video as interlaced or not. The default depends
on id AviSynth thinks the video is Field based (ie interlaced) or
Frame based.
To instead show a motion map and noise that would of been eliminated use:
DeSpotMap(clip, <parms>, [show_chroma = false])
DeSpotMedianMap(clip, <parms>, [show_chroma = false])
The luma is changed as follows:
255 (White): Noise that will be removed
159: Noise that won't be remove because it might be motion
95: Motion map for the current image
63: Motion map for the next frame
To just highlight the noise instead of removing it use
DeSpotMark(clip, <parms>, [mark_v = 255])
Where mark_v is the luma value to highlight the noise with
TUNING THE PARMS
In order for the filter to work right the various parameters MUST be
set correctly. There is no good default values.
The first parameter that needs to be set is interlaced, set it to true
if your video is interlaced, false otherwise.
Than pwidth and pheight need to be set. Set these to be slightly
larger than the specks you want to eliminate. If your video is
interlaced than height represents the height of an individual field.
Thus, it will essentially be doubled.
Than p1, p2, and mthres need to be set. In general, p1 > mthres > p2.
If these are set too low than you may lose detail as small pixel
variations might be mistaken as specks, thus losing detail, and more
importantly, real specks might not be recognized as the size of the
filter thinks the spec is might be larger than pwidth by pheight.
DeSpotMark, and DeSpotMap might be helpful in
setting these parameters.
Than "sign" may be set, if almost all spots are only white or only black.
Correct tuning reduces number of false spot detections and artifactes.
If frames brightness are variable (flickered), so brightness in spaces
of deleted spots are different from background, then try to enable
its correction by parameter fixluma.
To reduce noticeability of some borders in places of deleted spots ,
tune local spatial smoothing by parameter blur.
For denoising of static areas, try to use temporal smoothing,
with tsmooth parameter about 4-8.
HOW IT WORKS
The filter works as follows:
1) Find pixes that are different from its neighbors by at least P1.
(If sign parameter is not 0, the sign of difference is also taken into account)
1b) Enlarge outliers based on P2.
2) Determine the size of the specks and reject all those
larger than PWIDTHxPHEIGHT
3) Find moving areas of an image by simply comparing each
pixel to the previous frame and considering all those which
are greater than MTHRES.
3b) Remove pixels determined as noise from the motion map.
4) Denoise the motion map by erosion and then dilating
(i.e. morphological opening).
This is probably the most important step.
5) Only remove the specks in which there was no motion in
the current or the next frame.
6) Optional make spatial smoothing of spot borders.
7) Optional make temporal smoothing of static areas (Wiener filter).
The filter can also be configured to work as follows (Median mode):
1) Find moving areas of an image by simply comparing each
pixel to the previous frame and considering all those which
are greater than MTHRES.
2) Denoise the motion map by erosion and then dilating
(i.e. morphological opening).
This is probably the most important step.
3) Apply a simple temporal median filter on the non-moving areas of
the image.
OPTIMIZATION NOTES
Fizick's version of filter is NOT SSE optimized yet.
Original filter by Kevin Atkinson is integer SSE optimized
using either GCC or Intel MMX/SSE builtins.
Unfortunately Gcc versions before 3.3 does not use them very
well. There for it is recommended that either GCC 3.3+ or Intel's
compiler is used. Even GCC 3.0 and Intel's compiler do not generate
optimal code when using MMX/SSE builtins, in particular GCC 3.3
interests a few unnecessary movq. Thus hand optimizing some of the
code might be beneficial.
COMPILING
To compile this filter you will need to install Gcc and perhaps GNU Make.
K.A. used MinGw (2.0.0-3) with MSYS 1.09, Gcc 3.3.1.
Fizick's version was compiled by GCC-g++ 3.3.1, MinGW 3.0.0-1, MSYS 1.09.
All of the required utilities can be found at
http://www.mingw.org/download.shtml.
Other configurations should work, but you may need to edit the
Makefile. Once all the proper tools are installed and in the path,
simply type:
make
from the MSYS shell and that is all that should be required.
COMBINED USAGE
Very good results are may be obtained with combined this filter
with motion estimation and compensation, even if only general motion,
produced my (Fizick) GENMOTION plugin.
LICENSE
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
VERSIONS CHANGES LOG AND DOWNLOADS
Changes 1.0 from 0.934 (Dec 30, 2003)
Added "tsmooth" parameter for temporal smoothing in static areas.
Changed filter name to DeSpot, filter file name to despot.dll,
and function names:
ConditionalDenoise to DeSpot,
ConditionalDenoiseMark to DeSpotMark,
ConditionalDenoiseMap to DeSpotMap,
ConditionalMedian to DeSpotMedian,
ConditionalMedianMap to DeSpotMedianMap.
Corrected info.
(Fizick)
Download DeSpot version 1.0 with source
Changes 0.934 from 0.93c (Dec 20, 2003)
Added "fitluma" and "blur" parameters to reduce noticeability
of deleted spots places.
Remove "per" parameter used previously for that.
New default value of "mp" parameter.
(Fizick)
Changes 0.93c from 0.93b (Nov 30, 2003)
More short filter name ctmedian.dll
Added version info to DLL
(Fizick)
Changes 0.93d from 0.93b (Nov 13, 2003)
"per" parameter for more smoothed specks perimeter.
(Fizick, non public)
Changes 0.93a from 0.93 (Nov 7, 2003)
"sign" parameter for removing only black or white specks.
non optimized general version only
(Fizick, non public)
Changed from 0.92 (Sep 27, 2003)
Fix another nasty bug.
Included non optimized version.
Expanded the manual a bit
(Kevin Atkinson)
Changes from 0.91 (Sep 10, 2003)
Fixed nasty bug.
(Kevin Atkinson)