JKP123

Francesco.1989.720p-Q0S

May 5th, 2021 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.17 KB | None | 0 0
  1. import vapoursynth as vs
  2. import havsfunc as hvf
  3. import kagefunc as kgf
  4. import vsutil
  5. import kashfunc as ksh #my own python module for vapoursynth will releaase it publicly soon still a lot of work to do
  6. import fvsfunc as fvf
  7. import adptvgrnMod as adp
  8. core = vs.get_core()
  9. video=core.lsmas.LWLibavSource(r'D:\Video\UNNAMED DISC\title_t00.mkv')
  10. #Fixing dirty lines by extracting each plane and fixing them individually
  11. l=vsutil.get_y(video)#luma plane
  12. u=vsutil.plane(video,1)
  13. v=vsutil.plane(video,2)
  14. l=core.edgefixer.ContinuityFixer(l,2,2,2,0,10)
  15. u=core.edgefixer.ContinuityFixer(u,1,1,1,0,5)
  16. v=core.edgefixer.ContinuityFixer(v,1,1,1,0,5)
  17. video1=core.std.ShufflePlanes([l,u,v],[0,0,0],colorfamily=vs.YUV)#merging the planes together
  18. video1=core.resize.Spline36(video1, width=1280, height=720)
  19. video3=vsutil.depth(video1,16)
  20. luma=core.std.ShufflePlanes(video3, [0], colorfamily=vs.GRAY)#extracting luma plane
  21. gblur=core.bilateral.Gaussian(luma,[3.0,0,0],[3.0,0,0])#performing Gausian blur so that our mask doesn't pickup useless stuffs especialy noise
  22. #Defining a Kirsch mask by taking an arbitary point and rotating by 45 degrees in each 8 directions
  23. k1 = core.std.Convolution(gblur,[ 5, 5, 5, -3, 0, -3, -3, -3, -3])
  24. k2 = core.std.Convolution(gblur,[-3, 5, 5, 5, 0, -3, -3, -3, -3])
  25. k3 = core.std.Convolution(gblur,[-3, -3, 5, 5, 0, 5, -3, -3, -3])
  26. k4 = core.std.Convolution(gblur,[-3, -3, -3, 5, 0, 5, 5, -3, -3])
  27. k5 = core.std.Convolution(gblur,[-3, -3, -3, -3, 0, 5, 5, 5, -3])
  28. k6 = core.std.Convolution(gblur,[-3, -3, -3, -3, 0, -3, 5, 5, 5])
  29. k7 = core.std.Convolution(gblur,[ 5, -3, -3, -3, 0, -3, -3, 5, 5])
  30. k8 = core.std.Convolution(gblur,[ 5, 5, -3, -3, 0, -3, -3, 5, -3])
  31. kirsch=core.std.Expr([k1, k2, k3, k4, k5, k6, k7, k8], 'x y max z max a max b max c max d max e max')#Finding maximum slope along a particular direction(North,North-West,West,South-West,South,South-East,East and North-East) which is essentially the max of first order partial derivative in eight different directions.
  32. #Performing hysteresis on the resultant mask by sharpening and brightening the edgemask, bit of a lazy way however it kinda works so be it and I'm tired :p Will improve it further in my own python module kashfuc.py. What it does is it removes useless stuffs which does not require fixing.
  33. sharpen=core.std.Convolution(kirsch,[0,-1,0,-1,5,-1,0,-1,0],planes=[0],saturate=False).std.Expr('x 65535 / sqrt 65535 *')
  34. m1=vsutil.iterate(sharpen, core.std.Maximum, 2)#Maximizing the mask twice
  35. m2=vsutil.iterate(m1, core.std.Minimum, 3)#Minimizing the expanded max thrice
  36. halos=core.std.Expr([m1,m2], 'x y -').std.Inflate().std.Inflate()#Subtracting the maximized mask from the minimized mask to cover only the halos.
  37. dehalo1=hvf.FineDehalo(video3)#Performing dehaloing
  38. dehalo2=hvf.DeHalo_alpha(video3)#using another filter to dehalo
  39. d=ksh.MaxFilter(video3,dehalo1,dehalo2,strict=True)#Inspired from Kgarbs MaxFilter https://github.com/kgrabs/zzfunc when strict is set to 'True' it averages the output of two filters. Will modify it more in Kashfunc.py to perform Weighted average filtering.
  40. d=core.std.MaskedMerge(video3,d,halos)#Masked dehaloing
  41. d=vsutil.depth(d,8,dither_type='error_diffusion')#dithering down to 8 bit
  42.  
  43. #Trying another method to dehalo
  44. video2=hvf.FineDehalo(vsutil.depth(video1,16),showmask=4).std.Maximum().std.Maximum()#Extrating FineDehalo's strong mask
  45. video5=video2.std.Minimum().std.Minimum().std.Minimum()
  46. stronghalo=core.std.Expr([video2,video5], 'x y -').std.ShufflePlanes([0], colorfamily=vs.GRAY).std.Inflate().std.Inflate()
  47. video6=vsutil.depth(video1,16)
  48. dh=hvf.DeHalo_alpha(video6,rx=2.5,ry=2.5)
  49. dh=core.std.MaskedMerge(video6,dh,stronghalo)
  50. dh=vsutil.depth(dh,8,dither_type='error_diffusion')
  51.  
  52. finaldehalo=ksh.MaxFilter(video1,d,dh,strict=False)#visually both the filtration method gave almost similar results but I don't want to take chances hence taking the maximum outcome of either of the two filters by setting Strict=False
  53.  
  54. video4=finaldehalo
  55. video4=vsutil.depth(video4,16)
  56. #n=video4.num_frames
  57. #thresh = (32 * (1 << video4.format.bits_per_sample) - 1) / 255
  58. mask=kgf.retinex_edgemask(video4).std.Maximum().std.Inflate().std.Maximum().std.Inflate()
  59. #deblocking and debanding by adding some grain across the luma and chroma planes.
  60. deband = core.neo_f3kdb.Deband(video4, y=128, cb=64, cr=64, range=25, grainy=32,grainc=20, output_depth=16, keep_tv_range=True)
  61. #Modyifying the added grain a bit to compress better and not look like a mosquito noise or whatever it is called!
  62. deband=adp.adptvgrnMod(deband, strength=2, size=1.5, sharp=25, static=False,luma_scaling=5, grain_chroma=True)
  63. merge=core.std.MaskedMerge(deband,video4,mask)#masked
  64. merge=vsutil.depth(merge,8,dither_type='error_diffusion')
  65.  
  66. #6232 #11270-36308/2 97398/2 104680-110896/2 136606-144942 163034-168926/2 176062-180412/2 207896-220054/2 #252366-257132 373936-374918/2
  67. #Applying the filters by replacing the original frames with the filtered frames.
  68. video1=fvf.rfs(video1,finaldehalo,"[3116 187459]")
  69. video1=fvf.rfs(video1,merge,"[5635 18154][52340 55448][58388 58859][59518 59818][68303 72471][81517 84463][88031 90206][103948 110027][126183 128566][185914 187459]")
  70.  
  71. video1.set_output()
  72.  
  73.  
Advertisement
Add Comment
Please, Sign In to add comment