Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage

Reply
 
Thread Tools Search this Thread Display Modes
Old 13th February 2011, 00:45   #1  |  Link
prokhozhijj
Registered User
 
Join Date: Feb 2009
Posts: 48
Local Deflicker

I've made local deflicker based on Didée idea (I took it from this post http://forum.doom9.org/showthread.ph...ight=Deflicker)

This deflicker can deflick part of frame.

Usage very simple. You run it as follows

Code:
 dest=dest.LDeflicker()
There is the code

Code:
#based on Didée idea from this post http://forum.doom9.org/showthread.php?t=149465&highlight=Deflicker

function LDeflicker(clip o, int "lumatg1", int "lumatg2", int "blury", int "blurc", int "lumatl1")
{
	lumatg1 = default(lumatg1,100) #luma treshold for first pass of soften in GlobalDeflicker
	lumatg2 = default(lumatg2,lumatg1/2) #luma treshold for second pass of soften in GlobalDeflicker

	blury = default(blury,115/4) # blur strength for luma in LocalDeflicker
	blurc = default(blurc,115/4) # blur strength for chroma in LocalDeflicker
	
	lumatl1 = default(lumatl1,4) #luma treshold for first pass of soften

	res=o.GlobalDeflicker(lumatg1, lumatg2)

	res=res.LocalDeflicker(blury, blurc, lumatl1)

	return res
}


# =============================================================================
# Local Deflicker
# It affects only part of frame
# =============================================================================
function LocalDeflicker(clip o,int "blury", int "blurc", int "lumat1")
{
	res=o

# Motion search	
	super=res.MSuper(rfilter=4)
	
	prefiltered = medianblurt(  res , radiusy=1 ,   radiusu=0 ,   radiusv=0 ,   temporalradius=3 , mc=false , calcborder=true)
	superfilt = prefiltered.MSuper(rfilter=4) 

	lambdav=1000
	backward_vec1 = MAnalyse(superfilt, isb=true, delta=1, search=4, searchparam=2, lambda=lambdav)
	forward_vec1 = MAnalyse(superfilt, isb=false, delta=1, search=4, searchparam=2, lambda=lambdav)
	
	thSCDv=500
	backward_comp1 = MFlow(res, super, backward_vec1, thSCD1=thSCDv)
	forward_comp1  = MFlow(res, super, forward_vec1, thSCD1=thSCDv)

	res=interleave(forward_comp1, res, backward_comp1)

# Source bluring
	screen = res.gaussianblur(varY=blury, varC=blurc, border=3) #.greyscale

# Source deflickering and debanding
	screenTS = screen.Soften2(lumat1).GradFun2DBmod(thr=1.5,mask=true,mode=3,temp=100,str=4,adapt=-1,radius=3,range=30)

# Find the difference in luma between source and deflickered clip 
	calmed   = res.mt_makediff(mt_makediff(screen,screenTS),U=2,V=2)
	
# Simple brightness mask for the range that actually is concerned
	masky    = res.mt_lut("x 32 < x 16 - 16 * x 204 > 255 204 x - abs 255 30 / * - 255 ? ?").removegrain(11,-1)
	
# Merge-in the deflickered result by that mask
	final_dumb = res.mt_merge(calmed,masky,U=2,V=2).SelectEvery(3,1)

# We will not process frames when scene schanges
	o_curr = o
	p_curr = final_dumb
	final_dumb=SCSelect(o_curr , o_curr , o_curr , p_curr, dfactor=2.0)

	return final_dumb

}

# =============================================================================
# Global defliker
# It affects whole frame
# =============================================================================
function GlobalDeflicker(clip o, int "lumat1", int "lumat2")
{
	lumat1 = default(lumat1,100) #luma treshold for first pass of soften
	lumat2 = default(lumat2,lumat1/2) #luma treshold for second pass of soften

	res=o

# Average brightness calculating for the whole frame
	screen   = mt_lutf(res,res,mode="average",yexpr="x",U=1,V=1) #.greyscale

# Deflickering for intermaedite clip
	screenTS = screen.Soften1(lumat1,lumat2) 
	
# Find the difference in luma between source and deflickered clip 
	calmed   = res.mt_makediff(mt_makediff(screen,screenTS),U=2,V=2)
	
# Simple brightness mask for the range that actually is concerned
	masky    = res.mt_lut("x 32 < x 16 - 16 * x 204 > 255 204 x - abs 255 30 / * - 255 ? ?").removegrain(11,-1)

# Merge-in the deflickered result by that mask
	final_dumb = res.mt_merge(calmed,masky,U=2,V=2)

	return final_dumb

}

