Code: Select all
#edgefix v2.1 by mirkosp
#A function that supposedly fixes the edges of the image in upscaled stuff.
#Ideally similar results can be achieved through inpainting, but as that process is quite slow and as we can just try to restore the ideal pixel
#value through some process reminiscent of delogoing of partially opaque logos, I made this function, which is rather fast for what it does, as I'm having it work on the edges only.
#Either way, here's the gist of what I'm doing and why.
#Sometimes, with upscaled sources, it happens that the pixels at the borders of the picture are slightly darker or brighter than what they should be.
#Since it might be better to not just crop these pixels away (particularly if one wants to employ an algorithm like debilinear), I made this.
#Keep in mind that some sources might require different settings based on the scene. Mostly if a certain scene was zoomed and thus doesn't need fixing or if
#an edge is darker/brighter than the rest in a single scene for whatever reason.
#
#Usage:
#xedge, yedge, wedge, and hedge are the amount of pixels that need fixing on each side.
#thr lightens the pixel with positive values and darkens them with negative ones. major difference with the older implementation
#radius is used to decide the area on which to calculate the median, which is used as reference on what the pixel's value is supposed to be more or less.
#matrix is, well, avisynth's matrix value. Pick your poison between "Rec601", "PC.601", "Rec709", and "PC.709".
#Radius should always be bigger than the biggest edge value. Perhaps an equal value could work anyway, but I'd advise against it.
#The way I'm doing the speedup could mean that the areas around the corners might not be perfectly filtered. I'm not sure. Worst case, run multiple instances of the filter based
#on your needs and put stuff together with quadratura on your own. That might do. IDK.
#
#Oh, right. Requires avisynth 2.6, masktools 2.0a48+, fillmargins, and quadratura.
#
function edgefix2 (clip c, int "thr", int "xedge", int "yedge", int "wedge", int "hedge", int "radius", string "matrix"){
xedge = default(xedge,1)
yedge = default(yedge,1)
wedge = default(wedge,xedge)
hedge = default(hedge,yedge)
thr = default(thr,45)
radius = default(radius,max(xedge,yedge,wedge,hedge)+1)
matrix = defined(matrix) ? matrix : (c.width > 1024) || (c.height > 600) ? "Rec709" : "Rec601"
c
converttoy8(matrix=matrix)
e = stackvertical(stackhorizontal(crop(0,0,0,radius+1),crop(width-radius-1,0,0,0).turnleft()),stackhorizontal(crop(0,0,-width+radius+1,0).turnleft(),crop(0,height-radius-1,0,0)))
c
fillmargins(xedge,yedge,wedge,hedge)
converttoy8(matrix=matrix)
stackvertical(stackhorizontal(crop(0,0,0,radius+1),crop(width-radius-1,0,0,0).turnleft()),stackhorizontal(crop(0,0,-width+radius+1,0).turnleft(),crop(0,height-radius-1,0,0)))
d = mt_luts(last,last,"med",mt_circle(radius),"y",u=1,v=1)
mt_lutxy(e,d," "+string(thr)+" 16 <= y "+string(thr)+" < y x "+string(thr)+" x 255 / * + ? y 235 "+string(thr)+" + >= y x "+string(thr)+" x 255 / * + ? ?",u=1,v=1)
#thr > 0 ? y < thr ? y : x + thr*(x/255) : y > 255 + thr ? y : x + (thr)*((x)/255)
e = last
se = stackvertical(stackhorizontal(crop(0,0,1,1),crop(c.width-1,0,1,1)),stackhorizontal(crop(c.height,height-1,1,1),crop(width-1,height-1,1,1)))
d
sd = stackvertical(stackhorizontal(crop(0,0,1,1),crop(c.width-1,0,1,1)),stackhorizontal(crop(c.height,height-1,1,1),crop(width-1,height-1,1,1)))
se = mt_lutxy(se,sd," "+string(thr)+" 16 <= y "+string(thr)+" < y x "+string(thr)+" x 255 / * + ? y 235 "+string(thr)+" + >= y x "+string(thr)+" x 255 / * + ? ?",u=1,v=1)
stackvertical(stackhorizontal(se.crop(0,0,1,1),e.crop(1,0,c.width-2,-e.height+1),se.crop(1,0,1,1),e.crop(c.width,0,0,-e.height+1)),e.crop(0,1,0,-1),stackhorizontal(e.crop(0,e.height-1,-c.width,0),se.crop(0,1,1,1),e.crop(c.height+1,e.height-1,c.width-2,0),se.crop(1,1,1,1)))
stackvertical(crop(0,0,c.width,height/2),stackhorizontal(crop(radius+1,height/2,-c.width-radius-1,0).turnright(),c.converttoy8(matrix=matrix).crop(radius+1,radius+1,-radius-1,-radius-1),crop(c.width+radius+1,0,-radius-1,-height/2).turnright()),crop(c.height,height/2,0,0))
converttoyv12(matrix=matrix)
quadratura(last,c,xedge,yedge,-wedge,-hedge)
mergechroma(c)
return last}
Source:
Code: Select all
edgefix2(10,3,0,3,0)
edgefix2(78,2,0,2,0)
edgefix2(78,1,0,1,0)
quadratura(c.edgefix2(78,2,0,2,0),last,0,1,0,-1)
v2.1