Code:
LoadPlugin("I:\Hybrid\64bit\Avisynth\AVISYN~1\mvtools2.dll")
LoadPlugin("I:\Hybrid\64bit\Avisynth\AVISYN~1\masktools2.dll")
LoadPlugin("I:\Hybrid\64bit\Avisynth\AVISYN~1\MedianBlur2.dll")
LoadPlugin("I:\Hybrid\64bit\Avisynth\AVISYN~1\RgTools.dll")
Import("I:\Hybrid\64bit\Avisynth\avisynthPlugins\SpotLess.avs")
/*
SpotTest(),
Purpose:
Spotless produces frame global changes [most pixels are changed], here we want to try alter only real spots where there is significant change in spotless Y [by YTh8]
and also that are near grey in source [by CTh8], these targeted pixels are then overlayed from Spotless result onto original src.
Src, Pre-Spotless source
Filtered, Spotless result
YTh8, threshold for Y in 8 bit range.
CTh8, threshold for Chroma in 8 bit range.
YNoiseTh8, ?????
Show, Show Masks.Mask is the final mask made from MaskY and MaskC
*/
Function SpotTest(clip Src,clip Filtered,int "YTh8",int "Cth8",int "YNoiseTh8",Bool "Show") { # 8 -> 16 bit YUV444 only
YTh8 = Min(Max(Default(YTh8,12),0),255)
CTh8 = Min(Max(Default(CTh8,12),0),255)
YNoiseTh8 = Min(Max(Default(YNoiseTh8,8),0),255)
Show = Default(Show,False)
try {bpc = Src.BitsPerComponent} catch(msg) {bpc=8}
try {Y444=Src.IsYV24||Src.Is444} catch(msg) {Y444=false}
Assert(Y444,"SpotTest: Must be YV24/YUV444")
YTh = BitLShift(YTh8,bpc-8).String
CTh = BitLShift(CTh8,bpc-8).String
YNoiseTh = BitLShift(YNoiseTh8,bpc-8).String
SY = Src.ExtractY
FY = Filtered.ExtractY
PSY = SY.SelectEvery(1,-1)
NSY = SY.SelectEvery(1, 1)
MaskN = Mt_Lutxyz(PSY,SY,NSY,"x "+ YNoiseTh + " - y <= y z "+ YNoiseTh + " + <= & z "+ YNoiseTh + " - y <= y x "+ YNoiseTh + " + <= & | 0 range_max ?")
# ( ((x-YNoiseTh <= y) & (y <= z+YNoiseTh)) | ((z-YNoiseTh <= y) & (y <= x+YNoiseTh)) ) ? 0 : 255 ## Infix @ 8 bit
# ((YPrv-YNoiseTh <= YCur <= YNxt+YNoiseTh) || (YNxt-YNoiseTh <= YCur <= YPrv+YNoiseTh)) ? 0 : 255
MaskY = Mt_Lutxy(SY,FY,"x y - abs " + YTh + " > range_max 0 ?") # "(abs(x-y) > YTh) ? 255 : 0"
MU = Mt_Lut(Src.ExtractU,"x range_half - abs") # "abs(x - 128)"
MV = Mt_Lut(Src.ExtractV,"x range_half - abs") # "abs(x - 128)"
MaskC = Mt_Lutxy(MU,MV,"x 2 ^ y 2 ^ + .5 ^ round " + CTh + " <= range_max 0 ?") # "(round(((x^2)+(y^2))^0.5) <= CTh) ? 255 : 0" # 2D chroma distance from grey (by Pythagoras)
MaskN = MaskN.mt_Expand
MaskY = MaskY.mt_Expand
MaskC = MaskC.mt_Expand
MaskAnd = MaskC.Mt_Logic(MaskY,"and")
MaskAnd = MaskAnd.Mt_Logic(MaskN,"and")
MaskSelect = MaskAnd.mt_Expand.Blur(1.0)
Overlay(Src,Filtered,Mask=MaskSelect,Opacity=1.0)
CGREY = MaskSelect.Mt_lut("range_half")
SEP=$FF0000
HBAR=Src.BlankClip(length=1,Height=4,color=SEP)
LFT=StackVertical(Src.Subtitle("Src"),HBAR,Filtered.Subtitle("Filt"),HBAR,Last.Subtitle("Result"))
MID=StackVertical(YtoUV(CGrey,CGrey,MaskN).Subtitle("MaskN"),HBAR,YtoUV(CGrey,CGrey,MaskY).Subtitle("MaskY {abs(SrcY-FiltY) > YTh}"),HBAR,YtoUV(CGrey,CGrey,MaskSelect).Subtitle("MaskSelect=MaskAnd.mt_Expand.Blur(1.0) {Blk=Src used : Wht=Filt used}"))
RGT=StackVertical(YtoUV(CGrey,CGrey,MaskC).Subtitle("MaskC {Src Chroma distance from Grey <= Cth8"),HBAR,YtoUV(CGrey,CGrey,MaskAnd).Subtitle("MaskAnd {MaskN & MaskC & MaskY}"),HBAR,YtoUV(CGrey,CGrey,CGrey))
VBAR=LFT.BlankClip(length=1,width=4,color=SEP)
STK = StackHorizontal(LFT,VBAR,MID,VBAR,RGT)
(Show) ? STK : Last
}
ConvertToYV24()
Src=Last
Filtered=SpotLess(RadT=1)
YTH8 = 8
CTH8 = 12
YNOISETH8 = 2
SHOW = False
SpotTest(Src,Filtered,YTH8,CTH8,YNOISETH8,SHOW)
Return Last.ConvertToRGB32
ConvertToRGB32()
# colorformat RGB32
(fixed #colorspace RGB32 -> # colorformat RGB32)