08.03.2024, 22:09
I developed another type of Merge. The idea behind is to use the stable values provided by DeOldify, to force DDColor to don't generate an image with colors too different from DeOldify.
In the example below I set the threshold to 10%.
Unfortunately Pillow is unable to work with YUV colors, so I must work with RGB colors and then transfer the changes to YUV.
Do you have any idea on how it is possible to speed up the code ? also the clamping to 10% is not given the expected results and I don't understand way.
Now I'm too tired to think something useful.
Any idea is welcome.
Thanks,
Dan
In the example below I set the threshold to 10%.
def AdaptiveMerge3(clipa: vs.VideoNode = None, clipb: vs.VideoNode = None, clipb_weight: float = 0.0) -> vs.VideoNode:
def merge_frame(n, f):
img1 = frame_to_image(f[0])
img2 = frame_to_image(f[1])
img_m = chroma_smoother(img1, img2)
return image_to_frame(img_m, f[0].copy())
clipm = clipa.std.ModifyFrame(clips=[clipa, clipb], selector=merge_frame)
return clipm
def chroma_smoother(img_prv: Image, img: Image, strength: int = 0) -> Image:
r2, g2, b2 = img.split()
img1_up = Image.eval(img_prv, (lambda x: min(x*(1 + 0.10),255)) )
img1_dn = Image.eval(img_prv, (lambda x: max(x*(1 - 0.10), 0)) )
r1_up, g1_up, b1_up = img1_up.split()
r1_dn, g1_dn, b1_dn = img1_dn.split()
r_m = ImageMath.eval("convert(max(min(a, c), b), 'L')", a=r1_up, b=r1_dn, c=r2)
g_m = ImageMath.eval("convert(max(min(a, c), b), 'L')", a=g1_up, b=g1_dn, c=r2)
b_m = ImageMath.eval("convert(max(min(a, c), b), 'L')", a=b1_up, b=b1_dn, c=r2)
img_m = Image.merge('RGB', (r_m, g_m, b_m))
img_final = chroma_post_process(img_m, img)
return img_final
def chroma_post_process(img_m: Image, orig: Image) -> Image:
img_np = np.asarray(img_m)
orig_np = np.asarray(orig)
img_yuv = cv2.cvtColor(img_np, cv2.COLOR_RGB2YUV)
# perform a B&W transform first to get better luminance values
orig_yuv = cv2.cvtColor(orig_np, cv2.COLOR_RGB2YUV)
hires = np.copy(orig_yuv)
hires[:, :, 1:3] = img_yuv[:, :, 1:3]
final = cv2.cvtColor(hires, cv2.COLOR_YUV2RGB)
final = Image.fromarray(final)
return final
Unfortunately Pillow is unable to work with YUV colors, so I must work with RGB colors and then transfer the changes to YUV.
Do you have any idea on how it is possible to speed up the code ? also the clamping to 10% is not given the expected results and I don't understand way.
Now I'm too tired to think something useful.
Any idea is welcome.
Thanks,
Dan