function Soften1(clip clp, int "luma1", int "luma2")
{
	res = clp
    luma1 = default(luma1, 8)
    luma2 = default(luma1, luma1/2)


	# Luma averaging between 1 and 3 frames
	#         TemporalSoften(clip, int radius, int luma_threshold, int chroma_threshold, int "scenechange", int "mode")
	res = clp.temporalsoften(               1,              luma1,                    0,                18,          2)
	res = res.merge(clp,0.249)
	res = res.temporalsoften(               3,              luma2,                    0,                 9,          2)
     
	return res
}

function Soften2(clip clp, int "luma1")
{
	res = clp
	res = medianblurt( res, radiusy=luma1 ,   radiusu=0 ,   radiusv=0 ,   temporalradius=4 , mc=false , calcborder=true , markscenechange=false)
	
	return res
}
Required plug-ins:
===========
  • MV Tools 2
  • Mask Tools 2
  • VariableBlur 0.4 with neuron2 patch
  • Gradfun2db 1.0
  • AddGrainC 1.5
  • GradFun2DBmod 1.5
  • MedianBlur 0.8.4.1
  • RemoveGrain pre1.0
  • RemoveDirt 0.8

Possible artefacts that script can produce:
- tracking in scenes with fast motion. The reason is in probably incorrect prefiltering for motion search and insufficient motion compensation. I've tried to get rid tracking on fast scenes with MMask from MVtools2 but with no success.
- banding on smooth sources. This is due to preliminary blurring in LocalDeflicker function (screen variable).

There is source clip in lossless format (about 79Mb) http://www.mediafire.com/?7dhb66xphha6whh

PS. I am newbie.

Edit1: 20110227
- Added scene change detection (thanks to Didée);
- Changed some blurring parameters (they are located in very beginning of script. You can play with them);
- Motion estimation is calculated now on prefiltered clip for more accuracy
- border=3 in LocalDeflicker.

Edit2: 20110228
- Changed functions naming
__________________
CPU: Core 2 Quad q9505 RAM: 4G OS: Windows 7 64 Video: Nvidia Geforce 460 GTX VP4 AviSynth: 2.5.8

Last edited by prokhozhijj; 28th February 2011 at 14:47.
prokhozhijj is offline   Reply With Quote
Old 14th February 2011, 00:15   #2  |  Link
Lyris
Registered User
 
Join Date: Sep 2007
Location: Europe
Posts: 602
Can you tell me from where I can get the function "GaussianBlur"? Google's not turning anything up...
Lyris is offline   Reply With Quote
Old 14th February 2011, 00:32   #3  |  Link
Gavino
Avisynth language lover
 
Join Date: Dec 2007
Location: Spain
Posts: 3,431
It's part of the VariableBlur plugin.
__________________
GScript and GRunT - complex Avisynth scripting made easier
Gavino is offline   Reply With Quote
Old 15th February 2011, 23:13   #4  |  Link
prokhozhijj
Registered User
 
Join Date: Feb 2009
Posts: 48
So. No any comments or suggestions?
__________________
CPU: Core 2 Quad q9505 RAM: 4G OS: Windows 7 64 Video: Nvidia Geforce 460 GTX VP4 AviSynth: 2.5.8
prokhozhijj is offline   Reply With Quote
Old 16th February 2011, 01:02   #5  |  Link
TheProfileth
Leader of Dual-Duality
 
TheProfileth's Avatar
 
Join Date: Aug 2010
Location: America
Posts: 134
Quote:
Originally Posted by prokhozhijj View Post
So. No any comments or suggestions?
I want to give this a shot just give me a couple minutes
__________________
I'm Mr.Fixit and I feel good, fixin all the sources in the neighborhood
My New filter is in the works, and will be out soon
TheProfileth is offline   Reply With Quote
Old 18th February 2011, 01:35   #6  |  Link
Lyris
Registered User
 
