javlak
3rd June 2011, 14:35
What is it?
It's a function that tries to splice two different resizing algorithms together in order to maintain the advantages of both (i.e. sharpness and no ringing artefacts).
This is achieved by resizing twice, once using a "soft" algorithm, such as Hermite, and a second time using a sharper one, such as Spline36. The function compares the two pictures on a per-pixel basis, and if the difference between two pixels exceeds a certain threshold, it assumes that this must be some sort of artefact and it either averages the pixel values or it selects the pixel of the softer picture.
What isn't it?
It's not a deringing function. Its purpose is not to dering (although it has some built-in features to do that, but they are light), but rather to try and avoid ringing altogether as much as possible by manipulating the resizing algorithm.
How do I use it?
Here are all the customization parameters:
Width: Desired Width
Height: Desired Height
Threshold: If the difference between two pixels is lower than the threshold, then the pixel produced by the sharper algorithm will be used, otherwise either the pixel produxed by the softer algorithm will be used, or their average. Even though the final results of the softer and the sharper algorithm are apparent on a large scale, on a per-pixel basis, the difference between them isn't really too big, so bear in mind that a threshold can be as low as 3, depending on your tastes. The lower the threshold, the more the sharper algorithm gets used but artefacts might get missed, the higher the threshold, the less the artefacts, but the softer the result. Default:5
Mode: Mode 1 selects pixels. If the difference of the pixels is within the threshold, it selects the sharper one. Otherwise, it selects the softer one. Mode 2 averages pixels. If the difference of the pixels is within the threshold, it selects the sharper one. Otherwise, it averages them. The first mode has the advantage that, if there are artefacts caused by the sharper algorithm, they will be removed; the disadvantage is that this might soften the picture, especially if the threshold is high. The second mode has the advantage that it will not soften the picture as much as the first mode, but if there are artefacts caused by the resizing, then these will not be completely extinguished, but rather "softened out", due to the averaging out of the pixels. Default: 1 (Select)
Kernel1: This is the kernel to use for the soft resizing: Kernel1=0 is the Bilinear algorithm, which doesn't produce artefacts, but can produce some bluriness. Kernel1=1 is Hermite, which is an algorithm which is well-known to produce as little blurriness and artefacts as possible. Kernel1=2 is not a resizing kernel per se, as it is the average of the bicubic one, which is a mostly artefact-free and bluriness-free algorithm, and the 2-tap Blackman one, which does produce some artefacts, but not too many. Kernel1=3 is the 2-tap Blackman algorithm, which does produce some artefacts and as a matter of a fact is not what you would call a soft algorithm, but it produces less artefacts than all the algorithms that are used by the function as sharp ones. Default: 1 (Hermite)
Kernel2: This is the kernel for the sharp resizing: Kernel2=0 Is the Blackman algorithm with 4 lobes; kernel2=1, Spline16; kernel2=2 Spline36; kernel2=3 Spline64. In order of sharpening Spline64>Spline36>Spline16>Blackman and in order of artefacts Blackman>Spline16>Spline36>Spline64. Default:2 (Spline36)
Preprocess: If there's some grain or light artefacts in your picture already and you want to clean them before resizing to avoid any artefact accentuation or new artefacts, you can use this. To be honest, if your source isn't clean already you're probably better off using a dedicated noise and/or deringer such as MC_Spuds and YAHR before resizing. Default: False
Postprocess: If you feel that your threshold is too low and you might have artefacts in the final picture, you can use this to clean up the final result. To be honest, you're better off using a dedicated noise and/or deringer such as MC_Spuds and YAHR. Default: False
Sharper: This is used to sharpen up the softer picture. If the original picture is artefact free, and depending on your threshold values and kernel1, this can increase the visual quality perception of the resulting image.
Masktype: The type of mask used to detect edges. The default (Roberts) will definitely miss a few, but is faster than the alternative and noise-tolerant; canny is very accurate, but slower and may also detect noise as an edge. Default: 1 (Roberts)
Sensitivity: This is the sensitivity of the canny mask. Its value is irrelevant if you are using Roberts. Its value can be anywhere from 0 (which corresponds to sigma=1.2 and which is set to be the least sensitive setting and thus might miss a few edges, but will not catch noise) to 10 (which corresponds to sigma=0.6 and which is set to be the most sensitive setting and will detect the most edges, but might catch noise as well). Default: 0 (σ=1.2, l=4, h=4).
function canny(clip v, int "sensitivity")
{
sensitivity = 1.2 - (sensitivity/16.67)
v=v.RemoveGrain(mode=1).RemoveGrain(mode=5)
v=v.Grayscale()
v=v.RemoveGrain(mode=11).RemoveGrain(mode=1).RemoveGrain(mode=5)
v=v.tbilateral(7,3,1.3,1,5,5,1,0,true,false,kerns=4,kerni=3,restype=0)
v=v.tcanny(sigma=sensitivity,t_l=3.9,t_h=3.9)
v=v.RemoveGrain(mode=1).RemoveGrain(mode=5)
return v
}
function SpliceResize(clip v, int "w", int "h", int "threshold", int "mode", int "kernel1", int "kernel2", bool "preprocess", bool "postprocess", bool "sharper", int "masktype", int "sensitivity")
{
threshold = default (threshold, 5)
w=default(w, width(v))
h=default(h, height(v))
mode=default(mode, 1)
kernel1=default(kernel1, 1)
kernel2=default(kernel2, 2)
postprocess=default(postprocess, false)
preprocess=default(preprocess, false)
sharper=default(sharper, false)
masktype=default(masktype, 1)
sensitivity=default(sensitivity, 0)
sensitivity = (sensitivity>0 && sensitivity<11) ? sensitivity : 0
threshold = (threshold>0 && threshold<255) ? threshold : 5
v = (preprocess==true) ? v.RemoveGrain(mode=1).RemoveGrain(mode=5) : v
baseline = (kernel1==1) ? BicubicResize(v, w, h, b=0.0, c=0.0) : \
(kernel1==0) ? BilinearResize(v, w, h) : \
(kernel1==3) ? BlackmanResize(v, w, h, taps=2) : \
(kernel1==2) ? mt_average(BicubicResize(v, w, h, b=0.0, c=0.0),BlackmanResize(v, w, h, taps=2)) : BicubicResize(v, w, h, b=0.0, c=0.0)
sharp = (kernel2==2) ? Spline36Resize(v, w, h) : \
(kernel2==3) ? Spline64Resize(v, w, h) : \
(kernel2==0) ? BlackmanResize(v, w, h, taps=4) : \
(kernel2==1) ? Spline16Resize(v, w, h) : Spline36Resize(v, w, h)
func = (mode==1) ? ("x y - abs "+string(threshold)+" > y x ?") : \
(mode==2) ? ("x y - abs "+string(threshold)+" > y x y + 2 / ?") : ("x y - abs "+string(threshold)+" > y x ?")
baseline = (sharper==true) ? baseline.unsharp(strength=0.2) : baseline
v=mt_lutxy(sharp,baseline,func)
v = (postprocess==true) ? v.RemoveGrain(mode=1).RemoveGrain(mode=5) : v
vmask = (masktype==2) ? canny(baseline, sensitivity).mt_expand(mode="both") : baseline.mt_edge(mode = "roberts").mt_expand(mode="both").RemoveGrain(mode=11).RemoveGrain(mode=1).RemoveGrain(mode=5)
v = mt_merge(v, sharp, vmask)
return v
}
Dependencies:
For SpliceResize:
RemoveGrain
VariableBlur (any version that includes unsharp)
Masktools 2
For canny:
tcanny
MPEG2Dec3DG.dll OR DGDecode.dll
It's a function that tries to splice two different resizing algorithms together in order to maintain the advantages of both (i.e. sharpness and no ringing artefacts).
This is achieved by resizing twice, once using a "soft" algorithm, such as Hermite, and a second time using a sharper one, such as Spline36. The function compares the two pictures on a per-pixel basis, and if the difference between two pixels exceeds a certain threshold, it assumes that this must be some sort of artefact and it either averages the pixel values or it selects the pixel of the softer picture.
What isn't it?
It's not a deringing function. Its purpose is not to dering (although it has some built-in features to do that, but they are light), but rather to try and avoid ringing altogether as much as possible by manipulating the resizing algorithm.
How do I use it?
Here are all the customization parameters:
Width: Desired Width
Height: Desired Height
Threshold: If the difference between two pixels is lower than the threshold, then the pixel produced by the sharper algorithm will be used, otherwise either the pixel produxed by the softer algorithm will be used, or their average. Even though the final results of the softer and the sharper algorithm are apparent on a large scale, on a per-pixel basis, the difference between them isn't really too big, so bear in mind that a threshold can be as low as 3, depending on your tastes. The lower the threshold, the more the sharper algorithm gets used but artefacts might get missed, the higher the threshold, the less the artefacts, but the softer the result. Default:5
Mode: Mode 1 selects pixels. If the difference of the pixels is within the threshold, it selects the sharper one. Otherwise, it selects the softer one. Mode 2 averages pixels. If the difference of the pixels is within the threshold, it selects the sharper one. Otherwise, it averages them. The first mode has the advantage that, if there are artefacts caused by the sharper algorithm, they will be removed; the disadvantage is that this might soften the picture, especially if the threshold is high. The second mode has the advantage that it will not soften the picture as much as the first mode, but if there are artefacts caused by the resizing, then these will not be completely extinguished, but rather "softened out", due to the averaging out of the pixels. Default: 1 (Select)
Kernel1: This is the kernel to use for the soft resizing: Kernel1=0 is the Bilinear algorithm, which doesn't produce artefacts, but can produce some bluriness. Kernel1=1 is Hermite, which is an algorithm which is well-known to produce as little blurriness and artefacts as possible. Kernel1=2 is not a resizing kernel per se, as it is the average of the bicubic one, which is a mostly artefact-free and bluriness-free algorithm, and the 2-tap Blackman one, which does produce some artefacts, but not too many. Kernel1=3 is the 2-tap Blackman algorithm, which does produce some artefacts and as a matter of a fact is not what you would call a soft algorithm, but it produces less artefacts than all the algorithms that are used by the function as sharp ones. Default: 1 (Hermite)
Kernel2: This is the kernel for the sharp resizing: Kernel2=0 Is the Blackman algorithm with 4 lobes; kernel2=1, Spline16; kernel2=2 Spline36; kernel2=3 Spline64. In order of sharpening Spline64>Spline36>Spline16>Blackman and in order of artefacts Blackman>Spline16>Spline36>Spline64. Default:2 (Spline36)
Preprocess: If there's some grain or light artefacts in your picture already and you want to clean them before resizing to avoid any artefact accentuation or new artefacts, you can use this. To be honest, if your source isn't clean already you're probably better off using a dedicated noise and/or deringer such as MC_Spuds and YAHR before resizing. Default: False
Postprocess: If you feel that your threshold is too low and you might have artefacts in the final picture, you can use this to clean up the final result. To be honest, you're better off using a dedicated noise and/or deringer such as MC_Spuds and YAHR. Default: False
Sharper: This is used to sharpen up the softer picture. If the original picture is artefact free, and depending on your threshold values and kernel1, this can increase the visual quality perception of the resulting image.
Masktype: The type of mask used to detect edges. The default (Roberts) will definitely miss a few, but is faster than the alternative and noise-tolerant; canny is very accurate, but slower and may also detect noise as an edge. Default: 1 (Roberts)
Sensitivity: This is the sensitivity of the canny mask. Its value is irrelevant if you are using Roberts. Its value can be anywhere from 0 (which corresponds to sigma=1.2 and which is set to be the least sensitive setting and thus might miss a few edges, but will not catch noise) to 10 (which corresponds to sigma=0.6 and which is set to be the most sensitive setting and will detect the most edges, but might catch noise as well). Default: 0 (σ=1.2, l=4, h=4).
function canny(clip v, int "sensitivity")
{
sensitivity = 1.2 - (sensitivity/16.67)
v=v.RemoveGrain(mode=1).RemoveGrain(mode=5)
v=v.Grayscale()
v=v.RemoveGrain(mode=11).RemoveGrain(mode=1).RemoveGrain(mode=5)
v=v.tbilateral(7,3,1.3,1,5,5,1,0,true,false,kerns=4,kerni=3,restype=0)
v=v.tcanny(sigma=sensitivity,t_l=3.9,t_h=3.9)
v=v.RemoveGrain(mode=1).RemoveGrain(mode=5)
return v
}
function SpliceResize(clip v, int "w", int "h", int "threshold", int "mode", int "kernel1", int "kernel2", bool "preprocess", bool "postprocess", bool "sharper", int "masktype", int "sensitivity")
{
threshold = default (threshold, 5)
w=default(w, width(v))
h=default(h, height(v))
mode=default(mode, 1)
kernel1=default(kernel1, 1)
kernel2=default(kernel2, 2)
postprocess=default(postprocess, false)
preprocess=default(preprocess, false)
sharper=default(sharper, false)
masktype=default(masktype, 1)
sensitivity=default(sensitivity, 0)
sensitivity = (sensitivity>0 && sensitivity<11) ? sensitivity : 0
threshold = (threshold>0 && threshold<255) ? threshold : 5
v = (preprocess==true) ? v.RemoveGrain(mode=1).RemoveGrain(mode=5) : v
baseline = (kernel1==1) ? BicubicResize(v, w, h, b=0.0, c=0.0) : \
(kernel1==0) ? BilinearResize(v, w, h) : \
(kernel1==3) ? BlackmanResize(v, w, h, taps=2) : \
(kernel1==2) ? mt_average(BicubicResize(v, w, h, b=0.0, c=0.0),BlackmanResize(v, w, h, taps=2)) : BicubicResize(v, w, h, b=0.0, c=0.0)
sharp = (kernel2==2) ? Spline36Resize(v, w, h) : \
(kernel2==3) ? Spline64Resize(v, w, h) : \
(kernel2==0) ? BlackmanResize(v, w, h, taps=4) : \
(kernel2==1) ? Spline16Resize(v, w, h) : Spline36Resize(v, w, h)
func = (mode==1) ? ("x y - abs "+string(threshold)+" > y x ?") : \
(mode==2) ? ("x y - abs "+string(threshold)+" > y x y + 2 / ?") : ("x y - abs "+string(threshold)+" > y x ?")
baseline = (sharper==true) ? baseline.unsharp(strength=0.2) : baseline
v=mt_lutxy(sharp,baseline,func)
v = (postprocess==true) ? v.RemoveGrain(mode=1).RemoveGrain(mode=5) : v
vmask = (masktype==2) ? canny(baseline, sensitivity).mt_expand(mode="both") : baseline.mt_edge(mode = "roberts").mt_expand(mode="both").RemoveGrain(mode=11).RemoveGrain(mode=1).RemoveGrain(mode=5)
v = mt_merge(v, sharp, vmask)
return v
}
Dependencies:
For SpliceResize:
RemoveGrain
VariableBlur (any version that includes unsharp)
Masktools 2
For canny:
tcanny
MPEG2Dec3DG.dll OR DGDecode.dll