DeScratch -  Scratches Removing Filter
Plugin for Avisynth 2.5

Version 0.5 July 31, 2004
Copyright (C)2003-2004 Alexander G. Balakhnin aka Fizick.
http://bag.hotmail.ru

This plugin  removes vertical scratches from films.

Also it may be used for removing of horizontal noise lines such as drop-outs from analog VHS captures

(after rotation of image).

 

Examples

Old scratched 8 mm film. Top half frame - before filter , bottom half frame - after filter

 


How it works

The plugin firstly detects scratches, then removes its.
It works with every frame, uses spatial information only from current frame.
I adapt it to restoration of my old 8 mm films, may be it will be useful to somebody else.


Scratch detecting

Apply some vertical blur to frame copy, for suppression of image thin
structure, inclined lines and noise.
Search for local extrems of pixel values in every row, with some threshold difference criterion.
Put these extrems to some map (frame).
Optional close vertical gaps in extrems by vertical expanding of extrems points.
Test extrems map with length and angle criterions, so select real scratches.

Scratch removing

Scratches may be either partially transparent, smooth (with image details),
or opaque (with no details or almost destroyed).
In the first case, plugin can subtract smooth (blurred) part of scratch pixels
variation from original image. Therefore, image details are kept .
In the second case, plugin replaces scratched pixels by mean values of
some neighbours pixels (in same row).
We have also intermediate case by setting some percent of detail to keep.
The plugin have some adaptation of keeped value according to pixels difference.
In all cases, some nearest neighbours may also partially changed for more smoothing.
 

Needed software
The program is plugin (filter) for free  Avisynth 2.5  (http://www.avisynth.org)

Tested with version 2.55beta (some versions have bug with Turnleft() !).

Filter is not more needed in Avisynth C Interface (avithynth_c.dll) by Kevin Atkinson.

Compiled with  MS VC++ Toolkit 2003.


Sample script for Avisynth (used for sample clip above):

Avisource("input.avi")
loadplugin("descratch.dll")
converttoYV12()
descratch(mindif=2)


Plugin Parameters:

mindif - minimal difference of pixel value in scratch from neighbours pixels for luma plane
            (from 1 to 255, default 5):
maxgap - maximal vertical gap to be closed (from 0 to 255, default 3):
maxwidth - maximal scratch width ( fixed to default=3 in current version)
minlen - minimal scratch length (default = 100)
maxangle - maximal angle to vertical (float in degrees, default=5.0)
blurlen - radius of vertical blur for frame analysis (default = 15)
keep - percent of scratch detail to keep (default = 100)
border - thickness of border near scratch for partial restoration (default=2)
modeY - processing mode for luma  plane (0 - no, 1 - low(black), 2 - high(white), 3 - both, default=1)

modeU - processing mode for chroma U plane (0 - no, 1 - low(green), 2 - high(red), 3 - both, default=0)

modeV - processing mode for chroma V plane (0 - no, 1 - low(yellow), 2 - high(blue), 3 - both, default=0)

mindifUV - minimal difference of pixel value in scratch from neighbours pixels for chroma planes
            (from 0 to 255, default 0):
            if =0, then internal mindifUV value is same as mindif .
mark - mark scratch in debug mode (true or false, default =false)
            ( set rejected extrems pixels to grey, set scratch pixels to lowest or highest values)


(maxgap, maxwidth, minlen, blurlen, border - in pixels.)

You MUST tune parameters for your video.

Use Avisynth commands Greyscale(), UtoY(), VtoY() , and."mark" parameter for debug and tuning.
 

Horizontal noisy lines removing

 

Such long thin noisy lines sometimes appears in analog capture material (TV or VHS line drop-out, etc).

This problem was discussed at Doom9, see article Removal of clicks and scratches (July 10-31, 2004).

But in article DeSpot plugin was firstly  used, what is not most appropriate tool for this, due to very weak noise level.

DeScratch could be more effective, however is not ideal too (old version can process only luma plane).

Of course, you must rotate clip for processing with Descratch (and do not forget to rotate it in reverse direction after cleaning :-).

See results for some VHS source (from Ivo).

Top half frame - before processing , bottom half frame - after processing:


 

    Script used for example clip (with optimal Descratch parameters - big minlen, null maxangle):

 

loadplugin("descratch.dll")
Avisource("drop-outs_.avi")
converttoYV12()
crop(0,0,-0,288) # select top part of frame to save screen space
input=last
assumetff()

separatefields() # for analog interlaced source
turnleft()
descratch(mindif=4,maxgap=20,minlen=300,blurlen=50,keep=100,border=0,maxangle=0)
turnright()
weave() # restore fields
# Compare source frame and frame after scratch removing
StackVertical(input,last)

 

For color line dropouts removing, you may use modeU, modeV and mindifUV parameters.


 

License


This program is FREE software for non-commercial using, but WITHOUT ANY WARRANTY.
Please consider to make some donation.


Version changes log and downloads:
 

Version 0.5, July 31, 2004

    fixed read-write pointer bug,
    add "mindifUV" parameter for separate control of chroma U,V planes cleaning,

    some code reorganization,

    documentation corrected.
Download DeScratch version 0.5

 

Version 0.4, July 17, 2004

     Added chroma processing: modeY, modeU, modeV parameters,

    introduced in version 0.4 after Ivo's request (old versions can process luma only).

    mindif is now positive only,
    marked scratches value is not inverted but contrasted now.
Download DeScratch version 0.4

 

Version 0.3, July 14, 2004 - ported to native Avisynth plugin,
Speed increased due to fast blurring by Avisynth resizing functions.
Download DeScratch version 0.3


version 0.2, 31 December 2003 - change to sharp extrems, add gaps closing
Download DeScratch version 0.2

 

version 0.1, 22 December 2003 - initial beta (not public)
 


Current version limitations:

Maximal scratch width is fixed to 3.
The plugin works only in YV12.
 

Return to main page