Join Date: Sep 2007
Location: Europe
Posts: 602
Just tried it - impressive! Except for the fact that it produces a strange sort of glowing effect on the last frame of each scene, and the first frame of the next. Can you fix that?
Lyris is offline   Reply With Quote
Old 18th February 2011, 06:41   #7  |  Link
prokhozhijj
Registered User
 
Join Date: Feb 2009
Posts: 48
Quote:
Originally Posted by Lyris View Post
Just tried it - impressive! Except for the fact that it produces a strange sort of glowing effect on the last frame of each scene, and the first frame of the next. Can you fix that?
I think that issue exists because of incorrect scene detection in some cases.

Try to play with temporalradius=4 in DedeeSoften2 function. May be it helps.
__________________
CPU: Core 2 Quad q9505 RAM: 4G OS: Windows 7 64 Video: Nvidia Geforce 460 GTX VP4 AviSynth: 2.5.8
prokhozhijj is offline   Reply With Quote
Old 18th February 2011, 17:17   #8  |  Link
Lyris
Registered User
 
Join Date: Sep 2007
Location: Europe
Posts: 602
Setting that to "0" does solve the problem, but it also seems that it kills the flicker correction - I'll need to do more thorough tests when I have time. It's often quite hard to tell if flicker has been fixed unless it's seriously bad.
Lyris is offline   Reply With Quote
Old 18th February 2011, 21:21   #9  |  Link
prokhozhijj
Registered User
 
Join Date: Feb 2009
Posts: 48
Quote:
Originally Posted by Lyris View Post
Setting that to "0" does solve the problem, but it also seems that it kills the flicker correction - I'll need to do more thorough tests when I have time. It's often quite hard to tell if flicker has been fixed unless it's seriously bad.
All local deflickering is located in DedeeSoften2 procedure. medianblurt plugin can works with MVTools directly. But it seems with version 1 only. May be medianblurt can be replaced with some other luma smoother. I am newbie in Avisynth so I don't know all good plugins in that area. Sorry.

You can test if deflicker is still in effect if you compare screen and screenTS clips in LocalDeflicker function. screen is the clip before deflickering, and screetTS - after.

EDIT1. It looks like I understood where the problem is. If we can split somehow original clip on separate scenes and feed every scene to LocalDeflicker function then glowing artefact on scenes border will disappear. But for now I don't know how to do it better.
__________________
CPU: Core 2 Quad q9505 RAM: 4G OS: Windows 7 64 Video: Nvidia Geforce 460 GTX VP4 AviSynth: 2.5.8

Last edited by prokhozhijj; 18th February 2011 at 21:51.
prokhozhijj is offline   Reply With Quote
Old 18th February 2011, 21:47   #10  |  Link
Lyris
Registered User
 
Join Date: Sep 2007
Location: Europe
Posts: 602
Actually, I've just ended up using LocalDeflicker, since that is adequate for this source. Many thanks!
Lyris is offline   Reply With Quote
Old 25th February 2011, 16:04   #11  |  Link
Lyris
Registered User
 
Join Date: Sep 2007
Location: Europe
Posts: 602
OK, scratch that. I'd really love to be able to use the entire DeDeeDeflicker() command, but can't because of the fact that it causes artefacting on the first and last frames of a scene.

Is there any way of writing a script which detects the scene changes, so we can turn the filter off on these frames? Or better yet, incorporate it into the function so we can fix the issue?
Lyris is offline   Reply With Quote
Old 25th February 2011, 17:08   #12  |  Link
Didée
Registered User
 
Join Date: Apr 2002
Location: Germany
Posts: 5,389
http://forum.doom9.org/showthread.ph...19#post1140719

__________________
- We´re at the beginning of the end of mankind´s childhood -

My little flickr gallery. (Yes indeed, I do have hobbies other than digital video!)
Didée is offline   Reply With Quote
Old 26th February 2011, 18:45   #13  |  Link
Lyris
Registered User
 
Join Date: Sep 2007
Location: Europe
Posts: 602
Thanks DiDee!

Suggestion:

I found that using the function causes the edges of the frame to be shadowed, even if all borders are cropped off prior to applying the deflicker.

