StainlessS
20th September 2021, 01:01
Might come in useful.
TweakYPlaneNoiseThresh.avsi
# TweakYPlaneNoiseThresh.avsi
/*
TweakYPlaneNoiseThresh() v0.04, by StainlessS @ Doom9. # https://forum.doom9.org/showthread.php?p=1952709#post1952709
Req AVS+, RT_Stats, Grunt.
Any Planar Y/YUV/A. [depth 8 -> 32].
Shows results of some YPlane operation [YPlaneMin, YPlaneMax, YPlaneMinMaxDifference] on sampled clip, with multiple simultaneous Threshold settings.
TweakYPlaneNoiseThresh(clip c,int "YPlaneOp"=0,Float "NoiseBase"=0.0,Float "NoiseStep"=0.005,Int "Rows"=21)
eg YPlaneMin(Threshold=NoiseTh)
Threshold is a percentage, the maximum percent population of the pixels that are allowed to be ignored below minimum (ignore extreme pixels ie noise).
YPlaneMin first creates a pixel population count array of the current frame, and then it creates a running total of pixel population stepping from 0 upwards.
When the running total pixel population % is greater or equal to threshold arg, it returns the index of the array element that it has so far reached.
If the lowest populated array elements total less than Threshold, they are ignored. If the lowest populated element % population is greater or equal to Threshold, then nothing
is ignored and that lowest poplated pixel element index is returned.
YPlaneMin(threshold=0.0) ignores nothing and returns the index of the lowest populated element in the count array.
You might use eg YPlaneMin(Threshold=0.1) to ignore (at most) the darkest 0.1% of pixels when finding minimum pixel luma.
YplaneMax(Threshold=0.2), ignores lightest (at most) 0.2% of pixels.
YplaneMinMaxDifference(Threshold=0.3), ignores (at most) the darkest 0.3% and lightest 0.3% of pixels.
YPlaneMedian(), is implemented as YPlaneMin(Threshold=50.0).
Metrics: (only 5 lines of results out of 21)
0] TweakYPlaneNoiseThresh: YPlaneMin @ NoiseTh % (1280x720P10)
Ignore up to Ignore single
NoiseTh OneInXPixels Square of Pixels YPlaneMin(NoiseTh)
0.000 % 0 0x0 152
0.050 % 2000 21x21 168
0.100 % 1000 30x30 172
0.150 % 666 37x37 172
0.200 % 500 43x43 172
To specify to ignore at most 1 in every 500 darkest pixels in a frame, you can use YPlaneMin(Threshold=100.0/500.0) [as the 5th result line above].
The "Ignore single Square of pixels" entry shows the approx max size of a non noise object that could potentially be ignored by mistake [when no other lower level noise is on frame].
"Ignore single Square of pixels" size shown will vary with frame size.
The rightmost "YPlaneMin(NoiseTh)" column shows the actual result from (in this case) YPlaneMin().
Args:
YPlaneOp, default 0, 0->2, 0=YPlaneMin(), 1=YPlaneMax(), 2=YPlaneMinMaxDifference().
NoiseBase, default 0.00, 0.0->1.0, Lowest Noise Threshold tested.
NoiseStep default 0.005, 0.0<NoiseStep<=1.0, Noise Theshold step between 21 different Noise Thresholds, relative to NoiseBase.
Rows, default 21, 1 -> max allowed by frame height (silent limit). Number of rows displayed. [the more rows, the slower]
*/
Function TweakYPlaneNoiseThresh(clip c,int "YPlaneOp",Float "NoiseBase",Float "NoiseStep",Int "Rows") {
c myName="TweakYPlaneNoiseThresh: "
YPlaneOp = Default(YPlaneOp,0)
NoiseBase = Default(NoiseBase,0.0)
NoiseStep = Default(NoiseStep,0.005)
Assert(0 <= YPlaneOp <= 2,myName+"0 <= YPlaneOp <= 2")
Assert(0.0 <= NoiseBase <= 1.0,myName+"0.0 <= NoiseBase <= 1.0")
Assert(0.0 < NoiseStep <= 1.0,myName+"0.0 < NoiseStep <= 1.0")
W=Width H=Height RowMax=(H/18)-3 bpc=BitsPerComponent
Rows=Default(Rows,21).Max(1).Min(RowMax)
Assert(!IsRGB&&IsPlanar,myName+"Planar with Y only")
YOP=Select(YPLaneOp,"YPlaneMin","YPLaneMax","YPlaneMinMaxDifference")
evS="T%d=NoiseBase+(i*NoiseStep) I%d=(T%d>0.0?100.0/T%d:0).Int S%d=(T%d>0.0?SqRt(P*T%d/100).Round:0).Int Y%d="+YOP+"(T%d)"
# Newlines are a PITA, gotta leave "\\n" for Scriptclip Eval("S=RT_string..."), so using here "\\\\n" to leave "\\n" in Scriptclip. [Same for "%%%%"="%%"]
dhS=RT_String("""RT_String("%%d] TweakYPlaneNoiseThresh: %s @ NoiseTh %%%% (%dx%dP%d)\\\\n Ignore up to Ignore single\\\\n""" +
\ "NoiseTh%%%% OneInXPixels Square of pixels %s(NoiseTh)\\\\n",YOP,W,H,bpc,YOP)
drS="%.6f %10d %5dx%-5d " + (bpc==32?"%f":"%5d")
dvS="\ T%d,I%d,S%d,S%d,Y%d"
vS=","+Chr(10)+"\ n,"+chr(10)
for(i=0,Rows-1) { dhS=RT_String("%s%s%s",dhS,drS,(i==Rows-1?Chr(34):"\\n")) vS=RT_String("%s%s%s",vS,RT_String(dvS,i,i,i,i,i),(i==Rows-1)?")":","+Chr(10)) }
rtS=dhS+vS
# DbgS=rts.RT_StrReplace("\\n",Chr(10)) RT_WriteFile(".\TweakYPlaneNoiseThresh_DEBUG.txt","%s",DbgS) RT_Debug(DbgS) # Output Debugging to file and DebugView
SSS="""
n=current_frame W=Width H=Height P=W*H
For(i=0,Rows-1) { Eval(RT_String(evS,i,i,i,i,i,i,i,i,i)) }
Return Subtitle(Eval(rtS),lsp=0,Font="Courier New")
"""
Return GScriptClip(SSS,Args="NoiseBase,NoiseStep,Rows,evS,rtS",Local=True)
}
Client
##### CONFIG #######
#Avisource(".\OUT\TEST1.avi")
#FfVideoSource(".\OUT\TEST1.avi")
AviSource(".\BP-KTL.AVI")
#ConvertBits(10)
YPlaneOp = 0 # 0) 0->2, 0=YPlaneMin(), 1=YPlaneMax(), 2=YPlaneMinMaxDifference().
NoiseBase = 0.00 # 0.0) 0.0->1.0, Lowest Noise Threshold shown.
NoiseStep = 0.005 # 0.005) 0.0<NoiseStep<=1.0, Noise Theshold stepping.
Rows = 9999 # 9999, All of em' (silent limited to frame height).
##### END CONFIG ######
Return TweakYPlaneNoiseThresh(YPlaneOp,NoiseBase,NoiseStep,Rows)
What it looks like [not selected to demo anything, just a clip I had at hand]
https://i.postimg.cc/4ncpCNcZ/Tweak-YPlane-Noise-Thresh-01.jpg (https://postimg.cc/4ncpCNcZ)
TweakYPlaneNoiseThresh.avsi
# TweakYPlaneNoiseThresh.avsi
/*
TweakYPlaneNoiseThresh() v0.04, by StainlessS @ Doom9. # https://forum.doom9.org/showthread.php?p=1952709#post1952709
Req AVS+, RT_Stats, Grunt.
Any Planar Y/YUV/A. [depth 8 -> 32].
Shows results of some YPlane operation [YPlaneMin, YPlaneMax, YPlaneMinMaxDifference] on sampled clip, with multiple simultaneous Threshold settings.
TweakYPlaneNoiseThresh(clip c,int "YPlaneOp"=0,Float "NoiseBase"=0.0,Float "NoiseStep"=0.005,Int "Rows"=21)
eg YPlaneMin(Threshold=NoiseTh)
Threshold is a percentage, the maximum percent population of the pixels that are allowed to be ignored below minimum (ignore extreme pixels ie noise).
YPlaneMin first creates a pixel population count array of the current frame, and then it creates a running total of pixel population stepping from 0 upwards.
When the running total pixel population % is greater or equal to threshold arg, it returns the index of the array element that it has so far reached.
If the lowest populated array elements total less than Threshold, they are ignored. If the lowest populated element % population is greater or equal to Threshold, then nothing
is ignored and that lowest poplated pixel element index is returned.
YPlaneMin(threshold=0.0) ignores nothing and returns the index of the lowest populated element in the count array.
You might use eg YPlaneMin(Threshold=0.1) to ignore (at most) the darkest 0.1% of pixels when finding minimum pixel luma.
YplaneMax(Threshold=0.2), ignores lightest (at most) 0.2% of pixels.
YplaneMinMaxDifference(Threshold=0.3), ignores (at most) the darkest 0.3% and lightest 0.3% of pixels.
YPlaneMedian(), is implemented as YPlaneMin(Threshold=50.0).
Metrics: (only 5 lines of results out of 21)
0] TweakYPlaneNoiseThresh: YPlaneMin @ NoiseTh % (1280x720P10)
Ignore up to Ignore single
NoiseTh OneInXPixels Square of Pixels YPlaneMin(NoiseTh)
0.000 % 0 0x0 152
0.050 % 2000 21x21 168
0.100 % 1000 30x30 172
0.150 % 666 37x37 172
0.200 % 500 43x43 172
To specify to ignore at most 1 in every 500 darkest pixels in a frame, you can use YPlaneMin(Threshold=100.0/500.0) [as the 5th result line above].
The "Ignore single Square of pixels" entry shows the approx max size of a non noise object that could potentially be ignored by mistake [when no other lower level noise is on frame].
"Ignore single Square of pixels" size shown will vary with frame size.
The rightmost "YPlaneMin(NoiseTh)" column shows the actual result from (in this case) YPlaneMin().
Args:
YPlaneOp, default 0, 0->2, 0=YPlaneMin(), 1=YPlaneMax(), 2=YPlaneMinMaxDifference().
NoiseBase, default 0.00, 0.0->1.0, Lowest Noise Threshold tested.
NoiseStep default 0.005, 0.0<NoiseStep<=1.0, Noise Theshold step between 21 different Noise Thresholds, relative to NoiseBase.
Rows, default 21, 1 -> max allowed by frame height (silent limit). Number of rows displayed. [the more rows, the slower]
*/
Function TweakYPlaneNoiseThresh(clip c,int "YPlaneOp",Float "NoiseBase",Float "NoiseStep",Int "Rows") {
c myName="TweakYPlaneNoiseThresh: "
YPlaneOp = Default(YPlaneOp,0)
NoiseBase = Default(NoiseBase,0.0)
NoiseStep = Default(NoiseStep,0.005)
Assert(0 <= YPlaneOp <= 2,myName+"0 <= YPlaneOp <= 2")
Assert(0.0 <= NoiseBase <= 1.0,myName+"0.0 <= NoiseBase <= 1.0")
Assert(0.0 < NoiseStep <= 1.0,myName+"0.0 < NoiseStep <= 1.0")
W=Width H=Height RowMax=(H/18)-3 bpc=BitsPerComponent
Rows=Default(Rows,21).Max(1).Min(RowMax)
Assert(!IsRGB&&IsPlanar,myName+"Planar with Y only")
YOP=Select(YPLaneOp,"YPlaneMin","YPLaneMax","YPlaneMinMaxDifference")
evS="T%d=NoiseBase+(i*NoiseStep) I%d=(T%d>0.0?100.0/T%d:0).Int S%d=(T%d>0.0?SqRt(P*T%d/100).Round:0).Int Y%d="+YOP+"(T%d)"
# Newlines are a PITA, gotta leave "\\n" for Scriptclip Eval("S=RT_string..."), so using here "\\\\n" to leave "\\n" in Scriptclip. [Same for "%%%%"="%%"]
dhS=RT_String("""RT_String("%%d] TweakYPlaneNoiseThresh: %s @ NoiseTh %%%% (%dx%dP%d)\\\\n Ignore up to Ignore single\\\\n""" +
\ "NoiseTh%%%% OneInXPixels Square of pixels %s(NoiseTh)\\\\n",YOP,W,H,bpc,YOP)
drS="%.6f %10d %5dx%-5d " + (bpc==32?"%f":"%5d")
dvS="\ T%d,I%d,S%d,S%d,Y%d"
vS=","+Chr(10)+"\ n,"+chr(10)
for(i=0,Rows-1) { dhS=RT_String("%s%s%s",dhS,drS,(i==Rows-1?Chr(34):"\\n")) vS=RT_String("%s%s%s",vS,RT_String(dvS,i,i,i,i,i),(i==Rows-1)?")":","+Chr(10)) }
rtS=dhS+vS
# DbgS=rts.RT_StrReplace("\\n",Chr(10)) RT_WriteFile(".\TweakYPlaneNoiseThresh_DEBUG.txt","%s",DbgS) RT_Debug(DbgS) # Output Debugging to file and DebugView
SSS="""
n=current_frame W=Width H=Height P=W*H
For(i=0,Rows-1) { Eval(RT_String(evS,i,i,i,i,i,i,i,i,i)) }
Return Subtitle(Eval(rtS),lsp=0,Font="Courier New")
"""
Return GScriptClip(SSS,Args="NoiseBase,NoiseStep,Rows,evS,rtS",Local=True)
}
Client
##### CONFIG #######
#Avisource(".\OUT\TEST1.avi")
#FfVideoSource(".\OUT\TEST1.avi")
AviSource(".\BP-KTL.AVI")
#ConvertBits(10)
YPlaneOp = 0 # 0) 0->2, 0=YPlaneMin(), 1=YPlaneMax(), 2=YPlaneMinMaxDifference().
NoiseBase = 0.00 # 0.0) 0.0->1.0, Lowest Noise Threshold shown.
NoiseStep = 0.005 # 0.005) 0.0<NoiseStep<=1.0, Noise Theshold stepping.
Rows = 9999 # 9999, All of em' (silent limited to frame height).
##### END CONFIG ######
Return TweakYPlaneNoiseThresh(YPlaneOp,NoiseBase,NoiseStep,Rows)
What it looks like [not selected to demo anything, just a clip I had at hand]
https://i.postimg.cc/4ncpCNcZ/Tweak-YPlane-Noise-Thresh-01.jpg (https://postimg.cc/4ncpCNcZ)