Changing border=0 to border=3 in the GaussianBlur function fixes this (it's part of the LocalDeflicker function).

I changed

Code:
# Source bluring
	screen = res.gaussianblur(varY=blury,varC=blurc,border=0) #.greyscale
to


Code:
# Source bluring
	screen = res.gaussianblur(varY=blury,varC=blurc,border=3) #.greyscale
and fixed this.

The remaining problem is that banding is now visible. If you Alter the LocalDeflicker function to return "screen" instead of "final_dumb" and pull the brightness up a little, we can see that the blurred image has horrible contouring which gets merged into the final deflickered output. I don't have a solution for this and sadly can't share the example clip... but imagine a scene from a black and white clip with a black background and a girl lit (appearing white) in front of it. On the deflickered clip, the flicker is gone (yay!) but there is now a sort of pulsating contoured glow around her head and body.

Last edited by Lyris; 26th February 2011 at 19:15.
Lyris is offline   Reply With Quote
Old 26th February 2011, 21:00   #14  |  Link
Lyris
Registered User
 
Join Date: Sep 2007
Location: Europe
Posts: 602
Example images (from credits):

Before deflicker


Uploaded with ImageShack.us

After deflicker


Result of "return screen" showing contours exist at this stage


edit: I tried patching Gradfun2dbmod into the script - messy, I'd rather avoid banding in the first place than try to clean it up later... but it didn't do much good. It almost eliminated contouring in the "return screen", but not in the final result. Dark areas in the video still have a "tree rings" look to them.

Last edited by Lyris; 27th February 2011 at 01:32.
Lyris is offline   Reply With Quote
Old 27th February 2011, 16:40   #15  |  Link
prokhozhijj
Registered User
 
Join Date: Feb 2009
Posts: 48
Script has been changed. Please see first post for details.
__________________
CPU: Core 2 Quad q9505 RAM: 4G OS: Windows 7 64 Video: Nvidia Geforce 460 GTX VP4 AviSynth: 2.5.8
prokhozhijj is offline   Reply With Quote
Old 27th February 2011, 18:30   #16  |  Link
Lyris
Registered User
 
Join Date: Sep 2007
Location: Europe
Posts: 602
Thanks for the latest update. Do we have any better way to remove the banding other than throwing grain into the affected areas? This is not good for compression.

It still also creates artefacting which almost makes the film look like it is water damaged... it's frustrating because other than these two issues, it makes a huge improvement.

before



after




Can you tell me, is there any way to use an effective single-frame (Global) deflicker? I've tried running GlobalDeflicker on its own but it doesn't do a lot of good. For some reason, #ing out the LocalDeflicker part of the "DeDeeDeflicker" function makes the flicker worse.

Last edited by Lyris; 27th February 2011 at 18:35.
Lyris is offline   Reply With Quote
Old 27th February 2011, 21:47   #17  |  Link
prokhozhijj
Registered User
 
Join Date: Feb 2009
Posts: 48
Quote:
Originally Posted by Lyris View Post
Thanks for the latest update. Do we have any better way to remove the banding other than throwing grain into the affected areas? This is not good for compression.

It still also creates artefacting which almost makes the film look like it is water damaged... it's frustrating because other than these two issues, it makes a huge improvement.
I've implemented the first found solution for debanding. I'll try to find something else. But it'll take some time. Maybe someone from Avisynth gurus can help. I am just newbie.

EDIT1: http://forum.doom9.org/showthread.ph...18#post1386418 in this post some expert says you can not get a clear video without banding. I am just blur a frame (screen variable) before deflickering it. So that is the reason of banding. Please try to move GradFun2DBmod(thr=1.5,mask=true,mode=3,temp=100,str=4,adapt=-1,radius=3,range=150) from screenTS to screen in LocalDeflicker. And also try to play with "range" and "mode" parameters. Maybe it helps. I've not changed my mind to find a better solution. But I am not sure if I can.

Quote:
Originally Posted by Lyris View Post
Can you tell me, is there any way to use an effective single-frame (Global) deflicker? I've tried running GlobalDeflicker on its own but it doesn't do a lot of good. For some reason, #ing out the LocalDeflicker part of the "DeDeeDeflicker" function makes the flicker worse.
Deflicker consists from two parts. Global - for the whole frame, it prepares frame for the following part of deflicker, i.e. local. Local deflicker tries to allign luma in different parts of frame. But for the good results it is needed that global luma has been aligned properly.

Of course you can use something else instead of GlobalDeflicker function. I've tried all deflickers I could find. But Didée's deflicker was one of the best in this area. So I've used it in my Deflicker.
__________________
CPU: Core 2 Quad q9505 RAM: 4G OS: Windows 7 64 Video: Nvidia Geforce 460 GTX VP4 AviSynth: 2.5.8

Last edited by prokhozhijj; 28th February 2011 at 11:09.
prokhozhijj is offline   Reply With Quote
Old 27th February 2011, 23:01   #18  |  Link
Lyris
Registered User
 
Join Date: Sep 2007
Location: Europe
Posts: 602
Still no success, the banding stays... I'm only beginning to get my feet wet with actual function making/altering too, so if anyone else can weigh in, I'd really appreciate it...

The other deflicker filters I've tried don't really have much effect. Is this consistent with other people's findings?
Lyris is offline   Reply With Quote
Old 27th February 2011, 23:12   #19  |  Link
Didée
Registered User
 
Join Date: Apr 2002
Location: Germany
Posts: 5,389
There are several open traps in this function, and most of the issues are coming up when the filtering stumbles into one of the traps.

- the prefiltering for Soften2 function is way too rough. TemporalMedian with radius=3 will create enough artifacts on motion to fool the motion search

- compensation for Soften2 is insufficient. With +/-1 compensations, you're only "safeguarding" a temporal radius of 1. Maybe 2, with some good will. But actually Soften2 is using a temporal radius of 4. In this combination, all the MVTools stuff is mostly a waste of time.
-- specifically, MedianBlurT with radius=4 is very slow. In combination with Motion Compensation, it is *ridiculously* slow. (For yet unknown reasons....)

- In the context in which they're used, the spatial parts of MedianBlur might do both harm and good. Possibly more harm. Related to the next point:

- It doesn't exactly help that ontop of the flicker, the source is noisy/grainy. This might interfere with some of the blurring operations. But that's a task for closer examination.


It's difficult to give specific "change this, change that" advice. I'd recommend to start from scratch, and think more closely about the implications of the used operations.

* * * *

BTW, who is this "Dedee" guy? I'd like to talk to him.
__________________
- We´re at the beginning of the end of mankind´s childhood -

My little flickr gallery. (Yes indeed, I do have hobbies other than digital video!)
Didée is offline   Reply With Quote
Old 28th February 2011, 00:36   #20  |  Link
prokhozhijj
Registered User
 
Join Date: Feb 2009
Posts: 48
Quote:
Originally Posted by Didée View Post
There are several open traps in this function, and most of the issues are coming up when the filtering stumbles into one of the traps.

- the prefiltering for Soften2 function is way too rough. TemporalMedian with radius=3 will create enough artifacts on motion to fool the motion search
In generally you are right probably. But in my two cases it works fine. The matter is that flicker is very strong sometimes. And I don't know how to make it weaker before motion search.

Quote:
Originally Posted by Didée View Post
- compensation for Soften2 is insufficient. With +/-1 compensations, you're only "safeguarding" a temporal radius of 1. Maybe 2, with some good will. But actually Soften2 is using a temporal radius of 4. In this combination, all the MVTools stuff is mostly a waste of time.
I've tried with radius 2 but it produces more artefacts than radius 1 (in my cases of course). Maybe the reason is in incorrect prefiltering. I don't know yet.

Quote:
Originally Posted by Didée View Post
-- specifically, MedianBlurT with radius=4 is very slow. In combination with Motion Compensation, it is *ridiculously* slow. (For yet unknown reasons....)
Yes. Again you are right. But with strong flicker it gets better results.

Quote:
Originally Posted by Didée View Post
It's difficult to give specific "change this, change that" advice. I'd recommend to start from scratch, and think more closely about the implications of the used operations.
Thanks. I will try. I saw results of deflickering in following site http://www.algosoft-tech.com/ (section Demo). I was very impressed and after I've decided to reach simillar results. But I don't know for sure if it is possible with avisynth's plugins only.

* * * *

Quote:
Originally Posted by Didée View Post
BTW, who is this "Dedee" guy? I'd like to talk to him.
Well. I thought that if I am using your algorithm then I need to keep this fact in new deflicker. But this mention can be removed from source at any time if you have objections.
__________________
CPU: Core 2 Quad q9505 RAM: 4G OS: Windows 7 64 Video: Nvidia Geforce 460 GTX VP4 AviSynth: 2.5.8

Last edited by prokhozhijj; 28th February 2011 at 01:06.
prokhozhijj is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:22.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.